I have created a custom detail page button on lead to Accept Lead Ownership if user is member of the queue, OR current user is A particular User in my org. When queue member use this button it works perfectly but when user defined use this button then Owner of lead is not changing. It shows the Alert specified in else if condition though.
I have defined particular using condition :- else if (currentOwner == "005p0000001dOgP")
in code
Also OWD of lead is set to Private and using criteria based Sharing rule.
Main issue here is when specified user accepts a lead using this button specified alert pops up but owner remains the queue.
Button Code :-
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
var LeadObj = new sforce.SObject("Lead");
var previousOwner = "{!Lead.OwnerId}";
var currentOwner = "{!$User.Id}";
LeadObj.Id = "{!Lead.Id}";
LeadObj.OwnerId = "{!$User.Id}";
//if previousOwner is queue
var ownerRec = sforce.connection.query("SELECT owner.type, ownerid, Id from Lead WHERE owner.type= 'Queue' AND OwnerId='"+ previousOwner + "' AND Id='" + LeadObj.Id + "'");
var records1 = ownerRec.getArray('records');
if(records1 !=null && records1.length>0)
{
//currentOwner is part of queue
var currentOwnerRec = sforce.connection.query("SELECT g.UserOrGroupId From GroupMember g WHERE groupId ='" + previousOwner + "' AND g.UserOrGroupId ='" + currentOwner + "'");
var records2 = currentOwnerRec.getArray('records');
if (records2 !=null && records2.length>0)
{
var result = sforce.connection.update([LeadObj]);
location.reload();
alert("You have accepted this Lead");
}
else if (currentOwner == "005p0000001dOgP")
{
var result = sforce.connection.update([LeadObj]);
location.reload();
alert("Specified User have accepted this lead");
}
else
{
alert("You cannot take this Lead because you are not part of the queue");
}
}
else
{
if(previousOwner == currentOwner)
{
alert("You are already the Lead Owner ");
}
else if (previousOwner != currentOwner && previousOwner.startsWith("005"))
{
alert("You can not accept Lead Owned by other User");
}
else
{
var result = sforce.connection.update([LeadObj]);
location.reload();
}
}
Thanks for your time!
OR current user is A particular User in my org
is not clear. How you are defining particular user who can take the ownership. Secondly, what is OWD settings of Lead object? How the record is getting shared to the particular user who is not part of queue member?OwnerId
. You are merely updating the same lead without changing any of the values.