• #15032
    Salesforce | PRANAV PRANAV #15
    Forcetalks

    How to add error message in a Salesforce Apex Trigger?

    Hi All,

    How to add error message in apex trigger ?

    Thanks

    Tagged: , , ,

    #15039
    Salesforce | Abhinav Abhinav #399
    Forcetalks

    Hi Pranav,

    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’);
    }

    #21038
    Salesforce | shariq shariq #1
    Forcetalks

    Hi Pranav,

    Yes you can add error messages in triggers by using Salesforce trigger addError method.
    For Example – Sobject.addError(‘Error Messages’);

    • This reply was modified 1 year, 2 months ago by Salesforce | shariq shariq.
    #27405
     Ratnakar #194
    Forcetalks

    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,’Enter Your error message here’));

    #27407
    Salesforce | PRANAV PRANAV #15
    Forcetalks

    Hi Ratnakar,

    Whatever you are saying is not correct.

    The solution given by Abhinav is correct and helpful. And the issue resolves previously.

    I appreciate your response if it works for the same but the reply provided by you works for VF page only.

    Thanks

    #35848
    Salesforce | shariq shariq #1
    Forcetalks

    Hi,

    Try this –

    obj.addError(‘error’);

    Hope this helps.

    #35859
    Salesforce | Parul Parul #2
    Forcetalks

    Adding some points:

    If you want to print error message on particular field . you can use the following syntax
    FieldName.addError(‘Write error message’);
    Display error message on visual force page include  <apex:pageMessages /> tag in vf page

    Thanks

    #35909
    Salesforce | shariq shariq #1
    Forcetalks

    Hi,

    This is the code snippet –

    trigger AvoidDuplicateAccounts on Account (before insert, before delete)
    {
    Set<String> setAccountName = new Set<String>();
    for(Account objAccount: [Select Name from Account])
    setAccountName.add(objAccount.Name);
    for(Account objAccount: Trigger.new)
    {
    if(!setAccountName.contains(objAccount.Name))
    {
    setAccountName.add(objAccount.Name);
    }
    else
    {
    objAccount.Name.addError(‘Account with same name Exists’);
    }
    }
    }

    Hope this helps

Viewing 8 posts - 1 through 8 (of 8 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?