+ Start a Discussion

Is there a way to detect name and id of the current object from JavaScript

I want to reuse client-side JavaScript functions to perform common tasks on multiple SalesForce pages. Is there a way I can detect in JavaScript which sObject (it's name and Id) the script is being exectuted on?


For example, if my script is being called on Opportunity details page it will return 'Opportunity' and it's id. If it's being called from Account details it will return 'Account' and it's Id. Can this be done?


Thank you in advance.



Presuming you're talking about s-controls, you can do something ugly like:


var objectId = "{!Contact.Id}{!Account.Id}{!Opportunity.Id}{!Lead.Id}"  // etc...


Then when substitution is done, all will resolve to blank except for the one where you've been launched.


Once you have the id, you can strip off the first three numbers and use that to determine the object type. 


(Assuming you're dealing w/ standard objects.)



A second, cleaner, way is to call the S-control with a set of query parameters.


Make your buttons URL buttons instead of S-Control buttons and then pass the parameters you want:




and then, in your Javascript you can  use QueryString to get your values:


/* Client-side access to querystring name=value pairs
    Version 1.2.3
    22 Jun 2005
    Adam Vandenberg
function Querystring(qs) {
     // optionally pass a querystring to parse
    this.params = new Object()
    if (qs == null)

    if (qs.length == 0) return
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-
    qs = qs.replace(/\+/g, ' ')
    var parameters = qs.split('&') // parse out name/value pairs separated via &
// split out each name=value pair
    for (var i=0;i < parameters.length;i++) {
        var value;
        var pair = parameters[i].split('=')
        var name = unescape(pair[0])
        if (pair.length == 2)
            value = unescape(pair[1])
            value = name
        this.params[name] = value
function Querystring_get(key, default_) {
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;
    var value=this.params[key]
    if (value==null) value=default_;
    return value


    var ThisQuerystring = new Querystring();

    var objectType= ThisQuerystring.get("type");
    var objectId = ThisQuerystring.get("id");



Best, Steve.


Is it possible to update such a field, e.g. Account.Name, (in Edit mode) in javascript without saving it to the database so that the new value is displayed on the screen?



Valentino Rijhen