+ Start a Discussion
Sneha Goli 7Sneha Goli 7 

variable not found error

Hi,
Below is my code which looks prety good for me but dont know what the problem is.
I get the below error sayimg Variable does not exist: CampaignId in line 10 and 20 


trigger LeadCampaignTrigger on Lead(before insert, before update){
    
    Id canadaLeadRecTypId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Canada Leads').getRecordTypeId();
    Id usLeadRecTypId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('US Leads').getRecordTypeId();
    
    Map<String, String> CampaignIdMap = new Map<String, String>();
    Set<String> setOfCampaignIds = new Set<String>();
    for(Lead l : Trigger.new){
        if(l.RecordTypeId == canadaLeadRecTypId || l.RecordTypeId == usLeadRecTypId){
           setOfCampaignIds.add(l.CampaignId); 
        }
    }
    
    for(Campaign camp : [SELECT Id, Name FROM Campaign WHERE Id IN :setOfCampaignIds]){
        CampaignIdMap.put(camp.Id, camp.Name);
    }
    
    for(Lead l : Trigger.new){
        if(l.RecordTypeId == canadaLeadRecTypId || l.RecordTypeId == usLeadRecTypId){
            l.Campaign_Name__c = CampaignIdMap.get(l.CampaignId);
        }
    }
}
 
Raj VakatiRaj Vakati

'CampaignID' field is not accessible on the Lead record


On Lead detail page there is field as campaign (Lookup). When user try to access same record through API, field is not visible in workflow and apex class.


https://help.salesforce.com/articleView?id=000205967&type=1





User is trying to access CampaignId field from Lead object . This field is not accessible if you query a Lead object using API. User can get the details by querying CampaignMember object through API. 

Below are few of the useful links: 

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xZbIAI 

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_campaignmember.htm 

 
Raj VakatiRaj Vakati
https://help.salesforce.com/articleView?id=000005401&type=1
https://success.salesforce.com/answers?id=90630000000gsSlAAI
https://developer.salesforce.com/forums/?id=906F00000008xZbIAI
Prashant Pandey07Prashant Pandey07
Hi Sneha,

if you are trying to access CampaignId field from Lead object then its not accessible if you query a Lead object using API. you  can get the details by querying CampaignMember object through API. 

https://developer.salesforce.com/forums/?id=906F00000008xZbIAI

https://help.salesforce.com/articleView?id=000005401&type=1


--
Thanks,
Prashant
Shivdeep KumarShivdeep Kumar
Hi Sneha,
Try below code
trigger LeadCampaignTrigger on Lead(after insert, after update){
    
    Id canadaLeadRecTypId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Canada Leads').getRecordTypeId();
    Id usLeadRecTypId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('US Leads').getRecordTypeId();
    
    Map<String, String> CampaignIdMap = new Map<String, String>();
    Set<String> setOfCampaignIds = new Set<String>();
for(CampaignMember cm : [Select Id,CampaignId,LeadId,lead.RecordTypeId from CampaignMember where leadId IN: Trigger.NewMap.keySet()]){
if(cm.lead.RecordTypeId == canadaLeadRecTypId || cm.lead.RecordTypeId == usLeadRecTypId){
           setOfCampaignIds.add(cm.CampaignId); 
        }
}
      
    for(Campaign camp : [SELECT Id, Name FROM Campaign WHERE Id IN :setOfCampaignIds]){
        CampaignIdMap.put(camp.Id, camp.Name);
    }
      
}
As per above sample code you can build your own logic.

Please let me know if this help !

Thanks
Shivdeep