0

I've been trying to debug this piece of onClick JS in a detail page button, as users where having problems. I saw a few syntax issues (such as no enclosing "" around the merged fields) and I think I've corrected the code now. But I'm getting somewhat confused as to when to use double as opposed to single quotes, and how that applies to onClick JS buttons. Is my understanding correct based on the implemented code below? Thanks in advance.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}
{!REQUIRESCRIPT("/support/console/41.0/integration.js")}

var invId = "{!kugo2p__KugamonInvoice__c.Id}";

if (invId != '') {

    var status = "{!kugo2p__KugamonInvoice__c.RecordStatus__c}";
    var shipmentNumber = "{!kugo2p__KugamonInvoice__c.Shipment__c}";
    var noLetter = "{!kugo2p__KugamonInvoice__c.No_Letter__c}";
    var portLetter = "{!kugo2p__KugamonInvoice__c.Port_Letter__c}";
    var splitLetter = "{!kugo2p__KugamonInvoice__c.Split_Letter__c}";
    var transferLetter = "{!kugo2p__KugamonInvoice__c.Transfer_Letter__c}";

    var myquery = "SELECT Parent_Case_Record_Type__c, OrderType__c FROM kugo2p__SalesOrder__c WHERE Id = '{!kugo2p__KugamonInvoice__c.OrderId__c}'";
    result = sforce.connection.query(myquery); 
    records = result.getArray("records"); 

    var orderRec = records[0];
    var oPCaseRT = orderRec.Parent_Case_Record_Type__c;
    var orderType = orderRec.OrderType__c;

    var invQuery = "SELECT RecordStatus__c FROM kugo2p__KugamonInvoice__c WHERE Id = '{!kugo2p__KugamonInvoice__c.Id}'";
    result = sforce.connection.query(invQuery); 
    records = result.getArray("records"); 

    var invRecRef = records[0];
    var invRecRefStatus = invRecRef.RecordStatus__c;

    if (status != 'New') {
        alert('{!$Label.Error_Record_Status_Not_New}');
    } else if(shipmentNumber !='' && orderType == 'Standard' && (oPCaseRT != 'TBS' || oPCaseRT != 'VES Customer Support' ) && (!noLetter && !portLetter && !splitLetter && !transferLetter)) {
        alert('{!$Label.Error_Support_Letter_Option}');
    } else if(invRecRefStatus != 'New') {
        alert('{!$Label.Error_Support_Multi_Clicks}');
    } else {
        var emailInv = "{!kugo2p__KugamonInvoice__c.Email_Invoice__c}"; 
        if(emailInv == false) { 
            var oInvoice = new sforce.SObject("kugo2p__KugamonInvoice__c"); 
            oInvoice.Id = invId ; 
            oInvoice.kugo2p__RecordStatus__c = "Sent";

            var InvoiceResult = sforce.connection.update([oInvoice]); 
            if(!InvoiceResult[0].getBoolean("success")) { 
                alert('Not able to update invoice record status to Submitted! Please check with a system administrator.\n\nError: ' + InvoiceResult[0].errors.message); 
            } else { 
                if (sforce.console.isInConsole()) { 
                    srcSelf("/" + invId); 
                } else { 
                    window.top.location.href = '/' + invId; 
                } 
            } 
        } else {
            if ('{!JSENCODE(kugo2p__KugamonInvoice__c.Hidden_Email_To_Send__c)}' == '') {
alert('Email to send is not found. Please provide override email or contact billing email.');
            } else {
                if (sforce.console.isInConsole()) {
                    sforce.console.openPrimaryTab(null, '/apex/SubmitInvoiceVF?id={!kugo2p__KugamonInvoice__c.Id}&email={!JSENCODE(kugo2p__KugamonInvoice__c.Hidden_Email_To_Send__c)}&submit=true', true, 'Submit Invoice');
                    var closePrevioustab = function closePrevioustab(result) {
                        var tabId = result.id;
                        sforce.console.closeTab(tabId);
                    };
                  sforce.console.getEnclosingPrimaryTabId(closePrevioustab);
                } else {
                    window.top.location.href = '/apex/SubmitInvoiceVF?id={!kugo2p__KugamonInvoice__c.Id}&email={!JSENCODE(kugo2p__KugamonInvoice__c.Hidden_Email_To_Send__c)}&submit=true'; 
                }
            }
        }
    }
}
  • 1
    Any value enclosed in quotes (single or double) represents a string in JavaScript. You can use any approach to construct your string values, however its always recommended that for consistency and readability, you stick to one of the representations. – Jayant Das Aug 22 '18 at 23:11
  • Thanks for the quick reply. Does that mean then that the merged formula for variables noLetter, porLetter, etc. should not be enclosed because they are actually boolean? – Francis A. Aug 22 '18 at 23:35
1

In JavaScript both '...' and "..." are strings. If you use ', then you don't need to escape ", and if you use ", you don't need to escape '. In any case, you should always use JSENCODE for any fields that might contain non-alphanumeric characters (note: good job, looks like you did!). And you should only use quotes for strings; do not use them for numbers or Boolean values. Some other types, like Date fields, require special attention as well.

One such example is here:

    var emailInv = "{!kugo2p__KugamonInvoice__c.Email_Invoice__c}"; 
    if(emailInv == false) { 

Ironically, this could evaluate to:

"false" == false

Since the String "false" is not a false value, this actually evaluates to false instead of the expected true.

For Boolean values, you can simply:

var emailInv = {!kugo2p__KugamonInvoice__c.Email_Invoice__c};

If for some reason you happen to need a Date, you would typically:

var createdDate = new Date("{!someObj.dateField}");

Overall, other than the misuse of quote characters for Boolean values, your code is mostly solved.

  • Thanks, I did realise the same when testing for the boolean fields. The date is new to me and will be useful in the future. Cheers! – Francis A. Aug 23 '18 at 5:07

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.