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
BrenzoBrenzo 

Updating Two Objects via OnClick Javascript Button with Combined Error Alert if Failed

I have a button on the Opportunity object that if clicked, first looks to ensure that a box on the account page is checked True, otherwise it fails and displays an error message. If the intial condition is met, it will the update fields on both the Opportunity and Account records.

I am struggling with getting the "if" logic to work at the end of my code to display the error(s) that might prevent this process for completing. This usually occurs as a result of validation rules on either the Opportunity or Account object.

Below is what I currently have:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}

//declare the variable that will hold all the information

var opp = new sforce.SObject("Opportunity");
var acct = new sforce.SObject("Account");
var ssn = '{!Account.Sent_Suspension_Notice__c}';

//Check to see if the Account has been marked as being sent a suspension notice.

if(ssn == '0')
{
alert("The account has not been marked as being sent a suspension notice (check box on Account page.) This must be done prior to moving forward with the suspension process.");
}
else{

//declare the variable that will hold all the information
opp.Id = '{!Opportunity.Id}';
opp.Service_Suspended_Cancelled__c = '1';
opp.StageName = 'Service Suspended';
opp.Suspension_Date__c = new Date();
opp.RecordTypeId ='012G0000001QILv';

acct.Id = '{!Opportunity.AccountId}';
acct.Service_Suspended__c == '1';

//save the change
var result1 = sforce.connection.update([opp]);
var result2 = sforce.connection.update([acct]);

if(and(((result1[0].getBoolean("success")),(result2[0].getBoolean("success"))) {
alert("You have successfully initiated the suspension / cancellation process. Press OK to continue. ");
} else {
alert('Error : '+result);
}

//refresh the page
window.location.reload();}

I'm trying to combine the two results and basically only display the "success" message if both objects were able to successfully update. If not, then I want to displa the error messages. 

Any help would be GREATLY appreciated!
Rahul SharmaRahul Sharma
You might want to write the save logic inside class method, with that Database.rollback() can be used in apex. Not sure if rollback's possible in Javascript code.
BrenzoBrenzo
Thanks Rahul. I'm not entirely sure if I'll be capable of doing that. 

What I was really looking for was to see if the IF logic could have an operator like AND incorporated so it would only display the success message if both conditions were met (the Opportunity AND the Account successfully updated) , else display the alert window with the error message(s) depending on the validation rule that is preventing the button from updating the records.