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
Michael Clarke 36Michael Clarke 36 

Variable does not exist: addError

Hi, I am new to Apex

I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?

trigger ContactBeforeDelete on Contact (before delete)
{
    for(Contact c:trigger.old)
    {
        if(c.accountId==null)
        {
            c.addError=('You are not authorised to delete this contact.');
        }
    }
}
Best Answer chosen by Michael Clarke 36
ANUTEJANUTEJ (Salesforce Developers) 
Hi Michael,

Make sure the api names of the field you are using is correct.

Looking forward to hearing back from you.

Regards,
Anutej

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Michael,

Make sure the api names of the field you are using is correct.

Looking forward to hearing back from you.

Regards,
Anutej
This was selected as the best answer
Janet EpebinuJanet Epebinu
Hi Michael,

You can use this code instead
trigger ContactBeforeDelete on Contact (before delete)

    List<Contact> conList = [select Id from Contact where Id in :Trigger.Old];
    for(Contact c : conList)    {
       trigger.oldMap.get(c.Id).addError('You are not authorised to delete this contact.'); 
                      
        }          
}
Kindly let me know if it helps you.
 
Michael Clarke 36Michael Clarke 36
c.addError=('You are not authorised to delete this contact.');
should read
c.addError('You are not authorised to delete this contact.');