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
Angelina MuddamalleAngelina Muddamalle 

Accept Button on Lead Detail Page

I have a requirement where the accept button should be available on the lead detail page. I came across the javascript custom button code shared in this link https://salesforce.stackexchange.com/questions/167037/accept-button-on-lead-detail-page which works well for admins and other manager profiles but is giving issues with Sales rep profiles.
Any suggestions what the issue might be?
Deepak Pandey 13Deepak Pandey 13
Hello Angelina ,
I think ,It's issue come because of role hierarchy or sales rep profile does not permission to update the record.
Angelina MuddamalleAngelina Muddamalle
Hi Deepak, I did check the differences in the profiles. The profiles for the reps do not have the permissions to transfer leads/ assign leads, so I tried to assign the same through permission sets but it is till not working. Any other permissions that need to be taken care of as well?
Deepak Pandey 13Deepak Pandey 13
Let me know about your Error to find out the exact reason.
Angelina MuddamalleAngelina Muddamalle
An Insufficient Privileges error message is dispalyed on the screen.
Deepak Pandey 13Deepak Pandey 13
This error come when the profile does not permission of Field or Object . So please check your Object level accessibility or field level accessibility.  
Angelina MuddamalleAngelina Muddamalle
Hi Deepak, I've tried all possible scenarios. The only difference is that when i gave the permisson through the permission set, the error is no longer displayed but nothing happens at the click of the button.
Deepak Pandey 13Deepak Pandey 13
Hi Angelina, Please Share your code. Than i can help you.
Angelina MuddamalleAngelina Muddamalle
{!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();  
  }
  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
 { 
   var result = sforce.connection.update([LeadObj]);     
   location.reload();
 }
}
Deepak Pandey 13Deepak Pandey 13
Hi Angelina,
Put alert on line where you are updating the record and than click on button and check where is the value not come.
{!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');
  alert('......'+records2);

  if(records2 !=null && records2.length>0)
  {
   var result = sforce.connection.update([LeadObj]);
   alert(result+'......');
   location.reload();  
  }
  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
 { 
   alert('......');
   var result = sforce.connection.update([LeadObj]);
 alert(result+'......');   
   location.reload();
 }
}