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

Why can't I insert OpportunityContactRole for existing Opportuntiy?

I want to insert OpportunityContactRole into an existing Opportunity in a controller extended from opportunity standard controller.


Below is my code:

/* Set CampaignId */
opportunity.CampaignId = sCampaignId;

OpportunityContactRole oppContactRole = new OpportunityContactRole(ContactId=sId, OpportunityId=opportunity.Id, IsPrimary=false, Role='Economic Decision Maker');
insert oppContactRole;


After I insert oppContactRole, it has ID but I can not access this object and it is not displayed in opportunities contact roles list after the whole action is over. There is no any exception throwed.

BTW: I can update or delete the existing opportunity's OpportunityContactRole.



I am sorry to say that there are something wrong with my code in that I remove this contact role later by myself.






Try with this code


opportunity op = [select id,name from opportunity where name=:'opportunity name'];

contact c=[select id,name from contact where id=:'some contact record id'];

opportunitycontactrole pcr= new opportunitycontactrole();;;



insert pcr;


were you able to achieve this - because now I have a same kind of requirement... please let me know.