+ Start a Discussion
Muthu_tMuthu_t 

How to add error message on apex standard page?

Hi,

 

I am trying to add error message by using adderror method. But it is throwing an error like this.

 

Error:

A problem with the OnClick JavaScript for this button or link was encountered:
{faultcode:'soapenv:Client', faultstring:'System.FinalException: SObject row does not allow errors
Class.InTheMarketOpportunity.marketOpportunity: line 8, column 9External entry point', }

 

Code:

 

Am executing this code.

 

    global class InTheMarketOpportunity{    webservice static void marketOpportunity(String accountid,String solicitid,String state,String city){    Account a=new Account();         list<Opportunity> marketAccount= new List<Opportunity>();        marketAccount=[select Id,AccountId from Opportunity where In_The_Market__c =: accountId];        if(marketAccount.size()>0){        In_The_Market__c acc=[select Id from In_The_Market__c where Id =: accountId];        acc.addError('Already created');        }    system.debug(':::::HERE::::::'+accountId);
        }}

 

Shashikant SharmaShashikant Sharma

You are geeting this error as you are using addError with a SOQL result object record you have two options here

 

1) you can use

 

new Account().addError('Already created');  

 or

2) you can also  use below instead of using add Error

 

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Already created');
ApexPages.addMessages(myMsg);

 


 

Rahul SharmaRahul Sharma

btw, you are not using any page here. so you'll not able to display page message.

one thing can be done here, displaying an alert message.

Could you post you onclick JS code, and also the class for furthur assistance.