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
jha.pk5@cloud.comjha.pk5@cloud.com 

Urgent Test Class for Trigger

 

trigger users on User (before insert, before update) {

    if(Test.isrunningtest()) {
        system.debug('Allowing user create/edit.');
    } else {
        List<User> oUser = [SELECT Id, Security_user__c, ProfileId, UserName FROM User WHERE Id = :System.UserInfo.getUserId() LIMIT 1];
        If(oUser[0].UserName.endsWith('.dev10')){
           
            system.debug('IS DEV USER: Allowing user create/edit.');
        } else {
            Boolean isSecurityUser = oUser[0].Security_user__c;
            if(isSecurityUser) {
               
              
            } else {
                if(Trigger.isUpdate) {
                    List<Profile> list_pAdmin = [SELECT Id FROM Profile WHERE Name IN ('System Administrator','SL: System Administrator','SL: Sys Admin ','SL: Read Only Admin','SL: Read Only Admin Non ') LIMIT 5];
                    Set<String> set_pAdmin = new Set<String>();
                    for(Profile pAdmin : list_pAdmin) {
                        set_pAdmin.add(pAdmin.Id);
                    }
                    for(User oUserRec : Trigger.New) {
                        if((oUserRec.Id == System.UserInfo.getUserId()) && (!set_pAdmin.contains(oUser[0].ProfileId))) {
                            system.debug('IS THE USERS OWN RECORD AND THEY ARE NOT A SYS ADMIN: Allowing user create/edit.');
                        } else {
                            system.debug('IS NOT A TEST, DEV ENVIRONMENT, A SECURITY USER OR THE USERS OWN RECORD: Forbidding user create/edit.');
                            oUserRec .addError('You do not have permission to create or edit user records!');
                        }
                    }
                } else {
                    for(User oUserRec : Trigger.New) {
                        system.debug('IS NOT A TEST, DEV ENVIRONMENT, A SECURITY USER OR THE USERS OWN RECORD: Forbidding user create/edit.');
                        oUserRec .addError('You do not have permission to create or edit user records!');
                    }
                }
            }
        }
    }

}