function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AndrewHammeAndrewHamme 

Automatically Add Contact Role Record to Opportunity

As you know, the Contact Role object can not have fields added to it.  In order to get around this I have created a custom object called Contact Roles and gave it a child relationship to the opportunity object.  The custom object has the same two fields that are on the Contact Role object, ContactID and Role, but also has additional fields on it.

What I would like to have happen is every time a Contact name and Role has been entered into the custom object and saved, a new Contact Role is automatically created on the opportunity with the same Contact name and Role.  Does anyone know of a solution for this?

 

Regards,

Andrew

hisrinuhisrinu

Hi,

 

 You need to write a trigger on your new custom object for after insert, which inturn create a new record in the standard OpportunityContactRole object.

 

trigger abc on Cotnact Role

{

   for(Contact Role c : Trigger.New)

   {

        OpportunityContactRole o = new OpportunityContactRole;

        o.ContactId = set the contact id here;

        o.OpportunityId = set the Opportunity id here; 

        insert o;

   }

}

AndrewHammeAndrewHamme
Thank you Srini for your post.  I am on the right track now.  I can get it to work as long as I manually pass the opportunity and contact IDs, but am getting errors when trying to pass the values through a variable.  What variables/expressions should I be using to pass the opportunity and contact IDs to the OpportunityContactRole object?
AndrewHammeAndrewHamme

I got it.  Thank you for your help!

 

Andrew

Lisa HorneLisa Horne
I need to do this same thing.  Would someone be able to help me do this?  I am new to apex.  Thanks,  LIsa