
Thanks!
trigger SetCustomerStatusAfterOpWin on Opportunity (after insert, after update) {
List<Contact> ContactsToUpdateListOriginal = new List<Contact>();
List<Account> AccountsToUpdateListOriginal = new List<Account>();
for( Opportunity o : Trigger.new)
{
//Get contact from Opportunity
string contactId = o.Primary_Contact__c;
//Contact contactToUpdate = new Contact(Id = contactId);
Contact contactToUpdate = [SELECT Id, RM_ID_Status__c, RSM_ID_Status__c, RWC_ID_Status__c
FROM Contact
WHERE Id = :contactId limit 1];
//Get contacts account id
string accountId = o.AccountId;
//Account accountToUpdate = new Account(Id = accountId);
Account accountToUpdate = [SELECT Id, RM_ID_Status__c, RSM_ID_Status__c, RWC_ID_Status__c
FROM Account
WHERE Id = :accountId limit 1];
//Get Opportunity's Name eg. RM-60013
string opName = o.Name;
//Update contact based on company and op status.
if((opName.contains('RM') || opName.contains('RAMP')))
{
if( o.StageName == 'Closed Won')
{
//update contact by id
if(contactToUpdate.Id != null)
{
contactToUpdate.RM_ID_Status__c = 'Customer';
ContactsToUpdateListOriginal.add(contactToUpdate);
//update contactToUpdate;
}
//Update Contact's account status
if(accountToUpdate.Id != null)
{
accountToUpdate.RM_ID_Status__c = 'Customer';
AccountsToUpdateListOriginal.add(accountToUpdate);
//update accountToUpdate;
}
}
else if( o.StageName == 'Quote Sent' || o.StageName == 'Closed Lost' || o.StageName == 'Sale Pending' || o.StageName == 'Updated Quote')
{
//update contact by id
if(contactToUpdate.Id != null && contactToUpdate.RM_ID_Status__c != 'Customer')
{
contactToUpdate.RM_ID_Status__c = 'Quoted';
ContactsToUpdateListOriginal.add(contactToUpdate);
//update contactToUpdate;
}
//Update Contact's account status
if(accountToUpdate.Id != null && accountToUpdate.RM_ID_Status__c != 'Customer')
{
accountToUpdate.RM_ID_Status__c = 'Quoted';
AccountsToUpdateListOriginal.add(accountToUpdate);
//update accountToUpdate;
}
}
}// end if
It looks to me like you are iterating the query over in the for loop. This will make you run into query limits. Instead you should do the query for the whole trigger set.
See the trailhead example herehttps://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulkThanks,FROM Contact WHERE Id IN :Trigger.new