You need to sign in to do that
Don't have an account?
Yeshi Mohammed 9
Update # of opportunity contact role - using Apex trigger
Hello,
We have a requiremet to have atleast one opportunity Contact role associated to an opportunity. I have proces to make sure contacts are assocaited to opportunity when they are created. But for existing opportunities, when users updates thier existing opportunities, I want to send them a reminder email alert to update with Contact role. I found this (https://salesforce.stackexchange.com/questions/25622/trigger-that-counts-how-many-contact-roles-have-a-specific-role-on-an-opportunit) Apex trigger code and updated to fit my requirement. here is my code. when there is Contact role associated to the opportunity my custom field get updated correctly but when there is no contact Role this code does not update my custom field to Zero. How do I get the code to update "Number_of_Contact_Roles__c" to 0 when no contact role ? I want to use this value for email alert.
Thank you
-Yeshi
We have a requiremet to have atleast one opportunity Contact role associated to an opportunity. I have proces to make sure contacts are assocaited to opportunity when they are created. But for existing opportunities, when users updates thier existing opportunities, I want to send them a reminder email alert to update with Contact role. I found this (https://salesforce.stackexchange.com/questions/25622/trigger-that-counts-how-many-contact-roles-have-a-specific-role-on-an-opportunit) Apex trigger code and updated to fit my requirement. here is my code. when there is Contact role associated to the opportunity my custom field get updated correctly but when there is no contact Role this code does not update my custom field to Zero. How do I get the code to update "Number_of_Contact_Roles__c" to 0 when no contact role ? I want to use this value for email alert.
trigger countOpportunityContactRole on Opportunity (before insert, before update) { // Only need to enforce rule if opportunity is Open Map<Id, Opportunity> oppsToCheckMap = new Map<Id, Opportunity>(); for(Opportunity opp : trigger.new) { // Add the opp Id to a Map(Id to Opp) if it is an insert/Upgrade trigger with a Open Status if(opp.ForecastCategoryName == 'Pipeline' || opp.ForecastCategoryName == 'Upside' || opp.ForecastCategoryName == 'Commit') { oppsToCheckMap.put(opp.Id, opp); } // For each Opportunity in the map keyset get the count of the List<AggregateResult> result = [ select OpportunityID, count(Id) from OpportunityContactRole where OpportunityID in :oppsToCheckMap.keySet() group by OpportunityId]; for(AggregateResult aggCount : result) { integer roleCount = (integer)aggCount.get('expr0'); if(roleCount == 0) { // update cutom field to 0 opp.Number_of_Contact_Roles__c = roleCount; } // update cutom field to # of contact role opp.Number_of_Contact_Roles__c = roleCount; } } }
Thank you
-Yeshi
Please change the code from same comment line as mentioned,
to this code.....in ur code..
*********************************************************************************
// For each Opportunity in the map keyset get the count of the
List<AggregateResult> result = [
select OpportunityID, count(Id)expr
from OpportunityContactRole
where OpportunityID in :oppsToCheckMap.keySet() group by OpportunityId];
for(AggregateResult aggCount : result) {
Integer roleCount = (Integer)aggCount.get('expr');
opp.Number_of_Contact_Roles__c = roleCount;
}
It will work..
Please mark it as BEST ANSWER..
Thanks,
Prasanthi