You need to sign in to do that
Don't have an account?
ckellie
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
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.
This should do the trick for you.
All Answers
What error do you see?
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
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.
This should do the trick for you.
Code the Cloud,
Your suggestion has solved the problem.
Thank you,
ckellie