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
John BraunJohn Braun 

Need help with a custom Javascript button!!!!

Hey everyone,

 

I'm attempting to create a custom submit for approval javascript button that will make sure that certain conditions are met before entering into an approval process. 

 

The problem I'm running into with my button is that it will keep running down the different conditions and still attempts to enter the approval process.

 

I would like the process to halt/stop when any of the conditions are met and an alert message is displayed. I would be SO thankful if anyone could help, thanks! My code is below:

 

 

if ( '{!Opportunity.Won_Lost_Comments__c}' == '') 

alert('In order to Submit an Opportunity for Approval you must populate the fields, Reason for Won/Lost and Won/Lost Comments AND the Opportunity Stage field should be at Closing. '); 


if ('{!Opportunity.Reason_for_Won_Lost__c}' == '') 
{alert('In order to Submit an Opportunity for Approval you must populate the fields, Reason for Won/Lost and Won/Lost Comments AND the Opportunity Stage field should be at Closing. '); 


if ('{!Opportunity.StageName}' == 'Closed/Won') 
{alert('This Opportunity has already been closed/decided'); 


if ('{!Opportunity.StageName}' == 'Closed/Lost') 
{alert('This Opportunity has already been closed/decided'); 


else 

navigateToUrl('/p/process/Submit?retURL=%2F{!Opportunity.Id}&id={!Opportunity.Id}','DETAIL','submit'); 
}

angelabregoangelabrego

Use "else if"  instead. 

 

if ( '{!Opportunity.Won_Lost_Comments__c}' == '')
{
alert('In order to Submit an Opportunity for Approval you must populate the fields, Reason for Won/Lost and Won/Lost Comments AND the Opportunity Stage field should be at Closing. ');
}

else if ('{!Opportunity.Reason_for_Won_Lost__c}' == '')
{alert('In order to Submit an Opportunity for Approval you must populate the fields, Reason for Won/Lost and Won/Lost Comments AND the Opportunity Stage field should be at Closing. ');
}

else if ('{!Opportunity.StageName}' == 'Closed/Won')
{alert('This Opportunity has already been closed/decided');
}

else if ('{!Opportunity.StageName}' == 'Closed/Lost')
{alert('This Opportunity has already been closed/decided');
}

else
{
navigateToUrl('/p/process/Submit?retURL=%2F{!Opportunity.Id}&id={!Opportunity.Id}','DETAIL','submit');
}