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
Lili ChavesLili Chaves 

Trigger to disable mass transfer

I need our reps to only be able to transfer ownership if the lead belongs to a queue, I was able to successfully to this with a validation. But because I have the "Transfer Leads" permission enabled it has exposed the "Mass Transfer" lead link on the Lead object. 

So I am trying to write a trigger to prevent any profile other than  System Administrator from being able to bulk transfer, but I have no idea what I am doing. Can someone help me with the code for this trigger?


This is what I have so far: 

Trigger LeadMassTransfernotallowed on Lead (after update){

Id userProfileId = userinfo.getProfileId(); 
  String userProfileName = [SELECT ID, Name from Profile Where Id = :userProfileId].Name;
  
  for(Lead leadInsert: Trigger.new){
  if(userProfileName <> 'System Administrator'){
  //system.debug('my Oldvalue');
lead.adderror('You do not have the permission to mass transfer Lead Ownership, contact SalesOp')
  if(trigger.new[0].OwnerId!=trigger.old[0].OwnerId){
   }
  }
 }
}

 
Sharat C 2Sharat C 2
Hi Lili,

Use this code and let me know if it works for. I have tested it seems to be working as expected.
Trigger LeadMassTransfernotallowed on Lead (after update){
    
    if(trigger.isUpdate){
        Id userProfileId = userinfo.getProfileId(); 
        String userProfileName = [SELECT ID, Name from Profile Where Id = :userProfileId].Name;
        
        for(Lead leadInsert: Trigger.new){
            if(userProfileName <> 'System Administrator' && leadInsert.OwnerId != trigger.oldMap.get(leadInsert.Id).OwnerId){
                //system.debug('my Oldvalue');                      
                    leadInsert.adderror('You do not have the permission to mass transfer Lead Ownership, contact SalesOp');           
            }
        }
    }
    
}

 
Raj VakatiRaj Vakati
Use this
trigger LeadMassTransfernotallowed on Lead (after update){
    
    if(trigger.isUpdate){
        String userProfileName = [SELECT ID, Name from Profile Where Id = :Userinfo.getProfileId()].Name;
        for(Lead leadInsert: Trigger.new){
            if((userProfileName <> 'System Administrator') && (leadInsert.OwnerId != trigger.oldMap.get(leadInsert.Id).OwnerId)){
                    leadInsert.adderror('You do not have the permission to mass transfer Lead Ownership, contact SalesOp');           
            }
        }
    }
    
}

 
Lili ChavesLili Chaves
Both of these are not preventing bulk transfer, the error message shows up, but the user can still transfer the lead ownership. How can we edit the code to prevent bulk transfer? 

User-added image
Lili ChavesLili Chaves
Is there a way to redirect non admin users, when they click on the "Mass Transfer" link on the lead object to another page through a trigger? 
 
Sharat C 2Sharat C 2
Hi Lili,

Even though it shows that " 3 Leads Transferred " owner of those leads won't be changed, I just verified it. And you cannot redirect the user via trigger, if you want to redirect then you will have to develop custom UI ( VF page / Lightning Component ) for that.