+ Start a Discussion
ppiyushppiyush 

Changing Contact Role in Opportunity

Hello everyone!

 

What I would like to do is to create a trigger which sets the primary contact role of an opportunity to a custom field I have created called "Primary Contact". Now I got some code off the internet which does it for me, and it works well, but I am not sure how it does it....

 

Can anyone enlighten me?  I want to make sure I am implementing the right stuff...

 

 

trigger ContactRoleUpdate on Opportunity (after update) {

	Opportunities o = new Opportunities();   
	o.SetContactRoleDefaults(Trigger.new);

}

 

And the Opportunities class is:

 

 

public class Opportunities {

    // Default Constructor
    public Opportunities()
    {
    }

    // Sets default values on the Contact Role record created during Conversion. Called on AFTER INSERT
    public void SetContactRoleDefaults(Opportunity[] oppty)
    {

	for(OpportunityContactRole oldocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :oppty]) {
		if (oldocr.IsPrimary=true){
			delete oldocr;
		}
	}
	
	
	OpportunityContactRole ocr = new OpportunityContactRole();
	ocr.OpportunityId = oppty[0].id;
	ocr.ContactId = oppty[0].Related_Contact__c;
	ocr.IsPrimary = true;
	ocr.Role = 'Business User';
	
	insert ocr;

    }
}

 

 

 

Best regards,

Pranav

Pradeep_NavatarPradeep_Navatar

You want to set Opportunity Primary Contact Role in a custom field “Primary Contact” right? But in the above code it seems that you are setting the Role in Opportunity Primary Contact Role standard field.

 

Is this working fine as per your requirements?