Map<ID,RecordType> mRT = New Map<ID,RecordType>();
for(Contact c : trigger.new){
conRT.add(c.RecordTypeID);
}
for(RecordType r : [Select Name From RecordType Where ID IN :conRT])
rtName.add(r.Name);
mRT = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Name IN :rtName]);
List<Region__c> rList = [select id, zip_start__c, zip_end__c, state__c,Region__c, channel__c, recordTypeId from Region__c where SalesRep__c!= null AND RecordTypeID IN :mRT.keySet()];
Your code made alot of sense to make rList a smaller size. Good news is i got the code to work but when i do a batch test method of 200 than it fails again on too many script statements. The highest i can get it to is 95 in a batch insert. i need to look at this more but any ideas how i can make rLIst smaller??. Notice now I add the contact record type to the set and pass it in the method as a parameter....my rList has to be smaller now but why am i still hitting script limits?? thinking......the trigger should only execute if the account associated with the contact.accountid has an account.status = Active...maybe i could work something with that?
Add this:
Set<ID> conRT = New Set<ID>();
Set<String> rtName = New Set<String>();
Map<ID,RecordType> mRT = New Map<ID,RecordType>();
for(Contact c : trigger.new){
conRT.add(c.RecordTypeID);
}
for(RecordType r : [Select Name From RecordType Where ID IN :conRT])
rtName.add(r.Name);
mRT = New Map<ID,RecordType>([Select ID From RecordType Where sObjectType = 'Region__c' and Name IN :rtName]);
List<Region__c> rList = [select id, zip_start__c, zip_end__c, state__c,Region__c, channel__c, recordTypeId from Region__c where SalesRep__c!= null AND RecordTypeID IN :mRT.keySet()];
Your code made alot of sense to make rList a smaller size. Good news is i got the code to work but when i do a batch test method of 200 than it fails again on too many script statements. The highest i can get it to is 95 in a batch insert. i need to look at this more but any ideas how i can make rLIst smaller??. Notice now I add the contact record type to the set and pass it in the method as a parameter....my rList has to be smaller now but why am i still hitting script limits?? thinking......the trigger should only execute if the account associated with the contact.accountid has an account.status = Active...maybe i could work something with that?
trigger ContactAutomationTrigger on Contact (before insert, before update)
{
Set<Id> aSet = new Set<Id>();
List<Contact> cNewList= Trigger.new;
Set<Id> cRTSet = new Set<Id>();
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 where Sales_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;
}
}