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
TimSTimS 

Help with javascript button code to check field for empty value

Using a custom button as written below to set value of a custom object (Request__c) picklist field (Status__c) to "Rejected." Need to modify so it validates any value has been entered in another picklist field (Reject_Reason__c). If no value is selected in Reject_Reason__c, then alert needs to appear instructing user to select a value and status field is NOT updated to "Rejected," as Status__c should only be set to "Rejected" if any value has been selected in Reject_Reason__c
 
{!requireScript("/soap/ajax/26.0/connection.js")} 
var request = new sforce.SObject("Request__c"); 
request.id = "{!Request__c.Id}" 
request.Status__c = 'Rejected'; 
sforce.connection.update([request]); 
window.location.reload(); //to reload the window and show the updated values
 
I've tried modifying but to no end as I am not vered in javascript at all. Anyone's assistance would be greatly appreciated!  Thanks!
Best Answer chosen by James Loghry
James LoghryJames Loghry
Any particular reason you're using the sforce javascript library to do this instead of straight visualfore / apex?  Looks like this might be related to service console?

Here's one (of a few ways) to do what you're asking:
 
{!requireScript("/soap/ajax/26.0/connection.js")} 
var reason = "{!Request.Reject_Reason__c}";
var request = new sforce.SObject("Request__c"); 
request.id = "{!Request__c.Id}";
if(typeof reason === "undefined" || reason === ""){
    alert("{!$Label.PLEASE_SELECT_VALE}");
}else{
    request.Status__c = 'Rejected'; 
    sforce.connection.update([request]); 
    window.location.reload(); //to reload the window and show the updated values
}

 

All Answers

James LoghryJames Loghry
Any particular reason you're using the sforce javascript library to do this instead of straight visualfore / apex?  Looks like this might be related to service console?

Here's one (of a few ways) to do what you're asking:
 
{!requireScript("/soap/ajax/26.0/connection.js")} 
var reason = "{!Request.Reject_Reason__c}";
var request = new sforce.SObject("Request__c"); 
request.id = "{!Request__c.Id}";
if(typeof reason === "undefined" || reason === ""){
    alert("{!$Label.PLEASE_SELECT_VALE}");
}else{
    request.Status__c = 'Rejected'; 
    sforce.connection.update([request]); 
    window.location.reload(); //to reload the window and show the updated values
}

 
This was selected as the best answer
TimSTimS
Thanks James...with a little tweaking this worked (had to change to full API name Request__c in reason variable). The consultant who designed our service cloud deployment set this up...there are a couple of other fields required via the page layout to save a record and this button allowed them to set status to Rejected without completing those fields since they were not necessary for rejected request records. But now those required fields are preventing a Reject Reason value from being selected and saved, so will be looking at removing requirement on page layout and replacing with validation rules that prevent saving only if Reject Reason is not entered. Hoping that will let me maintain the requirement for those two other fields when they are needed but allow adding a reject reason.  Thanks again.