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
jkucerajkucera 

How to remove Primary Campaign Source on Opportunity if Campaign Member Responded is False

To share with the community a discussion I had with another customer, here's the basics for a trigger that checks the campaign member HasResponded field and changes the Opportunity Primary Campaign Source. 

 

This could modified for whatever criteria you want PrimaryCampaignSource to use.

 

trigger checkPrimaryCampaignSource on Opportunity (before update) {
for (Opportunity o: Trigger.new) {
Lead l = [SELECT id,converteddate FROM Lead WHERE ConvertedOpportunityId =:o.ID LIMIT 1];
try {
if(l.converteddate!=null){
CampaignMember cm = [Select id,Status, HasResponded FROM campaignmember WHERE LeadId=:l.id AND CampaignId=:o.Campaign.id];
if (cm.HasResponded ==false){
o.CampaignId=null;
}
}
} catch (Exception e){
system.debug('error: ' +e);
}
}

 


}
Message Edited by jkucera on 12-21-2009 04:37 PM
LBartonLBarton

The advice gets rid of the campaign for an opportunity.  I want to add a campaign to an opportunity and not have it be Primary.  How do I do that?  I tried this in VB.NET

           

Dim oSFOpportunity As sForce.Opportunity = oQR.records(0)

            oSFOpportunity.CampaignId = SCampaignID

            oSFOpportunity.Campaign_Primary__c = Nothing

 I see the new campaign linked to the Opportunity, but marked as Primary.  What should I do?