-3

I've tweaked an unmanaged package to include an additional Affiliation upsert and it's causing my limit to exceed. if someone can please suggest what I can do to fix the following I would appreciate it.

public void processInteractions(List<Interaction__c> newInteractions) {

    List<hed__Affiliation__c> affiliationsToUpsertagent = new List<hed__Affiliation__c>();


    //added Agent Affiliation on Agent_Key__c

    if (interaction.Affiliated_Agent_Account__c != null) {

        affiliationsToUpsertagent.add(createAgentUpsertAffilFromInteraction(interaction));

        system.debug(affiliationsToUpsertagent);
    }

    // Upsert Affiliations using the Upsert_Agent_Key__c

    if (affiliationsToUpsertagent.size() > 0) {

        logPossibleErrors(Database.upsert(affiliationsToUpsertagent, hed__Affiliation__c.Upsert_Agent_Key__c, false));


    }

    /*
    We want to inject Apex directly into the SOQL query itself!
    What we want to do is create a bind variable. A “bind variable” is simply the term for an Apex variable used inside a SOQL query.
    Salesforce knows you’re using a bind variable when you precede your Apex variable with a colon (:)
    1. we set the string variables for Contact and Agent Account ID's
    2. Then, in our SOQL query, we used a bind variable to find every Affiliation Account in our database that has the same Contact with Agent to see if it exists!
    3. if it does NOT exists, we insert the agent Affiliation
    */


    private hed__Affiliation__c createAgentUpsertAffilFromInteraction(Interaction__c interaction) {



        //Set String Variables
        String checkConID =interaction.Contact__c;
        //String checkAccID =interaction.Agent_Key__c;
        String checkAccID =interaction.Affiliated_Agent_Account__c;
        system.debug(checkAccID); 
        //Use SOQL query with Bind Variables to check for Existing Agent Account on Contact
        List<hed__Affiliation__c> AllAccounts= [select id, name 
                                                FROM hed__Affiliation__c  
                                                WHERE hed__Account__c= :checkAccID AND hed__Contact__c= :checkConID]; 

        //Set a list Affiliation for upsert

        hed__Affiliation__c newAffil1 = new hed__Affiliation__c();
        // intMappingService.applyDataToSObject(interaction, newAffil1); 

        if(AllAccounts.size() ==  0)//if affilisation does NOT already exisits, then create new affiliaition on Contact
        { 

            intMappingService.applyDataToSObject(interaction, newAffil1);//set up mapping for affiliation

            newAffil1.Upsert_Agent_Key__c = interaction.Contact__c + '.' + interaction.Affiliated_Agent_Account__c;
            newAffil1.Upsert_Key__c = interaction.Contact__c + '.' + interaction.Affiliated_Agent_Account__c;// because of the same mapping being used for the first affiliation record we need to replace it with agent
            newAffil1.hed__Status__c='Current';
            newAffil1.hed__Role__c='Applicant';
            newAffil1.hed__Account__c=  interaction.Affiliated_Agent_Account__c;
            newAffil1.hed__Primary__c=True;
            newAffil1.hed__Contact__c=interaction.Contact__c;   

            system.debug(AllAccounts.size()); 
            system.debug(newAffil1.hed__Account__c); 
            system.debug(newAffil1.hed__Contact__c);   

            upsert newAffil1;
        }//EOR_IF  


        return newAffil1;

    }
}
  • Your code seems incomplete. You have missing brackets and you reference variables you haven't defined in the pasted code. These omissions make it difficult to properly assist you. Please edit your post to include a complete, unedited code sample unless you are sure you can remove irrelevant code without making paste errors. – Adrian Larson Dec 10 '19 at 17:16
  • Do you mean soql queries limit or the soql records limit? On which line does the error occur? – David Cheng Dec 11 '19 at 14:49
0

Although it's not fully clear in your provided code, most likely you are doing an insert/update/upsert within a loop (doing it once or more times for each record updated). You need to ensure you are bulkifying your code. And, you should also ensure that you don't have a recursive update going on (where your "after update" trigger causes an update to the same record, which fires the trigger again, etc). Also, look at what actions are calling the functions to see if they are part of triggers.

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

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.