• Kruse Collins
  • NEWBIE
  • 10 Points
  • Member since 2017
  • Salesforce Admin/Jr. Dev
  • Kruse in Salesforce


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies

I'm creating a custom Javascript button in SFDC classic. This button would rest on a Quote record. The ideal functionality is that this button would check and see if this particular Quote record has any child records associated with it.

  • The master-detail relationship is Quote (master) to QService__c (child).
  • The master-detail field name on the QService__c object is called Quote__c.
  • The child relationship name is QServices.

I want to try and handle any errors, so if the Quote has 1 or more QService__c records, then it would open a link in a new window (google is the test link I have so far). If the Quote doesn't have any QService__c records, then display an alert.
 

{!RequireScript("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 

var childRecords = sforce.connection.query("SELECT id, (SELECT id FROM QServices__r) FROM Quote WHERE Id = '{!Quote.Id}'"); 

if(childRecords.getArray("records").length) { 
       window.open("https://www.google.com/"); 
} else { 
       alert("There needs to be a service on this quote."); 
}​​
 

The Issue:
What's happening right now is when I DON'T have a QService__c record on the quote, still redirects to the link. So the If-statement or the Query isn't working correctly. Any suggestions on what needs to be fixed here? If it's the query or if I need to adjust my If-statement?

I'm creating a custom Javascript button in SFDC classic. This button would rest on a Quote record. The ideal functionality is that this button would check and see if this particular Quote record has any child records associated with it.

  • The master-detail relationship is Quote (master) to QService__c (child).
  • The master-detail field name on the QService__c object is called Quote__c.
  • The child relationship name is QServices.

I want to try and handle any errors, so if the Quote has 1 or more QService__c records, then it would open a link in a new window (google is the test link I have so far). If the Quote doesn't have any QService__c records, then display an alert.
 

{!RequireScript("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")} 

var childRecords = sforce.connection.query("SELECT id, (SELECT id FROM QServices__r) FROM Quote WHERE Id = '{!Quote.Id}'"); 

if(childRecords.getArray("records").length) { 
       window.open("https://www.google.com/"); 
} else { 
       alert("There needs to be a service on this quote."); 
}​​
 

The Issue:
What's happening right now is when I DON'T have a QService__c record on the quote, still redirects to the link. So the If-statement or the Query isn't working correctly. Any suggestions on what needs to be fixed here? If it's the query or if I need to adjust my If-statement?