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
Admin_dev.ax1029Admin_dev.ax1029 

How can this be implemented?

Hello,

 

Salesforce doesn't allow to write the triggers on Opportunity contact role and iam not sure how to implement this .

 

I am looking if it is possible to get the most recent campaign from the primary contact role  and assign this campaign to the primary campaign source field in Oppty if this field is null.?

 

There is no way to implement this ? Plz Help

 

Thanks

sfdcfoxsfdcfox

Since, as you say, you can't trigger on OpportunityContactRole, this leaves you with several options, each with a downside:

 

1) Write a scheduled (batch) apex code class. Have the class run on a schedule (e.g. midnight each day), and query for records that need to be updated. The obvious downside is that changes may take up to 24 hours to reflect, but at least you won't have to worry about missing any records.

 

2) Create a Visualforce page that is embedded in the opportunity page layout. This page, when loaded, checks to see if the opportunity needs to be updated, and silently does so, if required. You may set the "height" of the page to zero pixels, and it will not be displayed to the user, but will work. The downside here is that this is an UI update, meaning that API users won't trigger the update until the record is next viewed.

 

3) Create a trigger on the opportunity itself. Since it is likely that there will be edits to the opportunity, you can take this opportunity (pun intended) to update the campaign field directly. This is probably the easiest scenario, and will always (eventually) be triggered, but the time to update is variable and unpredictable.

 

4) You could have a button or link on the page, and train the users to click the button or link to update the campaign source. This gives way to human error, so unless you've an army of robotic employees, you'll probably want to steer clear of this method.

 

Other than that, you'll have to just deal with not having the feature directly available. I would suggest that you find or create an idea on the IdeaExchange.

Admin_dev.ax1029Admin_dev.ax1029

Thx,

  Considering the 3rd option so if i write the trigger on Opportunity and if i edit the opp everytime does the campaign from Contact role gets attached to the primary campaign field in Opp?

Admin_dev.ax1029Admin_dev.ax1029

Iam kinda new to triggers and in implementing this iam struck here, just don't knw what could be the next steps be.

 

trigger PrimaryCampaign on Opportunity (after update) {
list<Opportunity> opp = new list<Opportunity>();
List<Contact> con = new List<contact>();
set<Id> oId = new set<Id>();
set<Id> conId = new set<Id>();
for(Opportunity o :trigger.new)
{
  oId.add(o.Id);
 
}
List<OpportunityContactrole> ocr = [Select o.OpportunityId,o.ContactId,o.IsPrimary, o.Id From OpportunityContactRole o where o.OpportunityId in: oId and o.IsPrimary = true];
List<CampaignMember> cmp = [select CampaignId ,ContactId, CreatedDate from CampaignMember where ContactId in:conId order by CreatedDate];
 
  
 
}