+ Start a Discussion
tim111tim111 

Trigger error...INVALID FIELD CAMPAIGN ON SOBJECT LEAD

"if the first name of leads is test then add those leads under Test Campaign as campaign members
 if  the first name of leads is not test then add those leads under Final campaign as campaign members"

 

trigger Campaign on Lead (after insert)
{
    for(Lead l:Trigger.new)
    {
        if(Trigger.isInsert)
        {
            List<Lead> lst=[Select Id,Name,Company,Status from Lead where id=:l.Id];
            Campaign camp=[Select Id,Name from Campaign where Name=:'Test Campaign'];
            
            for(Lead b:lst)
            {
                if(b.Name=='Test')
                {
                b.Campaign=camp.Id;
                }
            }
        }
    }
}

 

i am getting a error"INVALID FIELD CAMPAIGN ON SOBJECT LEAD"

please help me i am new to apex.

RizwiRizwi

You have done 3 mistakes.

1. You cant set campaign like this. Use CampaignMember Object.

2. Never run soql queries inside loops.

2. If you want to set some values to a object when inserting or updating, you can use the before troggers

 

yvk431yvk431

What all Rizwi said are true and you must modify your trigger other wise you might face some other issues.

But the main reason for the error you got is with the field name you are using for Campaign on Lead.

 

Generally if we need to access standard lookup field on standard object , the api name needs to be appended with "id"

 

so its

 

b.CampaignId=camp.Id;

 

 

 

--yvk