I am working with generic object types for first time, and not sure how to go about this. Please advice,how shall i go about creating mainObjectList by initializing the TempsObject within loop before adding to the List mainObjectList.

I have tried below ways :

  1. SObject tempSObject = new SObject(); <-- throws an error Type cannot be constructed: SObject
  2. tempSObject = gd.get(sobjectName).newSObject();. .<----- this adds three empty objectType in the mainObjectList variable. Which isn't expected
  3. mainObjectList.add(new List(tempSObject)); <--- Invalid initializer type SObject found for List: expected an Integer or a List of type SObject

    tempSObject = gd.get(sobjectName).newSObject(); relatedTempSObject = gd.get(relatedSObjectName).newSObject();

    for(integer i=0;i < sobjectMapList.size() ;i++) { Map sObjLstMap = sobjectMapList[i]; system.debug('sObjLstMap : ' + sObjLstMap);

        for(String field : sObjLstMap.keySet())
            if(sObjLstMap.get(field)!=null && String.valueOf(sObjLstMap.get(field)).trim()!='')
                if(fieldTypeMapping.get(field) == Schema.DisplayType.Date )
                        relatedTempSObject.put(field.split('\\.')[1],Date.parse('' + sObjLstMap.get(field)));
                        tempSObject.put(field,Date.parse('' + sObjLstMap.get(field)));

Am not sure, how in case of generic objects we can create mainObjectList without overwriting its data with TempsObject.

  • You can create a generic List<Sobject> just like any other list type. Is that what you're asking? – David Reed Mar 12 '18 at 19:51
  • @DavidReed have already created List<sObject>, which is mainObjectList. thats why am adding tempSObject to this List so i can get all the values of tempSobject within List<SObject>. But the values of TempsObject is all overrwritten in the final outcome of mainObjectList. – user10727 Mar 12 '18 at 19:56
  • i know to solve this if i would have tempObject as a new instance and then added to List<Sobject> , this will solve my problem. But as this is a generic sObjects, am not sure how to go about this using the TempSObject.Hope that clarifies my question. – user10727 Mar 12 '18 at 20:02

This issue does not have to do with generic code per se. You are overwriting a single sObject instance each time you iterate through your for loop. The behavior would be exactly the same if it were an Account or Contact in this situation.

Adding an object to a List does not copy that object, so each time your for loop iterates, it overwrites the fields of tempSObject and then re-adds it to the list. As a result, at the end of your code, your List will contain sobjectMapList.size() count of the same object.

I do not fully understand what this code does, but it appears that you simply need to move the initializations of tempSObject and relatedTempSObject inside the loop. The behavior you observed of "adding three empty objects to the list" appears to be a completely different logic problem.

  • Agree, i need to initialize it, so i had already tried to instantialte tempSobject and relatedTempSObject within loop.... e.g by below ways 1. tempSObject = new SObject(); <-- but this throws an error Type cannot be constructed: SObject 2 o tempSObject = gd.get(sobjectName).newSObject();. .<----- this adds three empty object in the mainObjectList variable. 3. mainObjectList.add(new sObject(){tempsObject}) <---- this also it doesnt accept. – user10727 Mar 12 '18 at 20:22
  • Hence am unable to figure out how to instantiate and add this as per point(3) in the above trials for Generic Object. – user10727 Mar 12 '18 at 20:24
  • You may have other issues in your code. Options 1 and 3 are clearly invalid. Option 2 should have nothing to do with the count of records added to the list. Please edit your question - no debugging in comment threads. – David Reed Mar 12 '18 at 20:24
  • right there was some more issues in my code...fixed and is working fine. – user10727 Mar 12 '18 at 21:56

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.