I've just received a developer script exception email for a trigger I've got in a production environment:
Apex script unhandled trigger exception by user/organization: 00590000002GfMD/00D90000000cIze
SetContactDonorCampaign: execution of BeforeInsert
caused by: System.DmlException: Update failed. First exception on row 0; first error:
MISSING_ARGUMENT, Id not specified in an update call: []
Trigger.SetContactDonorCampaign: line 25, column 1
The error doesn't give me any id for the record it failed on, which is a bit annoying. The trigger itself just recognises when a Payment_Information__c record is created with the lookup Contact__c populated, and sets a multi-select picklist on that Contact to include "Donor" if it doesn't already.
trigger SetContactDonorCampaign on Payment_Information__c (before insert,before update) {
for(Payment_Information__c donation : Trigger.new)
{
Contact reg = new Contact();
if(donation.Contact__c != NULL)
{
reg = [SELECT id, Campaign__c FROM Contact WHERE Id = :donation.Contact__c];
String regCampaign = '';
if (reg.Campaign__c != NULL)
{
if (!reg.Campaign__c.contains('Donor'))
{
regCampaign = reg.Campaign__c + ';Donor';
} else {
return;
}
} else {
regCampaign = 'Donor';
}
reg.Campaign__c = regCampaign;
}
if (reg != NULL)
{
update reg;
}
}
}
Without a specific record to look up (the system doesn't even have a Payment Information record with modified date matching this error), any suggestions on what's failing here?
Contact object is not null even donation.Contact__c is null. because you created new contact object each time.
Contact reg = new Contact();
if (reg != NULL)
{
update reg;
}
above condition is always executed. That's way this error is occur.
Please change your Code to bellow.
trigger SetContactDonorCampaign on Payment_Information__c (before insert,before update) {
for(Payment_Information__c donation : Trigger.new)
{
Contact reg = null;
if(donation.Contact__c != NULL)
{
reg = [SELECT id, Campaign__c FROM Contact WHERE Id = :donation.Contact__c];
String regCampaign = '';
if (reg.Campaign__c != NULL)
{
if (!reg.Campaign__c.contains('Donor'))
{
regCampaign = reg.Campaign__c + ';Donor';
} else {
return;
}
} else {
regCampaign = 'Donor';
}
reg.Campaign__c = regCampaign;
}
if (reg != NULL)
{
update reg;
}
}
}