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
ckellieckellie 

Bulkify Trigger

The below trigger works great when it is managing on record at a time, but the trigger will return this error when more than one record is processed at the time. I thought the code was ulkified. What more needs to be done. Here is the trigger.

 

trigger cmOwner on CampaignMember (Before Insert, Before Update) {

    Set<Id> lIds = new Set<Id>();
    Set<Id> cIds = new Set<Id>();
    
    for(CampaignMember cm : Trigger.new) {
    System.debug('**** 1 lIds leadid: '+cm.leadid);
    System.debug('**** 1.5 cIds contactid : '+cm.contactid);

        LIds.add(cm.leadid);
        cids.add(cm.contactid);


    System.debug('**** 2 lIds leadid : '+cm.leadid);
    System.debug('**** 2.5 cIds contactid : '+cm.contactid);
    
   }
   String l;
   String user;
   String sUser;

   sUser = [select id from user where Name = 'Shayla Wentz'].id;
   List<contact> contact = [select ownerid from contact where id in:cIds];
   List<Lead> ow = [select ownerid from lead where id=:lids];

  System.debug('**** 2.5 ow : '+ow);

    List<user> leadowner = [select id from user where id =: ow[0].ownerid];
    
    System.debug('**** 3 leadowner : '+leadowner);
    
    for(CampaignMember c: trigger.new){
      If(leadowner != null){
        c.OwnerName__c = leadowner[0].id;
        System.debug('****************************aa 3 c.lead.ownerid : '+c.lead.ownerid);
        }  else{
        
        c.OwnerName__c = sUser;
        }
        
    if(c.contactid <> null){
        c.OwnerName__c = contact[0].ownerid;
        }
    }  
}

 

Thank you,

ckellie

Best Answer chosen by Admin (Salesforce Developers) 
myforcedotcommyforcedotcom

ckellie,

You need to load leads and contacts into maps first, then check the owners if they are users or a queue.

try the modifications I made below.

 

trigger cmOwner on CampaignMember (before insert, before update) {
	Set<Id> lIds = new Set<Id>();
    Set<Id> cIds = new Set<Id>();
    
    for(CampaignMember cm : Trigger.new) {
	    System.debug('**** 1 lIds leadid: '+cm.leadid);
    	System.debug('**** 1.5 cIds contactid : '+cm.contactid);

    	LIds.add(cm.leadid);
        cids.add(cm.contactid);
        
	    System.debug('**** 2 lIds leadid : '+cm.leadid);
    	System.debug('**** 2.5 cIds contactid : '+cm.contactid);
	}
	//Get Default User
	String sUser = [select id from user where Name = 'Shayla Wentz'].id;

	//Get a map of leads and contacts
   	Map<Id, Contact> contacts = new Map<Id, Contact>([select Id, ownerid from contact where id in:cIds]);
   	Map<Id, Lead> leads = new Map<Id, Lead>([select Id, ownerid from lead where id=:lids]);
	
	for(CampaignMember c: trigger.new){
		try{
			String uId;
			//Lead
			if(c.leadId != null){
				//Check if owner is user or queue
				uId = leads.get(c.LeadId).ownerId;
				if(uId.startsWith('005')){
					c.OwnerName__c = uId;
				}
				else{
					c.OwnerName__c = sUser;
				}
			}
			//Contact
			else if (c.contactId != null){
				uId = contacts.get(c.contactId).ownerId;
				//Check if owner is user or queue
				if(uId.startsWith('005')){
					c.OwnerName__c = uId;
				}
				else{
					c.OwnerName__c = sUser;
				}
			}
		}catch (Exception ee){
			//set to default in case an error happens
			c.OwnerName__c = sUser;
		}
	}
}

 This should do the trick for you.

 

All Answers

bob_buzzardbob_buzzard

What error do you see?

ckellieckellie

While I am not seeing a system code error I am recieving a data error as described below.

 

The purpose of the trigger is if the lead owner is an actual user, then put the user name in the OwnerName__c field on the CampaignMember record. If the lead is owned by a queue, then the owner should default to Shayla Wentz.

 

I am adding four leads to a campaign.

 

Lead 1 Owner - Bill

Lead 2 Owner - Heidi

Lead 3 Owner - Peter

Lead 4 Owner - Jane

 

When I add these leads to the campaign these are the values I recieve in the CampaignMember.OwnerName__c field

 

Lead 1  to CampaignMember 1 - Jane

Lead 2  to CampaignMember 2 - Jane

Lead 3  to CampaignMember 3 - Jane

Lead 4  to CampaignMember 4 - Jane

 

What I am looking for is:

 

 

Lead 1  to CampaignMember 1 - Bill

Lead 2  to CampaignMember 2 - Heidi

Lead 3  to CampaignMember 3 - Peter

Lead 4  to CampaignMember 4 - Jane

 

How do I change my code to achieve the desired result?

 

Thank you for the question. Best Regards,

ckellie



myforcedotcommyforcedotcom

ckellie,

You need to load leads and contacts into maps first, then check the owners if they are users or a queue.

try the modifications I made below.

 

trigger cmOwner on CampaignMember (before insert, before update) {
	Set<Id> lIds = new Set<Id>();
    Set<Id> cIds = new Set<Id>();
    
    for(CampaignMember cm : Trigger.new) {
	    System.debug('**** 1 lIds leadid: '+cm.leadid);
    	System.debug('**** 1.5 cIds contactid : '+cm.contactid);

    	LIds.add(cm.leadid);
        cids.add(cm.contactid);
        
	    System.debug('**** 2 lIds leadid : '+cm.leadid);
    	System.debug('**** 2.5 cIds contactid : '+cm.contactid);
	}
	//Get Default User
	String sUser = [select id from user where Name = 'Shayla Wentz'].id;

	//Get a map of leads and contacts
   	Map<Id, Contact> contacts = new Map<Id, Contact>([select Id, ownerid from contact where id in:cIds]);
   	Map<Id, Lead> leads = new Map<Id, Lead>([select Id, ownerid from lead where id=:lids]);
	
	for(CampaignMember c: trigger.new){
		try{
			String uId;
			//Lead
			if(c.leadId != null){
				//Check if owner is user or queue
				uId = leads.get(c.LeadId).ownerId;
				if(uId.startsWith('005')){
					c.OwnerName__c = uId;
				}
				else{
					c.OwnerName__c = sUser;
				}
			}
			//Contact
			else if (c.contactId != null){
				uId = contacts.get(c.contactId).ownerId;
				//Check if owner is user or queue
				if(uId.startsWith('005')){
					c.OwnerName__c = uId;
				}
				else{
					c.OwnerName__c = sUser;
				}
			}
		}catch (Exception ee){
			//set to default in case an error happens
			c.OwnerName__c = sUser;
		}
	}
}

 This should do the trick for you.

 

This was selected as the best answer
ckellieckellie

Code the Cloud,

 

Your suggestion has solved the problem.

 

Thank you,

ckellie