function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChubbyChubby 

fetch child records in custom button javascript written on parent

Hi All,

Can we write SOQL inside button scripting to fetch child records. I tried something like below.
var reocrds = sforce.connection.query(here parent to child query) but it doesn't seem to be working.
Please help me know how can i do this.
 Thanks.
 
sagar jogisagar jogi
Can you please try this code? 

var dataCheck = sforce.connection.query(here parent to child query);
var reocrds = dataCheck.getArray("records");
 
ChubbyChubby
Hi Sagar,
I tried below one and receiving error could not identify releationship. Please suggest.

var Products = sforce.connection.query("SELECT id, (SELECT Product_Name__c from Product_Selected__r WHERE Product_Name__c = 'Test') from Opportunity__c");
records = Products.getArray("records");

Here the requirement is if product selected with name =Test found then i need to give some alert message. I have th custom button on parent object.
Parent-> Opportunity__c
Child-> Product_Selected__c


Thanks.
sagar jogisagar jogi
Hi

Please click on lookup field on your object Product_Selected__c and then check your child relationship name
Please follow below step :-
1) open Product_Selected__c object
2) Then search for lookup field of opportunity.
3) Copy child relationship name and use that in your SOQL.

var Products = sforce.connection.query("SELECT id, (SELECT Product_Name__c from Product_Selected__r WHERE Product_Name__c = 'Test') from Opportunity__c");
records = Products.getArray("records");

(instead of Product_Selected__r use your child relationship name . For example - 'select id,( SELECT Name from Contacts) from account')

User-added image

​​​​​​​I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Sagar Jogi
 
ChubbyChubby
Hi Sagar,
I tried the same, since it is custom relationship i appended __r for the relationship name. Still i am receiving the below message when i click on button.

"Did not understand relationship 'Product_Selected' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names".

Any other suggestions?
Thanks.
sagar jogisagar jogi
can you please share the image of your field, as how I did in my above comment?
 
ChubbyChubby
Please see below.
User-added image
sagar jogisagar jogi
Is this object is part of managed package? if yes than can you add namespace of managed package.If you managed package namespace is 'test' then you need to use test__Product_Selected__r

 
ChubbyChubby
This is custom object not part of any managed package.
sagar jogisagar jogi
The reason why I am asking you because you have highlighted prefixes in black color for 'Child Relationship Name'. So you can add that also in your query.
ChubbyChubby
Yeah that is Org name space which I can not disclose so I striked off. I have added complete relationship name in the query.