[SalesForce] To add a list to an instance of another list

anyone have any idea of how to a one list to an instance of another list ..

    public List<WarrantyServices__c> GetResponse(){

        if(tnPSResponse != NULL) {      
            for(tnsPartservicesv13.PartServices ps : tnPSResponse.PartServices.PartServices) {          

}

In the above method, I_ws is an instance of WarrantyServices__c list and i am passing I_ws instance as an input to the above method . Now my problem is that
i want to add PSlst to I_ws which is an instance of warrantyservices list.

Above is my entire class, I want PSlst to add to I_ws, meaning when I click on warranty I should get partlist related to that warranty. I have added I_ws to wrapper class list .

Best Answer

To note:

  • I_ws is a single instance of WarrantyServices__c
  • DSlst is a list<Driver__c>
  • PSlst is a single instance of Part_Services__c

Since they are all different types, you can't just create a list of all of them together. I suggest creating a subclass that includes all the types you'd like to return.

Note: You can always create constructors and instance methods for any subclass you create.

Example:

public class WarrentyResponse{
    public WarrantyServices__c warrentyServices {get;set;}
    public list<Driver__c> driverList {get;set;}
    public Part_Services__c partServices {get;set;}

    public WarrentyResponse(){
        this.warrentyServices = new WarrantyServices__c();
        this.driverList = new list<Driver__c>();
        this.partServices = new Part_Services__c();
    }//END init()

    // another constructor
    public WarrentyResponse(list<Driver__c> driverList){
        this.warrentyServices = new WarrentyServices();
        this.driverList = driverList;        //<--------- what's different
        this.partServices = new Part_Services__c();
    }//END init(list<Driver__c> driverList)


    // Example of an instance method
    public integer getNumberOfDrivers(){
        return driverList.size();
    }//END getNumberOfDrivers()
}//END WarrentyResponse

public WarrentyResponse GetResponse( WarrantyServices__c I_ws, tnsPartservicesresponsev13.PartServicesResponse tnPSResponse, tnsDriverdetailsresponsev13.DriverDetailsResponse tnDRes){ 
    WarrentyResponse returnResponse=new WarrentyResponse();

    if(tnPSResponse != NULL) {        
          for(tnsPartservicesv13.PartServices ps : tnPSResponse.PartServices.PartServices) {            
               Part_Services__c p = new Part_Services__c();           
               returnResponse.partServices=p;               
          }
    } else if(tnDRes !=NULL) {
          for(tnsDriverdetailsv13.DriverDetails Dd : tnDRes.DriverDetails.DriverDetails){
               List<Driver__c> DSlst = new List<Driver__c>();               
               returnResponse.driverList=DSlst ;
          }
    }
    returnResponse.warrentyServices=I_ws;

    return returnResponse;
}

Update:

Try using this wrapper class instead:

public class TransformOfferSet{

    public List<WrapperService> I_WarrantyService {get;set;}
    public List<WrapperService> II_WarrantyService {get;set;}
    List<WarrantyServices__c> WSLst {get;set;}
    List<Driver__c> DSlst {get;set;}
    List<Part_Services__c> PSlst {get;set;}

    public TransformOfferSet(){
        this.I_WarrantyService = new list<WrapperService>();
        this.II_WarrantyService = new list<WrapperService>();
        this.WSLst = new list<WarrantyServices__c>();
        this.DSlst = new list<Driver__c>();
        this.PSlst = new list<Part_Services__c>();
    }//END init()
}//END TransformOfferSet

Then in your method(s), have the return type be TransformOfferSet, create a new TransformOfferSet instance in your method, do whatever calculations you need, and then set the TransformOfferSet variables appropriately.

What's great about creating a subclass to hold data is you can use TransformOfferSet as both a return type AND an input parameter. Therefore, you can collect/set all the TransformOfferSet variable over multiple methods:

Brief Example:

public TransformOfferSet applyEverything(){
    return applyParts(applyDrivers(applyWarrentyServices()));
}//END applyEverything     

public TransformOfferSet applyWarrentyServices(/* whatever you need */){
    TransformOfferSet returnClass = new TransformOfferSet();

    // do all your data-gathering and calculations
    list<WrapperService> myI_WarrantyService = new list<WrapperService>();
    list<WrapperService> myII_WarrantyService = new list<WrapperService>();
    list<WrapperServices__c> myWSLst = new list<WrapperServices__c>();
    //  ... more calculations...

    returnClass.I_WarrantyService = myI_WarrantyService;
    returnClass.II_WarrantyService = myII_WarrantyService;
    returnClass.WSLst = myWSLst;

    return returnClass;
}//END applyWarrentyServices

public TransformOfferSet applyDrivers(TransformOfferSet offer /*, other params */){
    // gather data
    list<Driver__c> myDrivers = new list<Driver__c>();
    // do desired calculations

    offer.DSlst = myDrivers;

    return offer;
}//END applyDrivers

public TransformOfferSet applyParts(TransformOfferSet offer /*, other params */){
    // gather data
    list<Part_Services__c> myParts = new list<Part_Services__c>();
    // do desired calculations

    offer.PSlst = myParts;

    return offer;
}//END applyParts