• Lili Chaves
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
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){
   }
  }
 }
}

 
Help with a unit test on a simple trigger
I have created a simple "before delete" trigger for Leads based on the current user's profile name, and it is working like a charm in Sandbox.  I have tried to write a unit test but have not had any luck. I downloaded a Test Generator app to assist and this is what I have so far. Can anyone please help! 

This is my trigger:

 trigger LeadDeleletionLQS on Lead (before delete) {

 //Prevent the deletion of leads of the current user is assigned the 
Lead Qualification Specialist profile.
  Id userProfileId = userinfo.getProfileId();
  String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
  
  for(Lead lead : trigger.old){ 
  if(userProfileName == 'Lead Qualification Specialist'){   

       lead.adderror('You do not have the permission to merge Leads, contact SalesOp');

        }

    }

}


Here is my draft of a unit test: 

@isTest
    private class LeadDeleletionLQS_Test {
 @isTest static void test_LeadDeleletionLQSU (){
    // Test data setup 
    // Create a Lead, then try to delete it
    Lead lead_obj = new Lead(LastName = 'LastName286', FirstName = 'First968', Company = 'Company326', Street = 'PratapNagar', City = 'Jaipur', State = 'TX', Phone = '54343-37369', LeadSource = 'TBD', Status = 'New');
    Insert lead_obj; 
    Id userProfileId = userinfo.getProfileId();
    String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
    if(userProfileName == 'Lead Qualification Specialist') 
    

    //Perform test
    Test.startTest();
    Database.DeleteResult result = Database.delete (lead_obj,false);
    Test.stopTest();
    // Verify 
    //In this case the deletion should have been stopped by the trigger,
    // so verify that we got back an error. 
    System.assert(!result.isSuccess());
    System.assert(result.getErrors().size() > 0);
    System.assertEquals('You do not have the permission to merge Leads, contact SalesOp',
                         result.getErrors()[0].getMessage());
    }
    
}
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){
   }
  }
 }
}

 
Help with a unit test on a simple trigger
I have created a simple "before delete" trigger for Leads based on the current user's profile name, and it is working like a charm in Sandbox.  I have tried to write a unit test but have not had any luck. I downloaded a Test Generator app to assist and this is what I have so far. Can anyone please help! 

This is my trigger:

 trigger LeadDeleletionLQS on Lead (before delete) {

 //Prevent the deletion of leads of the current user is assigned the 
Lead Qualification Specialist profile.
  Id userProfileId = userinfo.getProfileId();
  String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
  
  for(Lead lead : trigger.old){ 
  if(userProfileName == 'Lead Qualification Specialist'){   

       lead.adderror('You do not have the permission to merge Leads, contact SalesOp');

        }

    }

}


Here is my draft of a unit test: 

@isTest
    private class LeadDeleletionLQS_Test {
 @isTest static void test_LeadDeleletionLQSU (){
    // Test data setup 
    // Create a Lead, then try to delete it
    Lead lead_obj = new Lead(LastName = 'LastName286', FirstName = 'First968', Company = 'Company326', Street = 'PratapNagar', City = 'Jaipur', State = 'TX', Phone = '54343-37369', LeadSource = 'TBD', Status = 'New');
    Insert lead_obj; 
    Id userProfileId = userinfo.getProfileId();
    String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
    if(userProfileName == 'Lead Qualification Specialist') 
    

    //Perform test
    Test.startTest();
    Database.DeleteResult result = Database.delete (lead_obj,false);
    Test.stopTest();
    // Verify 
    //In this case the deletion should have been stopped by the trigger,
    // so verify that we got back an error. 
    System.assert(!result.isSuccess());
    System.assert(result.getErrors().size() > 0);
    System.assertEquals('You do not have the permission to merge Leads, contact SalesOp',
                         result.getErrors()[0].getMessage());
    }
    
}