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
praveenreddy gaddam 10praveenreddy gaddam 10 

How to write a test class ? please help me

public static void UpdateCreatedBy(list<Visit> vst){
        Set<Id> ownerIdSet = new Set<Id>();
        for(Visit c : vst){
            ownerIdSet.add(c.OwnerId);
        }
        Map<Id,User> userMap = new  Map<Id,User>([Select Id,userrole.developername From User Where Id =: ownerIdSet]); 
        for (visit Vs: vst){
            if(userMap.containsKey(vs.OwnerId)){
                string roleName = userMap.get(vs.OwnerId).UserRole.developername;
                if(string.isBlank(roleName) || roleName == null) continue;
                if(roleName.contains('FSO')){
                    vs.CREATED_BY_retail__c     ='Retail FSO';
                }
                else if(roleName.contains('SO')){
                    vs.CREATED_BY_retail__c     ='Retail SO';
                }
                
               
            }
        }
    }
Best Answer chosen by praveenreddy gaddam 10
CharuDuttCharuDutt
Hii Praveenreddy
Try Below Test Class
@isTest
public class UpdateCreatedByTest {
    @isTest
    public static void unitTest(){
	list<Visit> lstVisit = new list<Visit>();
	 Profile p = [SELECT Id FROM Profile WHERE Name='Office Profile'];
        UserRole roleId  = [SELECT Id, Name FROM UserRole where Name = 'CEO'];
        User usr = new User(LastName = 'sample',
                            FirstName='sam',
                            Alias = 'simplSam',
                            Email = 'ssamsimple@asdf.com',
                            Username = 'ssamsimple@asdf.com',
                            ProfileId = p.id,
                            TimeZoneSidKey = 'GMT',
                            LanguageLocaleKey = 'en_US',
                            EmailEncodingKey = 'UTF-8',
                            LocaleSidKey = 'en_US',
                            UserRoleId = roleId
                           );
      
        insert usr;
        system.runAs(usr){
	
    Visit v = new Visit();
	/*fill required Fields*/
	lstVisit.Add(v);
	insert lstVisit;
	}
	
	ClassName.UpdateCreatedBy(lstVisit);
	
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 

All Answers

CharuDuttCharuDutt
Hii Praveenreddy
Try Below Test Class
@isTest
public class UpdateCreatedByTest {
    @isTest
    public static void unitTest(){
	list<Visit> lstVisit = new list<Visit>();
	 Profile p = [SELECT Id FROM Profile WHERE Name='Office Profile'];
        UserRole roleId  = [SELECT Id, Name FROM UserRole where Name = 'CEO'];
        User usr = new User(LastName = 'sample',
                            FirstName='sam',
                            Alias = 'simplSam',
                            Email = 'ssamsimple@asdf.com',
                            Username = 'ssamsimple@asdf.com',
                            ProfileId = p.id,
                            TimeZoneSidKey = 'GMT',
                            LanguageLocaleKey = 'en_US',
                            EmailEncodingKey = 'UTF-8',
                            LocaleSidKey = 'en_US',
                            UserRoleId = roleId
                           );
      
        insert usr;
        system.runAs(usr){
	
    Visit v = new Visit();
	/*fill required Fields*/
	lstVisit.Add(v);
	insert lstVisit;
	}
	
	ClassName.UpdateCreatedBy(lstVisit);
	
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
This was selected as the best answer
praveenreddy gaddam 10praveenreddy gaddam 10
Thank you bro
CharuDuttCharuDutt
Please Close your Query By Marking  It As Best Answer If It Helps So It Also Helps Others In Future