0

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.

{!REQUIRESCRIPT("/SOAP/ajax/36.0/connection.js")}
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
window.open("https://services.qualtrics.com/jfe/form/SV_9o7r0gk556N0bGZ/?={!QService__c.id});} 
else {
//Catch error
alert("There needs to be a service on this quote."); }

2 Answers 2

3

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.

{!RequireScript("/soap/ajax/42.0/connection.js")}
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) {
   window.open("https://services.qualtrics.com/jfe/form/SV_9o7r0gk556N0bGZ/?={!QService__c.id}");
} 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.

https://developer.salesforce.com/forums/?id=906F0000000BPbaIAG

6
  • 2
    Good answer, but I went ahead and solidified it a bit. You were definitely on the right track.
    – sfdcfox
    Apr 2, 2018 at 23:44
  • @sfdcfox Thanks for doing that! I was hoping someone would be able to provide more concrete information.
    – Genko
    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? 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
    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
    Apr 3, 2018 at 16:46
0

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:

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

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 { 
window.open("http://www.krusecollins.com/") 
}

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 .