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
Sainath VenkatSainath Venkat 

condition on map field in apex class

I am working on apex class which will fetch details from Campaign Member object, once it is fetched I am looking at map size, if it is greater than zero then I am creating a record in Staging_Event_Attendee__c object.
in Campaign Member if ContactId field is not null then I am populating Staging_Event_Attendee__c.Attendant = ContactId, now same like that if LeadId is not null then I need to populate Staging_Event_Attendee__c.Lead__c = LeadId
Can anyone help me out in this issue, on Campaign Member record I can have either leadId or ContactId at time.
I need to have a condition to check whether LeadId or ContactId is not null, if leadId is not null then I need to create a new record where Staging_Event_Attendee__c.Lead__c = CampaignMap.get(Con.Barcode__c).LeadId If ContactId is not null then I need to create a new record where Staging_Event_Attendee__c.Lead__c = CampaignMap.get(Con.Barcode__c).ContactId
 
Map<Id,CampaignMember> CampaignMap = new Map<Id,CampaignMember>([select ContactId,CampaignId from CampaignMember where Id in: barcodeSet]);
        for(Staging_Event_Attendee__c con : stagingEventAttendeeList) {
            if(CampaignMap.size()>0 ){
            con.Attendant__c = CampaignMap.get(con.Barcode__c).ContactId;
            con.Campaign_ID__c = CampaignMap.get(con.Barcode__c).CampaignId;
            con.Date_Attended__c = system.now();
        }
            else{
                con.Invalid_Barcode__c = true;
                con.Date_Attended__c = system.now();
            }
        }

 
Best Answer chosen by Sainath Venkat
Raghu NaniRaghu Nani
Hi Sai,

use below code, it might help you

Map<Id,CampaignMember> CampaignMap = new Map<Id,CampaignMember>([select ContactId,LeadId,CampaignId from CampaignMember where Id in: barcodeSet]);
        for(Staging_Event_Attendee__c con : stagingEventAttendeeList) {
            if(CampaignMap.size()>0 ){
                if(CampaignMap.get(con.Barcode__c).LeadId != null) {
                    con.Lead__c = CampaignMap.get(con.Barcode__c).LeadId;
                }else{
                    con.Attendant__c = CampaignMap.get(con.Barcode__c).ContactId;
                }
                con.Campaign_ID__c = CampaignMap.get(con.Barcode__c).CampaignId;
                con.Date_Attended__c = system.now();
            }
            else{
                con.Invalid_Barcode__c = true;
                con.Date_Attended__c = system.now();
            }
        }

All Answers

Raghu NaniRaghu Nani
Hi Sai,

use below code, it might help you

Map<Id,CampaignMember> CampaignMap = new Map<Id,CampaignMember>([select ContactId,LeadId,CampaignId from CampaignMember where Id in: barcodeSet]);
        for(Staging_Event_Attendee__c con : stagingEventAttendeeList) {
            if(CampaignMap.size()>0 ){
                if(CampaignMap.get(con.Barcode__c).LeadId != null) {
                    con.Lead__c = CampaignMap.get(con.Barcode__c).LeadId;
                }else{
                    con.Attendant__c = CampaignMap.get(con.Barcode__c).ContactId;
                }
                con.Campaign_ID__c = CampaignMap.get(con.Barcode__c).CampaignId;
                con.Date_Attended__c = system.now();
            }
            else{
                con.Invalid_Barcode__c = true;
                con.Date_Attended__c = system.now();
            }
        }
This was selected as the best answer
Sainath VenkatSainath Venkat
Hello Raghu Nani,

Thanks a lot for helping me out in this issue here, I tried your code but no fields are not getting populated on Staging Event Attendee object from Campaign Member