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
tdevmantdevman 

script limit 200001

Ok I was able to get this up to 105 in my batch test method insert for contact. When I do 200, it gets thits error. Any ideas how I can reduce the statements?

trigger ContactAutomationTrigger on Contact (before insert, before update)
{
Set<Id> aSet = new Set<Id>();
Set<Id> aRecordTypeSet = new Set<Id>();
List<Contact> cNewList= Trigger.new;
for(Contact c: cNewList)
{
aSet.add(c.AccountId);
aRecordTypeSet.add(c.RecordTypeId);
}
ContactUtilities.RegionAssign(cNewList, aSet);

   for(Contact c: cNewList)

        {

        aSet.add(c.AccountId);  

        cRTset.add(c.RecordTypeId);

            

        }

        ContactUtilities.RegionAssign(cNewList, aSet, cRTSet); 

   

}

 

 

 public static void Region Assign(List<Contact> cList, Set<Id> aSet, Set<Id> cRTSetId)

    {         

            Map<Id, RecordType> rtMap = new Map<Id, RecordType>();      

            List<String> aList =new List<String>();       

            Map<Id, Account> aMap = new Map<Id, Account>([SELECT Id, channel__c, status__c FROM Account WHERE id =: aSet]);

            Set<String> cRTSetName = new Set<String>();

 

           for(RecordType rt : [Select Name From RecordType Where ID IN :cRTSetId])  

          {

           cRTSetName.add(rt.name);

           }

 

            rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Name IN     

                          :cRTSetName]);

 

            List<Region__c> tList = [select id, zip_start__c, zip_end__c, state__c,Sales_Rep__c, channel__c, recordTypeId from   

                                                        Region__c whereSales_Rep__c != null AND RecordTypeID IN :rtMap.keySet()];      

                                 

            for(Contact c:cList) 

            {

             try 

              {

                for(integer i= 0; i< rList.size(); i++)

                {

                 String mailingPostalCode= String.valueOf(c.mailingPostalCode);

                    if(c.mailingstate== rList.get(i).state__c  && aMap.get(c.accountId).channel__c == rList.get(i).channel__c &&

                       aMap.get(c.accountId).status__c=='Active' )

                    {    

                        if(c.mailingPostalCode >= tList.get(i).zip_start__c && c.mailingPostalCode<=rList.get(i).zip_end__c  ||

                          rList.get(i).zip_start__c ==null  && rList.get(i).zip_end__c ==null)

                        {

                            c.Region__c = rList.get(i).id;

                            c.OwnerId = rList.get(i).Sales_Rep__c;

                        }

                    } 

Best Answer chosen by Admin (Salesforce Developers) 
littledevillittledevil

There are different ways to reduce the Script statements:

 

1) Use transient keyword

2) reduce iterations on list and map

3) clean or set the collections once you no longer need them in code

 

one more way is" On your Personal Information page set the "Show View State in Development Mode" as true.Then you can see which collections are consuming more iterations and then try to reduce them.

 

In Your code:

 

First Solution:

 

for(RecordType rt : [Select Name From RecordType Where ID IN :cRTSetId])  

          {

           cRTSetName.add(rt.name);

           }

 

            rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Name IN     

                          :cRTSetName]);

 

Instead: (any particular reason for using the name?)

 

rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Where ID IN :cRTSetId]]);

 

Second Solution:

 

Instead of "aMap.get(c.accountId).status__c=='Active' " check, you can directly add this to query above:

Map<Id, Account> aMap = new Map<Id, Account>([SELECT Id, channel__c, status__c FROM Account WHERE id =: aSet and Status__c = : 'Active']);

 

This will reduce the iterations further. Let me know if problem still persists.

All Answers

littledevillittledevil

There are different ways to reduce the Script statements:

 

1) Use transient keyword

2) reduce iterations on list and map

3) clean or set the collections once you no longer need them in code

 

one more way is" On your Personal Information page set the "Show View State in Development Mode" as true.Then you can see which collections are consuming more iterations and then try to reduce them.

 

In Your code:

 

First Solution:

 

for(RecordType rt : [Select Name From RecordType Where ID IN :cRTSetId])  

          {

           cRTSetName.add(rt.name);

           }

 

            rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Name IN     

                          :cRTSetName]);

 

Instead: (any particular reason for using the name?)

 

rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Where ID IN :cRTSetId]]);

 

Second Solution:

 

Instead of "aMap.get(c.accountId).status__c=='Active' " check, you can directly add this to query above:

Map<Id, Account> aMap = new Map<Id, Account>([SELECT Id, channel__c, status__c FROM Account WHERE id =: aSet and Status__c = : 'Active']);

 

This will reduce the iterations further. Let me know if problem still persists.

This was selected as the best answer
tdevmantdevman

Thank you so much littledevil.

 

For your question on why I am filtering against cRTSetName is to reduce the collection size in tList().  This is because tList contains different record types and I only need to iterate through the ones that actually match the recordtype.name to the one passed from trigger.new. Since I only have the Ids, I am matching against the name.  I can't use the statement below since the id's in cRTSetId is only recordtypeids in the contact object passed from trigger.new

rtMap = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Where ID IN :cRTSetId]]);

 

I will try the 2nd solution you have below- that actually makes alot of sense to me. Stay tuned..

tdevmantdevman

I was able to succesfully pass 200 batch inserts!  However, I need to add one more condition the soql query by including Accounts that are 'Active' or 'Partner'. The list<> should contain both records that meet any of those conditions but my query below is erroring out. Any ideas on the syntax....i been checking the docs but SFDC doesn't recognize the OR operator.i will mark your post either way as solution=)

 

Map<Id, Account> aMap = new Map<Id, Account>([SELECT Id, channel__c, status__c FROM Account WHERE id =: aSet AND and Status__c = : 'Active'   OR Status__c =: 'Partner' ])

littledevillittledevil

You can not directly use the OR.Try below query:

 

Map<Id, Account> aMap = new Map<Id, Account>([SELECT Id, channel__c, status__c FROM Account WHERE id =: aSet AND and (Status__c = : 'Active'   OR Status__c =: 'Partner' )])