Javascript snippet button not working

  • 1
  • Problem
  • Updated 7 months ago
  • In Progress
I have a button in a Skuid Opportunity overlay that is not working. When pressed, nothing happens at all, not even an error message. Any idea why this might be happening? I and my team are stuck! 
 
Button Type: Run Skuid Snippet
Snippet Properties: 
Resource location: In-Line (Snippet)
Snippet Body
var $ = skuid.$;
var opp = skuid.model.getModel("Opportunity").getFirstRow();
var o = new sforce.SObject("Opportunity");
o.Id = opp.Id;
var contactId = sforce.connection.query("SELECT ID FROM Contact WHERE Name LIKE '%" + opp.Installer_Salesperson_Text__c + "%' AND AccountId  = '" + opp.AccountId + "'");
if (contactId.getArray("records")[0] === null || contactId.getArray("records")[0] === '' || contactId.getArray("records")[0] === undefined) { 
    alert('We were not able to find a contact record for the Salesperson contact on this opportunity. To login as user, please go to their contact page.');
} else {
    var result = sforce.apex.execute('GhostingUtil','login',{partnerConId:contactId.getArray("records")[0].Id});
    if (result.includes('ERROR: ')) {
        alert(result);
    } else {
        window.open(result,'_blank');
    }
}
Photo of Eugene Rinehart

Eugene Rinehart

  • 162 Points 100 badge 2x thumb
  • Confused

Posted 7 months ago

  • 1
Photo of Mike Dwyer

Mike Dwyer, Champion

  • 4,622 Points 4k badge 2x thumb
Thanks for showing useful coding approaches I haven't used before! Gives me a new tool to try.

To debug, you could put alerts after all (or most) statements and see if and when the script dies; I prefer console.log() for this.

But my guess is that when the query returns nothing, contactId doesn't get a getArray() object, and the if statement dies. Wrap the if with an outer if ( contactId.getArray() ) or maybe if ( contactId.getArray().size ) or similar construct.  (Would that be .length instead of .size?)

Photo of Eugene Rinehart

Eugene Rinehart

  • 162 Points 100 badge 2x thumb
Hi Mike, I appreciate your response! I had my team review your suggestion, and the sticking point is that this is older code that's worked properly in the past and we haven't made any recent updates. We're wondering whether Skuid's changed anything on their end where our code might need to be adjusted to accomodate.