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
KostyaKostya 

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.

SteveBowerSteveBower

 

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:

 

{!$SControl.mySControl}?type='Account'&id={!Account.Id}

 

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()
    this.get=Querystring_get
    if (qs == null)
        qs=location.search.substring(1,location.search.length)

    if (qs.length == 0) return
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    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])
        else
            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.

ValentinRomelioValentinRomelio

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?

 

Regards,

Valentino Rijhen