0

I'm trying to query for relatedchild records on Order and throw an alert message on cancel custom button but getting an error of didn't understand relationship.

    {!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
    {!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

     var relatedchildorders = sforce.connection.query("SELECT Id, (Select Id from Related_Child__r) from Order where Id = '{!Order.Id}'"); 
     var records = relatedchildorders.getArray("records"); 

if(records != null && records[0] != null){ 
     alert("You Cannot Cancel Order"); 
     }

Verified the relationship in workbench and able to query records in developer console. Does Javascript buttons support Inner Queries? If not, what would be the effective way to handle this requirement?

Also, trying to avoid an extra work of updating the child records count on parent and validating against that field.

1

I just run this code and it works fine for me so may be you are using wrong API name.

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var relatedchildorders = sforce.connection.query("SELECT Id, (Select Id from Contacts) from Account where Id = '{!Account.Id}'"); 
var records = relatedchildorders.getArray("records"); 

if(records != null && records[0] != null){ 
     alert("You Cannot Cancel Order"); 
}

As I can see you are not using nested query anywhere in your code so you might want to check if child exists then don't allow to delete. So you can directly make query on child based on parentID and check its size as well.

var relatedchildorders = sforce.connection.query("SELECT Id from COntact where AccountId = '{!Account.Id}'"); 
var records = relatedchildorders.getArray("records"); 

if(records != null && records[0] != null){ 
     alert("You Cannot Cancel Order"); 
}
0

I think you should use Related_Childs__r. Most of the time relationship name in plural for the Inner Queries. Or see the below image using that you can find the child relationship name

enter image description here

So Updated Query will be:

var relatedchildorders = sforce.connection.query("SELECT Id, (Select Id from Related_Childs__r) from Order where Id = '{!Order.Id}'");

Referance : http://www.sfdc99.com/2013/06/24/example-how-to-write-a-cross-object-soql-query-part-2/

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.