You need to sign in to do that
Don't have an account?

...caused by: System.StringException: Invalid id: undefined
So I have a strange error. I have a bit of code that is updating leads and adding them as members to campaigns when a field is updated from an external tool with a campign ID. It works fine in my testing, but in the last day have been getting the following error when the external tool is touching the record. From what I can see, the field in quesiton is not being accessed:
LeadCreateCampaignMember: execution of AfterUpdate
caused by: System.StringException: Invalid id: undefined
Trigger.LeadCreateCampaignMember: line 24, column 1
I will bold line 24 in this case:
/*
Author: John Athitakis
Synopsis: When a new or existing Lead has a special string field populated, an event is created with that string as its subject line
*/
trigger LeadCreateCampaignMember on Lead (after insert, after update) {
// Logic: On insert if the Event Activity Field is Populated, Create an Event.
if(Trigger.IsInsert) {
for (Lead UpdatedLead: Trigger.new) {
if(UpdatedLead.campaign_id__c!=null){
CampaignMember cml = new CampaignMember();
cml.campaignid = UpdatedLead.campaign_id__c;
cml.leadid = UpdatedLead.id;
Database.upsert(cml, false) ;
}
}
}
// Logic: On update, if there is a change in the Event Activity Field & it is not Null, Create an Event
if(Trigger.IsUpdate){
for (Lead UpdatedLead2: Trigger.new) {
Lead PriorLead = Trigger.oldMap.get(UpdatedLead2.ID);
if (UpdatedLead2.campaign_id__c != PriorLead.campaign_id__c && UpdatedLead2.campaign_id__c!=null){
CampaignMember cml2 = new CampaignMember();
cml2.campaignid = UpdatedLead2.campaign_id__c;
cml2.leadid = UpdatedLead2.id;
Database.upsert(cml2, false);
}
}}}
Obviously
Also always bulkify db access.
All Answers
Also always bulkify db access.
Also--and I apologize for this, my old test class had 100% coverage. Changing over I drop down significantly. How does one adapt a test class to be better for bulkification? this is my current test class (was 100%).
Figured out part of it. I havnt used prefix before and since I was comparing against an id I simply changed this and the code is now firing
String prefix = '701';
I'm a bit lost on the test class though; went from 100% to 51% -.-
String prefix = Campaign.sObjectType.getDescribe().keyPrefix;
Still a tad lost on the test class. Trying to look for other bulkified test class examples but not noticing what I might be missing.
Move Database.upsert(...) outside the for loop.
Do NOT do db access in loop. Queue them up and hit db only one shot and that is a good 'bulkify' example.