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
RedSalesRedSales 

Get A Parent/Page's Object ID in A Custom Link/Button

Hi,

 

I wish to add a custom button or custom link to a salesforce.com object's page. For example add such a button to an Account or Opportunity.

My custom link/button will  Execute JavaScript.  I would like to add/use the object id from the Account/Opportunity on which the button exists to a javascript function.  Is there a way of retrieving the parent object ID on which the custom link or button will exist.

 

I know for the User object a function UserInfo.getUserId() which can be used. Is there a similar function (or other process) that can be used for an Account Object & for Opportunity objects?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
sebcossebcos

Hi,

you can reference ids and other fields of the record(s) just as you can do in validation rules and formulas or by using the Ajax toolkit, a js library, you can add more complex behavior. 

Here a couple of examples of what you can do, one for single record (detail page link or button) and for multiple records (List button) :

http://www.shamrockcrm.com/salesforce/salesforce-com-javascript-to-override-buttons/ .

http://www.interactiveties.com/b_execute_javascript_button.php .

 

The Salesforce online help also gives some examples; search for "useful custom button and links" .

To learn more about the Ajax toolkit: http://www.salesforce.com/us/developer/docs/ajax/index.htm .

 

 

All Answers

sebcossebcos

Hi,

you can reference ids and other fields of the record(s) just as you can do in validation rules and formulas or by using the Ajax toolkit, a js library, you can add more complex behavior. 

Here a couple of examples of what you can do, one for single record (detail page link or button) and for multiple records (List button) :

http://www.shamrockcrm.com/salesforce/salesforce-com-javascript-to-override-buttons/ .

http://www.interactiveties.com/b_execute_javascript_button.php .

 

The Salesforce online help also gives some examples; search for "useful custom button and links" .

To learn more about the Ajax toolkit: http://www.salesforce.com/us/developer/docs/ajax/index.htm .

 

 

This was selected as the best answer
armen-intecharmen-intech

This is an old post, but I didn't think the official solution was that clear.

 

At the moment (Summer '13), there is a trivial way to reference the current page's object id in javascript. As far as I can tell this approach will work with javascript on the page in any form (button or link)

 

 

in javascript use either one of these (one's a property, the other's a method):

window.sfdcPage.entityId
window.sfdcPage.getEntityId()

 that's it! Use it anywhere in javascript where you need the current page's object ID.  in my case I used it to create a custom "Log a Call" button on activities.

 

I have tested this on a variety of standard and custom objects in my instance and the value has been present.

It doesn't work on Report objects or User objects, but it works on things like pricebooks and other less common objects.

 

Additionally, we can also collect the current object's URL path:

window.location.pathname

Most SF URLs are structured like this:

https://<subdomain>.salesforce.com/<objectid>[?parameters]

 

 this command collects only the path in the format "/<objectid>", omitting the front domain and parameter part. this call is handy for dynamically specifying the retURL parameter when calling sub objects.

 

this can be used in a pinch to get the objectid as well (particularly in the cases where the entityId doesn't exist), but it needs string manipulation to remove the leading '/'.

 

it's a shame I couldn't find much on this googling for a solution. Hopefully what I found is of use to others.

Maimoona S.Maimoona S.
Thanks @armen-intech ! This really helps :) 
Alexandru IleanaAlexandru Ileana
Absolutely awesome!

This has been very helpful in identifying the current object for related lists when doing an on click event for a custom button.
I am now attempting to differentiate record types and page layouts using this method.

Many thanks!