+ Start a Discussion
Scott BradyScott Brady 

How to access a related field in javascript button (Jscript Query?)

Good morning, everyone

I have the following issue.

I am trying to create a JS button that will update a few fields on the current object, as well as make a few updates on two related objects.  Here is their relation:

Recruiting Position
Personnel Management ---related to--> Recruiting Position via lookup ID
User Provision Requests ---related to--> Personnel Management via lookup ID

so it works like this:  User Provision Requests --->  Personnel Management ---> Recruiting Position

What I am trying to do is place a button on the User Provision Requests page  that would access a field on the Recruiting Position page and make a change.  I have already been able to make the correct ID calls for update on the User Provision Request and Personnel Management objects, but I am not having any luck with the recruiting position field.  Here's my code, which should explain what I am trying to do:

var PersonnelToUpdate = new sforce.SObject("Personnel_Management__c");
var PositionToUpdate = new sforce.SObject("Recruiting_Position__c");
var ProvisionToUpdate = new sforce.SObject("User_Provision_Request__c");

PersonnelToUpdate.Id = "{!User_Provision_Request__c.Employee__c}";
PersonnelToUpdate.Recruiting_Stage__c = "Former Employee";
PositionToUpdate.Id = "{Personnel_Management__r.Recruiting_Position__c.id}";
PositionToUpdate.Recruiting_Stage__c = "Open";
ProvisionToUpdate.Id = "{!User_Provision_Request__c.Id}";
ProvisionToUpdate.Provisioning_Request_Type__c = "Deactivate User Request Form (DURF)";

var result = sforce.connection.update([ProvisionToUpdate]);
var result = sforce.connection.update([PersonnelToUpdate]);
var result = sforce.connection.update([PositionToUpdate]);

if(result[0].success == "true"){
"An Error has Occurred. Error: " +
"An Error has Occurred. Error: " +

The Recruiting Position ID call is what is failing.  The personnel management and user provision requests calls/actions work just fine.  Any tips as to who I can call on a related field through a shared object?  I want to hop through a lookup field to an object that has an additional lookup field to have access to a 3rd object that contains the fields I want to update.

Your help is greatly appreciated!

Thank you in advance.

James LoghryJames Loghry
I'm not too familiar with the sforce javascript library, but relationships to other sobjects are always designated with an __r.  If I'm accessing any custom fields on a parent object, it's always Parent__r.Field__c.

What's more confusing is that I can either reference the "foreign key" or the Id of the parent relationship with __c on the child object OR with __r.Id on the parent object.

In your example, line 10 needs to be Recruiting_Position__r.Id in order to work.  (I believe with Javascript the fields are case sensitive too, so be careful with that).

Hope that helps.