1

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!

5
  • 2
    This part 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? Commented Sep 18, 2017 at 16:11
  • Also, format your code properly with correct indentations so that it could be readable. Commented Sep 18, 2017 at 16:16
  • Hi Sir, I have update my question with more details. "OR current user is A particular User" Means this button was developed for accepting lead from queue by queue member but i have added condition : else if (currentOwner == "005p0000001dOgP") to make an exception. Commented Sep 18, 2017 at 16:35
  • At no point do you loop through your records and actually assign the new OwnerId. You are merely updating the same lead without changing any of the values. Commented Sep 18, 2017 at 18:12
  • Hi, Line no 5 : LeadObj.OwnerId = "{!$User.Id}" is doing owner change. Actually above code works for members of queue but for the specified user in code its showing the alert but not changing the owner. Commented Sep 18, 2017 at 19:57

1 Answer 1

1

You need to change the logic.

First check the exception user who will be taking the ownership otherwise in the else part put your all other logic.

Also, hard coding of userId is not appreciated. Either you can build custom settings or Custom labels to make it more maintainable.

{!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}"; 

  //first you need to check the exception user.
if(currentOwner == "005p0000001dOgP")
{
    var result = sforce.connection.update([LeadObj]); 
    location.reload(); 
    alert("Specified User have accepted this lead"); 
}
else
{
      //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 
         { 
            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(); 
        } 
    }
}
3
  • Thanks for the help...but still same issue. Specified alert popup but Owner remains same. Commented Sep 18, 2017 at 17:57
  • Can you login with the exception user and try to change the ownership manually from Case detail page without using this button click? Commented Sep 18, 2017 at 18:22
  • Hi Sir, Thats exactly the problem was....user didn't had transfer leads permission to change the owner. Thanks a lot! Commented Sep 18, 2017 at 20:11

You must log in to answer this question.

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