2

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 .

  • Please clean up formatting, spelling and grammar. Thank you. – Bob Roberts Oct 8 '13 at 14:44
  • I cleaned up the code formatting, but I am still unsure what you are asking. Could you clarify what you want to know. – Chris Duncombe Oct 8 '13 at 15:01
  • I have edited my question .... please help ... – user3404 Oct 8 '13 at 15:57
11

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
  • thanks for sharing ....... could u pls be more elaborate i have shared my code of how it sholud me in the class . – user3404 Oct 8 '13 at 18:42
  • 1
    great .... its a good idea .... thank you for solving the problem. – user3404 Oct 8 '13 at 19:33
3

As long as the lists are of the same type, in this case WarrantyServices__c, you can use the list method addAll().

l_ws.addAll(DSlst);
l_ws.addAll(PSlst);

EDIT: I see now after answering that they are are not all of the same type, and in some cases not even lists, in which case addAll() would not work. I would defer to the suggestion from amatorVitae above to create a wrapper class for the response.

  • Add all will work .You just have to typecast it into superType. l_ws.addAll(List<Object>DSlst); assuming l_ws is a list<Object> – Pranay Jaiswal Sep 3 '15 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.