function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sana123Sana123 

how to insert parent and child object's record dynamically

HEllo everyone ,
I am new to salesforce , i want to know that how can i insert parent object and its field value dynamically and then insert child objects records to it dynamically.

Note :-  i want to pass argument by which i have to pass objects and its field and records

For eg :- I have two args
Map<String, List<String>> mapOfsobject:
In this i pass parent object and list of child objects 

EX:- 'Account'=>{'Contact' ,'Opportunity'}

Map<String, Map<String, String>> mapOfsobjectField

then in this arguments i pass the field and values which i have to insert in objects 

Ex:-'Account'=>new map{'Name'=>'Test'},'Contact'=>new map{'LastName'=>'Test1'}


I have to use dynamic apex in this



I write some code but by my code i only able to insert account i am not able to insert child contact record in the account whicjh i made recently..


Here is my code:--

public class dynamicInsertFieldsRecords {  
    public static void getSobjectRecords(Map<String, List<String>> mapOfsobject ,Map<String, Map<String, String>> mapOfsobjectField ,Map<String, Integer> mapofrecord) {
        
        List<Sobject> listOfParent = new List<Sobject>();
        List<Sobject> listOfChild = new List<Sobject>();
        
        List<Sobject> sObjects = new List<Sobject>();
        List<Sobject> childsObjects = new List<Sobject>();
        
        List<Sobject> litOfsObjects = new List<Sobject>();
        
       // Map< List<Sobject>, List<Sobject>> MapParentChild = new  Map< List<Sobject>, List<Sobject>>();
        
        
        for(String sobj : mapOfsobject.keySet()){
            sObject sObj1 = Schema.getGlobalDescribe().get(sobj).newSObject() ;
            sObjects.add(sObj1);
            
            system.debug(sObjects);
        }
        
        
        for(List<String> objname1 : mapOfsobject.Values()){
            //system.debug(objname1);
            for(String str : objname1)    
            {
                sObject childsObj1 = Schema.getGlobalDescribe().get(str).newSObject() ;
                childsObjects.add(childsObj1);     
            }
        }
        
        
        String objType = String.valueOf(sObjects);
        String ChildobjType = String.valueOf(childsObjects);
        system.debug(objType);
        
        for(String str : mapOfsobjectField.Keyset()){
            system.debug(str);
            
            if(objType.contains(str)){
                //System.debug('str: ' + str);
                for(Integer i = 0; i < mapofrecord.get(str); i++){
                    //System.debug('i: ' + i);
                    sObject sObj = Schema.getGlobalDescribe().get(str).newSObject() ;
                    
                    //System.debug('sObj: ' + sObj);
                    for (String K : mapOfsobjectField.get(str).keyset()){
                        // System.debug('k  '+k);
                        sObj.put(k, (mapOfsobjectField.get(str).get(k) + i));
                        // System.debug('SObject: ' + sObj);
                    }
                    listOfParent.add(sObj);
                    System.debug('SObject: ' + sObj);
                }
                System.debug('listOfParent: ' + listOfParent);
            }  
        }
        insert listOfParent;
        // 
        Map<Id, Sobject> recordsMap = new Map<Id, Sobject>();
        recordsMap.putAll(listOfParent);
        Set<Id> recordIds = recordsMap.keySet();
        system.debug(recordIds);
        
        
        
        for(Id accId : recordIds ){
            String sObjName = accId.getSObjectType().getDescribe().getName();
             SObjectType objToken = Schema.getGlobalDescribe().get(sObjName); 
            DescribeSObjectResult objDef = objToken.getDescribe();
             Map<String, SObjectField> fields = objDef.fields.getMap();
            system.debug(sObjName);
            system.debug(objToken);
            system.debug(objDef);
            system.debug(fields);
            
            for(String str : mapOfsobjectField.Keyset()){
                system.debug(str);
                
                if(ChildobjType.contains(str)){
                    //System.debug('str: ' + str);
                    for(Integer i = 0; i < mapofrecord.get(str); i++){
                        //System.debug('i: ' + i);
                        sObject sObj = Schema.getGlobalDescribe().get(str).newSObject() ;
                        
                        //System.debug('sObj: ' + sObj);
                        for (String K : mapOfsobjectField.get(str).keyset()){
                            // System.debug('k  '+k);
                            sObj.put(k, (mapOfsobjectField.get(str).get(k) + i));
                            System.debug('SObject: ' + sObj);
                        }
                        
                        // listOfChild.add(sObjName);
                        listOfChild.add(sObj);
                        System.debug('SObject: ' + sObj);
                       // MapParentChild.put(listOfParent,listOfChild);  
                     //    System.debug('listOfParent: ' + MapParentChild);
                    }
                    System.debug('listOfParent: ' + listOfChild);
                
                    
                    
                }  
            } 
            
        }
       // insert listOfChild;
      // update MapParentChild.values();
    }
}

Can someone please help me to resolve this
 
SFDC12SFDC12
Hi  Sana,u can use triggers to insert Account along with child object records(contact ,opportunity).

 Trigger:

trigger Insertrelatedlist on Account (After insert) {
    List<contact>contacts=new List<contact>();
    List<opportunity>opplist=new List<opportunity>();
    for(Account a:Trigger.new){
        contact c=new contact();
        c.LastName=a.name;
        c.firstName=a.name;
        c.AccountId=a.id;
        contacts.add(c);
        opportunity o=new opportunity();
        o.name=a.name;
        o.StageName='NeedAnalysis';
        o.closeDate=system.today();
        o.AccountId=a.id;
        opplist.add(o);
    }
    insert contacts;
    insert opplist;
}

Thanks
A.Anshi
Sana123Sana123
sorry but please can you help me without using trigger as the object is not fixed
The object in which i have to insert is passed from argument
As you see in my code

Please help me in this