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
pranavshahpranavshah 

how to create instance of child object to insert list of records

Hi All,

Can someone tell me is dis ryt way to create instance for child object an to insert the list of records of child object(Cable unit) on Account object...
if not can some one correct it

global class CustomerCunit
{
    global static String createNewAccount(list<accountWrapper> accwrapList) 
    {
        //RestRequest req = RestContext.request;
        //RestResponse res = RestContext.response;
        string returnString='';
        try
        {
        list<account> acclist = new list<account>();
        list<Cable_unit__c> Cunitlist= new list<Cable_unit__c>();
            system.debug('*****' + accwrapList);
            for(accountWrapper wrap : accwrapList) 
            {
            Account acc = new Account();
            Cable_Unit__c Cunit= new  Cable_Unit__c();
            acc.Name= wrap.AccName;
            acc.Potential_Installations__c =wrap.PotentialInstallations;
            acclist.add(acc);
            acc.Customer_Segment_Lookup__c=wrap.CustomerSegmentLookup;
            Cunit.Municipaly__c =wrap.Municipaly;
            Cunit.Housing_Type__c=wrap.HousingTypeLookup;
            Cunit.Housing_Ownership__c=wrap.HousingOwnershipLookup;
            Cunit.Cable_Unit_post_Code__c= wrap.CableUnitPostCode;
            Cunit.Building_Type__c=wrap.BuildingType;
            Cunit.Cable_Unit_No__c=wrap.CableUnitNo;
            CunitList.add(Cunit);
            }
            insert Cunitlist;
            insert acclist;
            if (accList <> null && acclist.size() > 0)
            {
             returnString='Successful';
            }
             else
            {
            returnString='Failed';
            }        
            }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return returnString;
    }
    
    
   global class accountWrapper
       {
        global string AccName;
        global Integer PotentialInstallations;
        global string Municipaly;
        global string CustomerSegmentLookup;
        global string HousingTypeLookup;
        global string HousingOwnershipLookup;
        global string CableUnitPostCode;
        global string CableUnitNo;
        global string BuildingType;
       }
}


Thanks & Regards
Pranav Shah
Niraj Kr SinghNiraj Kr Singh
Hi Pranav,

Your code will not work, Because you will get account id after inserting of acclist only then after you have to inetrate and pass acc id to your related chhild records.

I have modified your code here, try this.

Note: SIze of accList and accwrapList will be same and order of record in list will be same as well so iterate on the size of any one list doesn't matter
global class CustomerCunit
{
    global static String createNewAccount(list<accountWrapper> accwrapList) 
    {
        //RestRequest req = RestContext.request;
        //RestResponse res = RestContext.response;
        string returnString='';
        try
        {
			list<account> acclist = new list<account>();
        
            system.debug('*****' + accwrapList);
            for(accountWrapper wrap : accwrapList) 
            {
				Account acc = new Account();
				acc.Name= wrap.AccName;
				acc.Potential_Installations__c =wrap.PotentialInstallations;
				acclist.add(acc);
				acc.Customer_Segment_Lookup__c=wrap.CustomerSegmentLookup;
			}
            
            if (acclist.size() > 0)
            {
				insert acclist;
				
				//Note: SIze of accList and accwrapList will be same and order of record in list will be same as well so iterate on the size of any one list doesn't matter
				list<Cable_unit__c> Cunitlist= new list<Cable_unit__c>();
				for(Integer index = 0; index < accwrapList.size(); index++) {
					Cable_Unit__c Cunit= new  Cable_Unit__c();
					Cunit.Municipaly__c =wrap.Municipaly;
					Cunit.Housing_Type__c=wrap.HousingTypeLookup;
					Cunit.Housing_Ownership__c=wrap.HousingOwnershipLookup;
					Cunit.Cable_Unit_post_Code__c= wrap.CableUnitPostCode;
					Cunit.Building_Type__c=wrap.BuildingType;
					Cunit.Cable_Unit_No__c=wrap.CableUnitNo;
					Cunit.Account__c = acclist[index].Id;		//Here building relationship between Account(Parent) and Cable_Unit__c(Child). You can give Cunit.Account__c this api name according to you.
					CunitList.add(Cunit);
				}
				insert CunitList;
				returnString='Successful';
            }
            else
            {
				returnString='Failed';
            }        
        }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
        return returnString;
    }
    
    
    global class accountWrapper
    {
		global string AccName;
		global Integer PotentialInstallations;
		global string Municipaly;
		global string CustomerSegmentLookup;
		global string HousingTypeLookup;
		global string HousingOwnershipLookup;
		global string CableUnitPostCode;
		global string CableUnitNo;
		global string BuildingType;
    }
}


If it helps you, plz mark your ans.
Thanks
Niraj
pranavshahpranavshah
  Cunit.Housing_Type__c=wrap.HousingTypeLookup;
            Cunit.Housing_Ownership__c=wrap.HousingOwnershipLookup;,....

housing type and housing ownership are lookup object....now my requirement is,,,

External system doesn't know the salesforce id for above 2 object field..the willl send me the default value for the same,, how can i fetch the date thorugh this object,... like how can i query the same,,