[SalesForce] Display an error message on standard page

I have a class which i called from a custom button to send the record for approval. Before sending the record for approval I need to validate the record values and if any error is there I want to display it on standard page of the record. For this I have sobjectname.addError(). But is throwing a error :-

System.FinalException: SObject row does not allow errors

My code :-

global class SpecialPricingApproved_Controller{

webService static string SpecialPricingApproved_Request(ID oppid){

    List<opportunity> oppList = new List<opportunity>();

    for(Opportunity oppResult : [SELECT OwnerId, Manager_of_Manager__c, Max_Discount__c FROM opportunity WHERE ID = :oppid]){
        if(oppResult.Max_Discount__c == 10.00 || oppResult.Max_Discount__c == null){

            Opportunity oppError = new Opportunity();
            oppError = oppResult;
            oppError.addError('Max discount should be greater than 10 % for Approval');

            return null;
        }


        if(oppResult.Manager_of_Manager__c == null){
            oppList.add(oppResult);
            Opportunity_Helper.addManagerofManager(oppList, true);
        }
    }

    //Sending record for approval 
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitted for Approval');
    req1.setObjectId(oppid);
    Approval.ProcessResult res = Approval.Process(req1);   

    return null;

}
}

My 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.SpecialPricingApproved_Controller.SpecialPricingApproved_Request:
line 22, column 1', }

Line no 22 is :

oppError.addError('Max discount should be greater than 10 % for Approval');

Can anyone help me to solve this error.

Best Answer

You need to do 2 things.

1) Return the result back to your javascript button.

var result = myJSWebcallhere();
if (result != '')
  alert(result);

2) catch the errors in your service and return them as the result.

webService static string SpecialPricingApproved_Request(ID oppid)
{
  try
  {
    List<opportunity> oppList = new List<opportunity>();
    Opportunity errorHandler = new Opportunity();

    oppList = [SELECT OwnerId, Manager_of_Manager__c, Max_Discount__c FROM opportunity WHERE ID = :oppid];      

    if(oppList.size() > 0){
      if(oppList[0].Max_Discount__c == 10.00 || oppList[0].Max_Discount__c == null){
        return 'Max discount should be greater than 10 % for Approval';
      }

      if(oppList[0].Manager_of_Manager__c == null){
        Opportunity_Helper.addManagerofManager(oppList, true);
      }
    }

    //Sending record for approval 
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitted for Approval');
    req1.setObjectId(oppid);
    Approval.ProcessResult res = Approval.Process(req1);
  }
  catch (Exception e)
  {
    return 'Error = ' + e.getMessage());
  }
  return '';
}
Related Topic