1

Is there a way i can get a currently logged in user in a custom button running on javascript?

I have custom button on where i am trying to update certain fields like activated by. Is there a way i can get in a currently logged in user in the javascript?

Thanks

UPDATE :

Code

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}

var retVal = confirm("Are you sure?");
if(retVal)
{
var OrderObj = new sforce.SObject("Order__c");
OrderObj.id = '{! Order__c.Id}'; /* Need Id field to update Order*/
OrderObj.Status__c = 'Activated'; /* Change status */
OrderObj.Activated_By__c= {!User.Id};


/* update method takes an array of Orders; init to 1 element - 'OrderObj' */
var result = sforce.connection.update([OrderObj ]);

if (result[0].success == 'false') {
alert(result[0].errors.message);
}
else {
location.reload(true); /* redisplay the detail page */
}
}

Here is the error

enter image description here

2
  • Here is a good example - developer.salesforce.com/forums/?id=906F0000000AbWTIA0
    – kurunve
    Commented Mar 23, 2016 at 13:41
  • Thanks @kurunve. It was exactly what i was looking for. If you can add it as answer i can accept it and could be helpful for others who might be looking for this
    – Prady
    Commented Mar 23, 2016 at 14:05

1 Answer 1

3

You can use a global User object:

<a href="/{!$User.Id}">{!$User.Alias}</a>

In a custom button set the Behavior to "OnClick JavaScript", add following javascript location.href = "/{!$User.Id}" and add the button to the page layout:

enter image description here

7
  • No idea how to use it in Custom Javascript button
    – kurunve
    Commented Mar 23, 2016 at 13:43
  • @kurunve Check my update Commented Mar 23, 2016 at 13:49
  • @SergeyUtko i tried using the global User variable in the custom button, but it was throwing an error "Illegal" I couldnt make anything out of that error
    – Prady
    Commented Mar 23, 2016 at 14:07
  • 1
    @Prady, could you post your code? That example works for me as well
    – kurunve
    Commented Mar 23, 2016 at 14:18
  • 1
    @Prady just do OrderObj.Activated_By__c= "{!User.Id};" to ensure that those assigned variable is string
    – kurunve
    Commented Mar 24, 2016 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .