+ Start a Discussion
Shawn Low 37Shawn Low 37 

Im new to development, like this is my 1st real Trigger.

We use Campaigns differently than it was designed for. Every lead is assigned to a Campaign when it's created.
I have three (3) text fields on the Lead page payout, CampaignId, Campaign Name, & Campaign Member Id. I would like to use a Trigger to populate those fields.
I would like to capture the following fields from the Campaign member Object
1. CampaignMemberId
I am also trying to capture the CampaignId and the Campaign Name.
I would like to display those three (3) fields on the Lead Obejct page layout.
1. Is this even possisble? Im starting to think it's not.
2. being brand new I don;t have "borrowed code" per say, or a template I can use to build a trigger.
If someone has a template I can use I would be eternally greatful.
I have tried formual fields, and the Process Builder and both were not able to do what I needed.
Naren9Naren9
Hi Shawn,
Below is the sample code.Modify according to your environment.
I have tested below code in my Org and it is working.

Below Trigger will run when a New Lead is Inserted or Updated.
If that Lead is assigned with a Campaign, then it will get the Campaign Name and Update the Lead Description Field.
1. First you need to Identify the relationship field between Lead and Campaign. In my case it is related by CampaignName__c on lead.



trigger LeadCampaignUpdateFields on Lead (before insert,before Update) {
    for (Lead L: Trigger.new)
    {
        system.debug('Campaigns found.'+L.CampaignName__c);
        if(L.CampaignName__c!=null)
        {
            //Search for Matached Campaign
            List <Campaign> matchingCampaign = [Select Id,Name from Campaign where Id=:L.CampaignName__c limit 1];
            system.debug(matchingCampaign.size() + 'Campaigns found.');
            if(!matchingCampaign.isEmpty())
                    {
                       L.Description= matchingCampaign.get(0).Name;
                       L.CampaignId = matchingCampaign.get(0).Id;
 L.Campaign Name= matchingCampaign.get(0).Name;
//Campaign Member Id
                    
                        
                    }
        }
    }

}

Thanks,
Narendar
Shawn Low 37Shawn Low 37
Hi Narender,

Thank you so much, having code to look at like that really helps me as I learn this stiuff.

