0

I am trying to refer the element(listname.contactid) from the list but I am getting error:

Variable doesn't exist error

However I am getting the entire list when I just use list name.

I know I that I am doing something wrong.

Can someone please help me solve this guide me to learn the cause of the this. Thank you. Code is given below.


trigger dlrs_CampaignMemberTrigger on CampaignMember(after delete, after insert) {

    list campaignlist = new list();
    set cmpId = new set();
    if (trigger.isDelete) { //system.debug(trigger.old); 
        for (CampaignMember campMem: trigger.old) {
            CampaignMember cpm = new CampaignMember();
            cpm.CampaignId = campMem.CampaignId;
            cpm.contactid = campmem.contactid;
            cpm.LeadId = campmem.LeadId;
            campaignlist.add(cpm); //error Method does not exist or incorrect signature: void add(CampaignMember) from the type CampaignMember 
            cmpId.add(campMem.CampaignId);
            system.debug(campaignlist);
            system.debug(campaignlist.contactid); //---- error Variable doesn't exist: contactid 
        }
        campaign[] campname = [select name from campaign where Id in: cmpId];
        system.debug(campname);

    }
}
4
  • system.debug(campaignlist); //result give below |DEBUG|campaignlist(CampaignMember:{CampaignId=7011s0000008xLWAAY, ContactId=0031s000001XZ7gAAG, LeadId=null})
    – Megnath
    Commented Nov 28, 2019 at 10:38
  • You seem to lack the basic of List and Set, I would suggest to learn the basic on trailhead (like Getting started with Apex Unit) and look at error closely.
    – Raul
    Commented Nov 28, 2019 at 10:55
  • Thank you for the Guidance Rahul.
    – Megnath
    Commented Nov 28, 2019 at 11:03
  • 1
    it would be unusual to have to edit a DLRS trigger as these are generated by the DLRS package
    – cropredy
    Commented Nov 28, 2019 at 22:30

2 Answers 2

0

what I can understand from your code is you are trying to access contactId from a List type variable.

system.debug(campaignlist.contactid); //---- error Variable doesn't exist:

You need to iterate through campaignList and then access the contactid like below:

for(CampaignMember cmp :campaignlist){
   cmp.contactId;
   system.debug('contact id '+cmp.contactId);
}
1
  • Thank you Sunil
    – Megnath
    Commented Nov 28, 2019 at 11:09
0

Here is the correct code, my recommendation is to study more...

trigger dlrs_CampaignMemberTrigger on CampaignMember (after delete, after insert) {

    list<CampaignMember> campaignlist = new list<CampaignMember>(); //right way to declare a list
    set<ID> cmpId = new set<ID>(); // right way to declare a set
    if (trigger.isDelete) { //system.debug(trigger.old); 
        for (CampaignMember campMem: trigger.old) {
            CampaignMember cpm = new CampaignMember();
            cpm.CampaignId = campMem.CampaignId;
            cpm.contactid = campmem.contactid;
            cpm.LeadId = campmem.LeadId;
            campaignlist.add(cpm); //error Method does not exist or incorrect signature: void add(CampaignMember) from the type CampaignMember 
            cmpId.add(campMem.CampaignId);
            system.debug(campaignlist);
            system.debug(cpm.contactid); //right way to get the ContactID
        }
        string campname = [select name from campaign where Id in: cmpId limit 1].name; //right way to get the Campaign Name
        system.debug(campname);

    }

}
1
  • Thank you Peixoto. I will study more. Have a great day.
    – Megnath
    Commented Nov 28, 2019 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .