I'm creating a javascript button on the Quote object that would redirect to a link in another window. I only want to redirect to this other window, if there's a child under the record. For this example: a quote (master) can have multiple services (detail). The object name for services is QService__c.

I want to be able to do an if statement and see if Quote__c (name of the master-detail relationship field on the QService__c object) is not null. As well, I want to pass into the URL the id for the Quote record. Calling the SObject.id - is that the best way to pass that through? Here is what I have so far.

var qt = new sforce.SObject("QService__c");
qt.id = "{!QService__c.id}";

if({!QService__c.Quote__c} != null) {
//Open up survey link in new window
else {
//Catch error
alert("There needs to be a service on this quote."); }

2 Answers 2


You cannot simply reference child objects with a Javascript button. You will need to do a SOQL query to see if they exist. You can do a SOQL query from within a JS button.

var childRecords = sforce.connection.query("SELECT Id from Quote__c where QService__c ='{!QService__c.Id}'");

Then, you can check to see if you got any record back:

if(childRecords.getArray("records").length) {
} else {
    alert("There needs to be a service on this quote."); 

Edit: Here is a link to an example of what I'm suggesting on the developer.salesforce forums.


  • 2
    Good answer, but I went ahead and solidified it a bit. You were definitely on the right track.
    – sfdcfox
    Commented Apr 2, 2018 at 23:44
  • @sfdcfox Thanks for doing that! I was hoping someone would be able to provide more concrete information.
    – Genko
    Commented Apr 3, 2018 at 16:02
  • @sfdcfox The only error I'm getting is "Error: Field Quote__c.id does not exist. Check Spelling". This is happening in the SOQL query where seeing if Quote__c is equal to {!Quote__c.Id}. I thought this error might be because I didn't create a new SObject of type QService__c but even after creating that SObject, the error still persists. This button is on the Quote object, can the SOQL query reference to another object? Commented Apr 3, 2018 at 16:17
  • @KruseinSalesforce Looks like I had the object names mixed up a bit. Try the edited version. Note that we don't know what the relationship field on the Quote object is called, so you will need to consult your configuration to figure out the correct field names.
    – sfdcfox
    Commented Apr 3, 2018 at 16:24
  • @sfdcfox I think it's the other way around. The relationship field is on the QService__c object, named Quote__c (see OP's second paragraph). Shouldn't the SOQL be SELECT Id FROM QService__c WHERE Quote__c = {!Quote.Id} or something along those lines?
    – Genko
    Commented Apr 3, 2018 at 16:46

So I ended up doing a completely different solution than what was suggested here (even though I believe these could've done the trick). What I ended up doing was:

  1. Fixing my query. I was doing that all wrong. I needed to do a Child-to-parent query.
  2. Use .size and check against if the size of the array was 0.

Here's the code:


var quoteId = "{!Quote.Id}"; 

alert("This is the quote Id - " + quoteId); 

var childRecords = sforce.connection.query("SELECT id, Name, Quote__r.Id FROM QService__c WHERE Quote__r.id ='"+ quoteId +"'"); 

alert("The # of child records is/are: " + childRecords.size); 

if(childRecords.size==0) { 
alert("There needs to be a service on this quote."); 
} else { 

I hope this helps others, and thanks for everyone who helped on this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .