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
BA_AdminBA_Admin 

Help with trigger

Hi All,

    I have enabled salesforce to salesforce in our org so when the lead comes from other org it has to be assigned to the campaign directly in our org, so here is the triggerbut the lead is not getting assigned to the campaign, iam not sure where is the error , any help guys

 

trigger addCampaign on Lead (after insert)
{
Campaign C = [Select Id,Name from Campaign where Name = 'Connection' limit 1];
List<CampaignMember> CM = new List<CampaignMember>();
PartnerNetworkConnection  Con = [Select Id, ConnectionName From PartnerNetworkConnection where ConnectionName =:'Company, Inc' limit 1];
for(Lead L : Trigger.New)
{
    if(L.ConnectionReceivedId == Con.Id )
    {
        CampaignMember Cmp = new CampaignMember();
        Cmp.LeadId = L.Id;
        Cmp.CampaignId = C.Id;
        Cmp.Status = 'Responded';
        CM.add(Cmp);
    }
}
    
    insert CM;
}

 

cloudcodercloudcoder

There are a bunch of things going on here. For starters, the trigger has not been bulkified and the campaign is always going to 'Connection' and the partnerConnection is always 'Company Inc' (btw there is a syntax error there, you don't need the ':' unless you are passing in a variable.

 

My first question would be, are you sure it is even getting past your if(L.ConnectionReceivedId == Con.Id ) statement? I suspect it is not.

BA_AdminBA_Admin

Hi,

   I modified the trigger by removing the syntax error ':' and i guess the connection name is 'Company, Inc' and this is what i see on my connections tab but i made that to 'Company Inc' and tried by passing a lead from other org and it gave me trigger exception error  and the lead is not passing so i made that to back 'Company, Inc' and passed the lead i was able to get the lead but not able to assign it to campaign 'Connection'.

                        I was successfully able to assign the campign to lead if my code is below one but the only questions is i cannot hardcode it since the Connection Id in my sandbox and production is different so i thought it could be able to deploy it if i use the connection name instead of Id, please correct me if i was wrong as u said iam just starter.

 

trigger addCampaign on Lead (after insert)
{
Campaign C = [Select Id,Name from Campaign where Name = 'Connection' limit 1];
List<CampaignMember> CM = new List<CampaignMember>();
for(Lead L : Trigger.New)
{
    if(L.ConnectionReceivedId == '04PS0000000CbJi')
    {
        CampaignMember Cmp = new CampaignMember();
        Cmp.LeadId = L.Id;
        Cmp.CampaignId = C.Id;
        Cmp.Status = 'Responded';
        CM.add(Cmp);
    }
}
   
    insert CM;