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
Ravi sastriRavi sastri 

Test Class for Lead Owner Update

Hi Developers,
I'm new to salesforce. Below is my Trigger to Update lead Owner when Account owner field on Lead is updated. Can anyone help me with writing the test class for this code. 
trigger UpdateLeadOwner on Lead (before insert, before update) {
List<String> AccOwnerPicklist =  new List<String>();
    for(Lead L : Trigger.New){
        if(L.Account_Onwer__c != Null){  
            AccOwnerPicklist.add(L.Account_Onwer__c);
        } 
    }
    List<User> UserList = [select id, name from user where name IN :AccOwnerPicklist];
    
    if(UserList != NULL && UserList.size()>0){
        for(Lead L : Trigger.New){
            for(User U : UserList){
                if(L.Account_Onwer__c == U.Name){
                    L.OwnerId = U.Id;
                }
            }
        }
    }
}

Any help will be highly appriciated.

Thank you in advance
Ravi
Deepak Pandey 13Deepak Pandey 13
Like this Ravi.
@IsTest 
private class ClassName
    { 
        private static testMethod void myUnitTest() {
        Account objAcc=New Account(Name='FullNameTest');
        insert objAcc;
        Lead Lead lead=new Lead(LastName='TestLast',FirstName='TestFirst',Company='Test',Status='Inquiry',Account_Onwer__c =objacc.id); 
        insert lead; 
        
        update lead;
    }
}
Maharajan CMaharajan C
Hi Ravi,

You have to write the test class like below:

@IsTest 
private class ClassName
    { 
        private static testMethod void myUnitTest() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User UserObj= new User(Alias = 'Test', Email='Giga@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testinguser', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='Giga1234@testorg.com');
    
        Insert UserObj;
        
        
        Lead leads=new Lead(LastName='TestLast',FirstName='TestFirst',Company='Test',Status='Inquiry',Account_Owner__c= 'Testinguser'); 
        insert leads; 
                        
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Ravi sastriRavi sastri
Hi Raj,
Thank you very much for your concern. When I run the enclosed code, it shows the following error "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Account Onwer: bad value for restricted picklist field: Testinguser: [Account_Onwer__c]". Can you please help me in rectifying this error.

Thanks
Ravi
Maharajan CMaharajan C
Hi Ravi,

The error is due to you have enabled the Restrict Piclist Value Option in the Account Owner Field.

So use the Exact picklist value in the Test Class for Account owner Field and User Last Name based on your Account Owner Field in lead Object.

If you have the Account Owner Piclist have value as Ravi then use like below.

@IsTest 
private class ClassName
    { 
        private static testMethod void myUnitTest() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
     User UserObj= new User(Alias = 'Test', Email='Giga@testorg.com',
     EmailEncodingKey='UTF-8',LastName='Ravi', LanguageLocaleKey='en_US', // Use your Exact Account Picklistfield valueinLastName
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='Giga1234@testorg.com');
    
        Insert UserObj;
        
        
        Lead leads=new Lead(LastName='TestLast',FirstName='TestFirst',Company='Test',Status='Inquiry',Account_Owner__c= 'Ravi')
        insert leads; 
                        
    }
}


Or


Simply disable the Disable the Restrict picklist field option in the Leadowner field but its not a good.

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj