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
toby curtis 6toby curtis 6 

How to add error message in apex trigger ?

Test__c trigObj = trigger..get(Test.id);
if(trigObj = null){
trigObj.addError('Cloned record should not have the same period as that of Parent record');
}

I am using this code not getting the output as excepted.
Best Answer chosen by toby curtis 6
RituSharmaRituSharma
Change below line:
if(trigObj = null)

To:
if(trigObj == null)

All Answers

AbhishekAbhishek (Salesforce Developers) 
Test__c trigObj = trigger.newMap.get(Test.id);
if(trigObj != null){
trigObj.addError('Cloned record should not have the same period as that of Parent record');
}

Try this.


You can add error messages in triggers by using Salesforce trigger addError method.
For Example - Sobject.addError('Error Messages');


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
RituSharmaRituSharma
Change below line:
if(trigObj = null)

To:
if(trigObj == null)
This was selected as the best answer