I have a custom button “Submit Order” which simple change Opportunity Stage.
But i want to add some validation check before changing Stage
Here is my apex class
global class SubmitOrder {
public SubmitOrder(ApexPages.StandardController controller)
{
}
Webservice static string Submit(string oppId)
{
string message = '';
try
{
List<Opportunity> listOpp = new List<Opportunity>();
Opportunity oppNew = [SELECT id, StageName, Destination_Zone__c, Sold__c FROM Opportunity WHERE Id = :oppId];
boolean flag = true;
if(oppNew.StageName!= 'To Be Searched' && oppNew.StageName != 'Search')
{
oppNew.StageName.addError('Stage should be \'To Be Searched\' or \'Presentation\' or \'Search');
flag = false;
}
if( oppNew.Destination_Zone__c == '')
{
oppNew.Destination_Zone__c.addError('Destination Zone is required');
flag = false;
}
if(oppNew.Sold__c < 0)
{
oppNew.Sold__c.addError('Sold is required');
flag = false;
}
if(flag)
{
oppNew.StageName = 'Ticketing';
listOpp.add(oppNew);
if (listOpp != null && !listOpp .isEmpty())
{
Database.update(listOpp);
}
message ='Saved Successfully';
}
catch(System.CalloutException e)
{
message = e.getMessage();
}
return message;
}
}
And call for custom button is
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var oppId= "{!Opportunity.Id}";
var result = sforce.apex.execute("SubmitOrder","Submit",{oppId:oppId });
but getting following error
SObject row does not allow errors
Any help would be appreciated
Thanks in advance
The problem is that you are taking the ID and passing that to a method which retrieves the OLD row with that ID that is already in the database, and trying to use addError() on that. It won’t work because addError() will not work on rows that are already in the database.
Without knowing your exact requirements I would make this suggestion – put validation rules on the Opportunity object. Then you can code your button to just simply update the record. The validation rules will take care of the rest.