You need to sign in to do that
Don't have an account?
huskerwendy
Trigger to check if Primary Contact changes on opportunity
I found this app on the appexchange that will validate if there is a primary contact on the opportunity. However, I also need to know if the primary contact has changed. This is a link to the app:
https://appexchange.salesforce.com/listingDetail?listingId=a0N300000025Vs1EAE
I'm trying to modify the code but am getting an error.
What am I doing wrong?
https://appexchange.salesforce.com/listingDetail?listingId=a0N300000025Vs1EAE
I'm trying to modify the code but am getting an error.
trigger updatecontactrolecount on Opportunity (before insert, before update){ Boolean isPrimary; Integer iCount; Boolean isPrimaryChanged; Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map for (Integer i = 0; i < Trigger.new.size(); i++) { oppty_con.put(Trigger.new[i].id, Trigger.new[i]); } isPrimary = False; isPrimaryChanged = False; for (list<OpportunityContactRole> oppcntctrle :[select OpportunityId, ContactID from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())]){ if (oppcntctrle .Size() >0) { isPrimary = True; //for (Opportunity o : trigger.new){ //OpportunityContactRole contactRole =[select ContactID from OpportunityContactRole where IsPrimary = true and OpportunityId = :oppcntctrle.id]; for (list<OpportunityContactRole> oldContactRole :[select OpportunityID, ContactID from OpportunityContactRole where OpportunityContactRole.IsPrimary = True and OpportunityId IN :Trigger.oldMap.keySet()]){ if(oldContactRole.ID != oppcntctrle.ContactID){ isPrimaryChanged = true; } } //} } } iCount = 0; for (List<OpportunityContactRole> oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles { if (oppcntctrle2 .Size()>0) { iCount= oppcntctrle2 .Size(); } } for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required { Oppty.Number_of_Contacts_Roles_Assigned__c = iCount; Oppty.Primary_Contact_Assigned__c =isPrimary; Oppty.Primary_Contact_Changed__c= isPrimaryChanged; } }I'm getting an error (Initial term of field expression must be a concrete SObject: List) on line 21 if(oldContactRole.ID != oppcntctrle.ContactID){
What am I doing wrong?
Please try below code.
@Wendy, as I mentioned earlier, change List<OpportunityContactRole> to OpportunityContactRole and save your file. You may or may not get additional errors, but that will fix the one on line 20.