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
Bryan Revelant 7Bryan Revelant 7 

Javascript button Help - If userid != ownderid validation warning.

I want a validation on the Javascript buttin, where if the current user is not the owner of the record, then display an error message. Is this possible. It doesnt seem to like userid.

I have tried var currentUser = sforce.connection.getUserInfo().userId
$userid
etc.

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

  var currentUser = sforce.connection.getUserInfo().userId

// identify the record
var o = new sforce.SObject("ADR_Non_Approval_Object__c");

o.id = "{!ADR_Non_Approval_Object__c.Id}";

if(currentUser.id != o.OwnerId){
alert("You must be the Assigned Approver to Approve this request.");
}
else{



// make the field change
o.Approval__c = "Approve";
o.Approval_Status_Date_Time__c = new Date();

// save the change
sforce.connection.update([o]);
}
//refresh the page
window.location.reload();
Ramu_SFDCRamu_SFDC
Try the approach mentioned in the below post http://stackoverflow.com/questions/8085709/salesforce-javascript
Bryan Revelant 7Bryan Revelant 7
I tried that... It didnt work
Bryan Revelant 7Bryan Revelant 7
The error if I use that logic is !$User is underdefined On Sun, May 18, 2014 at 9:21 PM, Bryan Revelant wrote:
kevin lamkevin lam
A couple of things:
  1. currentUser is already an id, therefore currentUser.id is undefined.
  2. you initialised the variable o without setting any values and saving it, therefore its OwnerId is also undefined.
Bryan Revelant 7Bryan Revelant 7
This portion of the code is trying to say if current user id not = to ownerid then display error message.
if(currentUser.id != o.OwnerId){

do you mean it should be

currentuser != o.ownerid
then display error message
else set value?
Bryan Revelant 7Bryan Revelant 7
Tried this and it doesnt work either


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

  var currentUser = sforce.connection.getUserInfo().userId

// identify the record
var o = new sforce.SObject("ADR_Non_Approval_Object__c");

o.id = "{!ADR_Non_Approval_Object__c.Id}";

if(currentUser!= o.ownerid ){
alert("You must be the Assigned Approver to Approve this request.");
}
else{



// make the field change
o.Approval__c = "Approve";
o.Approval_Status_Date_Time__c = new Date();

// save the change
sforce.connection.update([o]);
}
//refresh the page
window.location.reload();
kevin lamkevin lam
var o = new sforce.SObject("ADR_Non_Approval_Object__c");

You initialised o in the line above but I can't see where its fields are initialised and the record saved, therefore o is still undefined.
kevin lamkevin lam
Have you tried this?

if('{$User.Id}' != '{!ADR_Non_Approval_Object__c.OwnerId}'){
KR_ForceKR_Force
Try this it should work..
you have toi set the owner id before you comapre it.

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

  var currentUser = sforce.connection.getUserInfo().userId

// identify the record
var o = new sforce.SObject("ADR_Non_Approval_Object__c");
o.OwnerId = "{!ADR_Non_Approval_Object__c.OwnerId}";
o.id = "{!ADR_Non_Approval_Object__c.Id}";

if(currentUser.id != o.OwnerId){

alert("You must be the Assigned Approver to Approve this request.");

}
else{
// make the field change
o.Approval__c = "Approve";
o.Approval_Status_Date_Time__c = new Date();

// save the change
sforce.connection.update([o]);
}
//refresh the page
window.location.reload();
Bryan REVELANT 2Bryan REVELANT 2
No, doesnt seem to work.. It validates if the current user is not the owner of the record however it also gives the valiadtion if I am the owner... It gives a valiadtion on all.
kevin lamkevin lam
Have you tried my suggestion earlier?

if('{$User.Id}' != '{!ADR_Non_Approval_Object__c.OwnerId}'){
Bryan Revelant 7Bryan Revelant 7
I did however if any oneelse is reading this the ownerid is 18chars where as the userid is 15 in which will never match
kevin lamkevin lam
You can convert it into 18 characters with the CASESAFEID function:
if('{!CASESAFEID($User.Id)}' != '{!ADR_Non_Approval_Object__c.OwnerId}'){