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
Christina GarciaChristina Garcia 

Custom Button to Update Stage on Oppty Isn't Working Correctly

I have a custom buttom, that is called "Create Sales Order". I want it to update to Processing unless one of the below else if statements is valid. The issue is, my error message displays but then still changes the Stage to Processing. Can you help me understand what I'm missing to prevent this from happening? 

Thanks!


{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")} 

try { 
var url = parent.location.href; 
var updateRecords = []; 
var update_Opportunity = new sforce.SObject("Opportunity"); 
update_Opportunity.Id ="{!Opportunity.Id}"; 
update_Opportunity.StageName = "Processing"; 
updateRecords.push(update_Opportunity); 

result = sforce.connection.update(updateRecords); 

if ("{!Opportunity.Sales_Order__c}" == true) { 

alert("Your request to create an ERP sales order has already been submitted."); 



else if ( !result[0].getBoolean("success") ) { 

var errors = result[0].errors; 
var errorMessages = errors.message; 
alert( errorMessages ); // display all validation errors 


else if ("{!Opportunity.Account_Order_Ready__c}" == false) { 

alert("The related account is not order ready. Your sales order request will not be processed."); 


else if ("{!Opportunity.StageName}" == 'Approved Prebook') { 

alert("This opportunity must be approved by your inside rep to proceed"); 



else { 
alert("Your request has been submitted") 

parent.location.href = url; 

} catch (e) { 
alert 
}
ra1ra1
Hi Christina,

As per your code, you are updating Opportunity first and then validating all conditions. Instead you should verify the conditions and then only update the code.

Below is sample code which might help you investigating more.
 
{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")} 

try { 
var url = parent.location.href; 

if ("{!Opportunity.Sales_Order__c}" == true) { 

alert("Your request to create an ERP sales order has already been submitted."); 

} 

else if ("{!Opportunity.Account_Order_Ready__c}" == false) { 

alert("The related account is not order ready. Your sales order request will not be processed."); 
} 

else if ("{!Opportunity.StageName}" == 'Approved Prebook') { 

alert("This opportunity must be approved by your inside rep to proceed"); 

} 
else  { 

var updateRecords = []; 
var update_Opportunity = new sforce.SObject("Opportunity"); 
update_Opportunity.Id ="{!Opportunity.Id}"; 
update_Opportunity.StageName = "Processing"; 
updateRecords.push(update_Opportunity); 

result = sforce.connection.update(updateRecords); 

var errors = result[0].errors; 
var errorMessages = errors.message; 
alert( errorMessages ); // display all validation errors 

} 

parent.location.href = url; 

} catch (e) { 
alert 
}

Thanks,
 
Christina GarciaChristina Garcia
Thank you!! That worked perfectly!