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
Sumant KuchipudiSumant Kuchipudi 

"System.LimitException: Too many SOQL queries" ERROR in RestResource

Hi, 
I have RestResource (httppost) that receives the data (JSON) from third party, they have bunch of members information which we need to upsert them into our system. if JSOn contains couple of records then fine but when they send 20 records then failing with "FATAL_ERROR|System.LimitException: Too many DML statements: 151". please see  below code. What is the best way to process those bunch of records? 
 
@RestResource(urlMapping='/ELPMData/*')
global with sharing class ELPMData {
	@httpPost
    global static String getMembersData(List<ELPMember> members){
        try{
            for (ELPMember elp: members){
                Bio bio = elp.bio;
                Account acc = new Account();
                acc.Name = bio.LastName+', '+bio.FirstName;
                acc.External_ID__c = bio.ID;
                acc.State__c = bio.State;
                acc.Type = 'Member';
                upsert acc External_ID__c;

                Contact con = new Contact();
                con.AccountId = acc.id;
                //Other contact fields mapping here
                upsert con External_ID__c;

                if (elp.ContactInformations != null){
                    List<Address__c> addrList = new List<Address__c>();
                    for (ContactInformation ci:elp.ContactInformations){
                        if (ci.AddressID != null && ci.AddressID != ''){
                            System.debug(' INSIDE ADDRESS '+ci.AddressID);
                            Address__c add = new Address__c();
                            add.AddressID__c = ci.AddressID;
                            //Other Address fields mapping here
                            addrList.add(add);
                        }
                    }
                    if (addrList.size()>0){
                        upsert addrList AddressID__c;
                    }
                }
                if (elp.Affiliations != null){
                    List<Affiliation__c> affilList = new List<Affiliation__c>();
                    for (Affiliation aff:elp.Affiliations){
                        if (aff.UniqueID != null && aff.UniqueID != ''){
                            Affiliation__c elpAffl = new Affiliation__c();
                            elpAffl.UniqueID__c = aff.UniqueID;
                            //Other Affiliation__c fields mapping here
                            affilList.add(elpAffl);
                        }
                        
                    }
                    if (affilList.size()>0){
                        upsert affilList UniqueID__c;
                    }
                }
                if (elp.Educations != null){
                    List<Education__c> eduList = new List<Education__c>();
                    for (Education edu:elp.Educations){
                        if (edu.InstitutionID != null && edu.InstitutionID != ''){
                            Education__c elpEdu = new Education__c();
                            elpEdu.InstitutionID__c = edu.InstitutionID;
                            //Other Education mapping here
                            eduList.add(elpEdu);
                        }
                    }
                    if (eduList.size()>0){
                        upsert eduList InstitutionID__c;
                    }
                }
                if (elp.Employments != null){
                    List<Employment__c> empList = new List<Employment__c>();
                    for (Employment emp:elp.Employments){
                        if (emp.UniqueID != null && emp.UniqueID != ''){
                            Employment__c elpEmp = new Employment__c();
                            elpEmp.UniqueID__c = emp.UniqueID;
                            //Other Employments mapping here
                            empList.add(elpEmp);
                        }
                    }
                    if (empList.size()>0){
                        upsert empList UniqueID__c;
                    }
                }
                
               
            }
        }catch(Exception e){
            System.debug('The following exception has occurred: ' + e.getMessage());
			return 'Integration FAILED, check with GU Admins';
        }
        return 'SUCCESS';
    }
    
    global class ELPMember {
        global Bio Bio {get;set;}
        global List<ContactInformation> ContactInformations {get;set;}
        global List<Affiliation> Affiliations {get;set;}
        global List<Education> Educations {get;set;}
        global List<Employment> Employments {get;set;}
    }
    global class Bio{
        //All Bio fields
    }
    global class Education {
        //All Education Fields
    }
    global class Affiliation{
        //All Affiliation fields
    }
    global class ContactInformation{
        //All Contact Fields
    }
    
    global class Employment{
        //All Employment fields
    }
    
}


 they may have 10000 members which we need to insert first time. 
Satyadev Kumar Singh 5Satyadev Kumar Singh 5
You shouldn't use DML statement inside For Loop. Please follow the link https://developer.salesforce.com/page/Apex_Code_Best_Practices
Sumant KuchipudiSumant Kuchipudi
Thanks Satyadev, but the process different here.. please look the steps I need to do from JSON
1) upsert Account
2) upsert Contact with accountid
3) upsert List of Address__c with accountid
4) For Education,
   a) upsert Education account
   b) upsert Education with parent accountid and education account id
5) For Empoyment, same steps as Education

so its difficult to combine all in list especially Step 2 to 5, I need accountid to create records.