3

I have a Javascript button on Case that merely changes the owner when clicked, so far so good. But I'd like to extend it with a tiny bit of validation that chck to see whether there's a picklist value that is empty (that is, not set) on the Case. If so, it throws an alert, else updates the case with the new ownerId. Here's what I've got:

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


var updateRecord = new Array(); 
var myquery = "SELECT Id,txFaultRequestInfo__c,pl_ReasonForAssignment__c FROM Case WHERE Id = '{!Case.Id}' limit 1"; 

result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

if(records[0]) 
{ 
var update_Case = records[0]; 

if("{update_Case.pl_ReasonForAssignment__c}" == '') 
{ 
alert("Picklist value is not set!"); 

} 
else 
{ 
update_Case.OwnerId = "00G20000000jeqC" ;  /* New Value */
//update_Case.OwnerId = "00G20000000jdLD"; /* Older Value */

updateRecord.push(update_Case); 
} 
} 

result = sforce.connection.update(updateRecord); 
parent.location.href = parent.location.href;

No matter what, it always evaluates the if condition as false and goes to the else condition that updates the OwnerId. Can you spot the error?

  • 2
    One thing is this "{update_Case should be this "{!update_Case. But if that field is editable in the page, then perhaps you should be looking at the input field value not the value when the page was rendered. – Keith C Apr 29 '14 at 23:17
  • If I add the !,I get an error: "Error:update_Case.pl_ReasonForAssignment__c does not exist. Check spelling". But it does exist, I can query for it and it is the correct API name. What I'm doing with the comparison is simply checking whether that field is empty or not. If there exists a simpler way to to do this i'm all for it. – akarnid Apr 29 '14 at 23:32
4

update_Case is a variable at the point where you're referencing it in the if condition so you don't need the quotes nor the merge syntax.

A couple of other points, you should always declare JavaScript variables with the var keyword and I would suggest using the triple-equals operator in the if expression to avoid type coercion:
if (update_Case.pl_ReasonForAssignment__c === '') {

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

var updateRecord = new Array();
var myquery = "SELECT Id, txFaultRequestInfo__c, pl_ReasonForAssignment__c FROM Case WHERE Id = '{!Case.Id}' limit 1";

var result = sforce.connection.query(myquery);
var records = result.getArray("records");

if (records[0]) {
    var update_Case = records[0];

    // a little debugging
    alert(update_Case.pl_ReasonForAssignment__c);

    if (update_Case.pl_ReasonForAssignment__c == '') {
        alert("Picklist value is not set!");

    } else {
        update_Case.OwnerId = "00G20000000jeqC"; /* New Value */
        //update_Case.OwnerId = "00G20000000jdLD"; /* Older Value */

        updateRecord.push(update_Case);
    }
}

result = sforce.connection.update(updateRecord);
parent.location.href = parent.location.href;
| improve this answer | |
  • Thanks for the pointers, especially on the triple comparison operator, had forgotten about that. But I'm still not getting this to work, I have in my test case this picklist (pl_ReasonForAssignment__c) being set to None (ie empty but the comparison operator is not picking up on that and skips straight to the else. – akarnid Apr 30 '14 at 12:44
  • So what is the value in that field? What do you get when you alert on that field? [null, undefined, empty string, etc] If that isn't providing any data, what do you get when you alert on the entire update_Case object? – Mark Pond Apr 30 '14 at 16:18
  • I fixed this. The value when debugged was null, so my comparison for this is is (update_Case.pl_ReasonForAssignment__c == null) . That works like like I want. I'll mark your answer as the correct one - Mark. – akarnid May 1 '14 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.