Ok so here's what I did.
I currently use Sublime3 & MavensMate for my IDE (I think that's what they are called)
Anyway, I copy and pasted the code you wrote into Sublime3 and Ran/Saved it.
It failed, and here is what the error messages said:

Timestamp: Wed, 20 Mar 2019 12:11:02
Result: [OPERATION FAILED]: triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignName__c (Line: 5, Column: 43)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignName__c (Line: 7, Column: 18)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignName__c (Line: 11, Column: 90)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignId (Line: 18, Column: 26)
triggers/Campaign_Details_Lead_Object.trigger: Invalid type: L.Campaign (Line: 20, Column: 2)

I am possitive I did a terrible job at explaining what I was trying to do, so this is on me.
Here's what I would like to try and do, if possible and the scenario:

1. Leads are created in SFDC through an API Posting. There is code that works (via an External Object I believ) that does some logic, and assignes the Lead to a campaign, so when the Lead is created, there is already a Campaign attached to the lead. (See Red Box in Picture #1)

2. I would like to have the CampaignId show up in the Lead field called "Campaign Id (Green Arrow) and the CampaignMemberId show up in the Lead field called "Campaign Member Id" (Red Arrow)
I would also like to do the same with the CampaignName showing up in a Lead Field called "Campaign Name" (I have not created the custom field yet though so it's not in the picture).

Picture #1

Is this possible, and if so, how would I alter the code to allow this to work?

Thank you very much,
Shawn
Naren9Naren9
Hi Shawn,
 triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignName__c 

You got the above error as above field is not existed in your organization.

Lead and Campaign are related by using the Junction Object called- CampaignMember. You can find this is Setup-->Object Manager.
User-added image

Try with the Below code:

trigger SFDC_LeadCampaign on Lead (before insert,before Update) {
    for (Lead L: Trigger.new)
    {
        
            //Search in CampaignMember by using Lead Id to get the Campaign details
            List <CampaignMember> MatchingCampaignMember = [Select Id,Name,CampaignId from CampaignMember where LeadId=:L.Id limit 1];
            system.debug(MatchingCampaignMember.size() + 'Campaignmember found.');
        // //Search in Campaign by using CampaignMember Id to get the Campaign details
        List<Campaign> MatchedCampaign = [Select Id,Name from Campaign where Id=:MatchingCampaignMember.get(0).CampaignId limit 1];
            if(!MatchedCampaign.isEmpty())
                    {
                       L.Description= MatchedCampaign.get(0).Name;      
                       L.CampaignId = MatchedCampaign.get(0).Id;      
                       L.Campaign Member Id   =   MatchingCampaignMember.get(0).Id          
                                   
                        
                    }
       
    }

}


Also, we can achieve this requirement by using the Formula Fields.
In Lead Object, you can create a new Formula Field CampaignId and Formula Expresion is - CampaignName__r.Id
More details on this link.
https://success.salesforce.com/answers?id=90630000000wlx2AAA

if you don't want to change your fieds to Formula fields, you have to use the Triggers.


Thanks,
Narendar
 
Shawn Low 37Shawn Low 37
I went back to reexamine the Formula Field route, but neither the Campaign or Campaign Member Objects appear on the Formula UI. (See screen shot).
We are forced to work within a Trial version of SFDC (Professional Edition) until SF opens up our "Real" Org (Unlimited Edition).
Could that be casuing the issue?

User-added image

Thanks
Shawn
Naren9Naren9
Not sure why it is not showing in your Org. try with the above modified trigger. 
Shawn Low 37Shawn Low 37
Now I get this erro:

Timestamp: Wed, 20 Mar 2019 14:09:39
Result: [OPERATION FAILED]: triggers/Campaign_Details_Lead_Object.trigger: Missing ';' at 'Member' (Line: 18, Column: 35)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignId (Line: 17, Column: 26)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: Campaign (Line: 18, Column: 26)
triggers/Campaign_Details_Lead_Object.trigger: Invalid type: Member (Line: 18, Column: 35)

I am so sorry for being so uneducated about all these things. I have not covered "List" or "Select" or really how to write the "If - Then" line, and then add a filter (the "where").

Thanks
Shawn
 
Naren9Naren9
Hi Shawn,
As I mentioned you have chnage the code according to your Organaization fields.

Result: [OPERATION FAILED]: triggers/Campaign_Details_Lead_Object.trigger: Missing ';' at 'Member' (Line: 18, Column: 35)
; missing in so I have added in below code.

triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: CampaignId (Line: 17, Column: 26)
triggers/Campaign_Details_Lead_Object.trigger: Variable does not exist: Campaign (Line: 18, Column: 26)
triggers/Campaign_Details_Lead_Object.trigger: Invalid type: Member (Line: 18, Column: 35)
You have to check what fields are actually mapped in your org.

For testing purpose, below code will update the Lead Description with the Campaign Name + Campaign Id.


trigger SFDC_LeadCampaign on Lead (before insert,before Update) {
    for (Lead L: Trigger.new)
    {
        
            //Search in CampaignMember by using Lead Id to get the Campaign details
            List <CampaignMember> MatchingCampaignMember = [Select Id,Name,CampaignId from CampaignMember where LeadId=:L.Id limit 1];
            system.debug(MatchingCampaignMember.size() + 'Campaignmember found.');
        // //Search in Campaign by using CampaignMember Id to get the Campaign details
        List<Campaign> MatchedCampaign = [Select Id,Name from Campaign where Id=:MatchingCampaignMember.get(0).CampaignId limit 1];
            if(!MatchedCampaign.isEmpty())
                    {
                       L.Description= MatchedCampaign.get(0).Name+MatchedCampaign.get(0).Id;      
                      // L.CampaignId = MatchedCampaign.get(0).Id;      
                       //L.Campaign Member Id   =   MatchingCampaignMember.get(0).Id       
                                   
                        
                    }
       
    }

}