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
Allen2Allen2 

please help me to correct the below trigger

Want to write a trigger on user, so for any user has einstein user package license, if any user of profile other than system admin and standard security is trying to update the user's profile should throw an error.

Also if the profile is changed to the new profile with prefix SM then also should throw the error.
public class errorMsgOnProfileUpdate {
    
    public static void errorMsgOnProfileChange (List<user> userList, Map<id,user> oldMap) {
        profile pf;
        userList = [Select Id, Profile.Name, ProfileId from User where  isActive = TRUE and Profile.UserLicense.LicenseDefinitionKey = 'SFDC' and Id in (SELECT UserID FROM UserPackageLicense WHERE (PackageLicenseId= '050800000004xiQ'))];
        pf = [Select Id, Name from Profile where Name = 'System administrator' or Name = 'Standard Security'];
        if(userList.size() > 0){
            for(User u: userList){
                if(trigger.isUpdate && u.Profile.Name != pf.Name && oldMap.get(u.id).ProfileId != u.ProfileId){
                    u.addError('Only system Administrator and security has access to edit profiles');
                }
            }  
        } 
    }
    
}
Please correct me as I'm gettin the exception error while updating the profile as below:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UserTrigger caused an unexpected exception, contact your administrator: UserTrigger: execution of BeforeUpdate caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times): ()

 
Agustin BAgustin B
Hi, it appears you are querying more than 200000 records on the query in line 5, is that correct for your requirement?
You could try filtering for more restrictive fields to get less records or use for example limit 200000 at the end of the query.
[Select Id, Profile.Name, ProfileId from User where  isActive = TRUE and Profile.UserLicense.LicenseDefinitionKey = 'SFDC' and Id in (SELECT UserID FROM UserPackageLicense WHERE (PackageLicenseId= '050800000004xiQ')) LIMIT 200000];
if it helps please like and mark as correct as it may help others with the same error.
Allen2Allen2

Hi,

I tried that as well but still the same error.

surojit Mondalsurojit Mondal
Hi Allen,

You can try out in this way.
public static void errorMsgOnProfileChange (List<user> userList, Map<id,user> oldMap) {
        Set<String> profIdSet = new Set<String>;
		Set<String> einsteinUserIds = new Set<String>;
		Set<String> transUserIds = new Set<String>;
		
		
		List<UserPackageLicense> einsteinUserPkgList = new List<UserPackageLicense>([SELECT UserID FROM UserPackageLicense WHERE (PackageLicenseId= '050800000004xiQ')]); /* Assuming 050800000004xiQ is Einstein LicenseId*/
		Map<Id, user> einsteinUserList;
		
		if(trigger.isUpdate && !einsteinUserPkgList.isEmpty()){
			for(UserPackageLicense ePkg: einsteinUserPkgList){
				einsteinUserIds.add(ePkg.UserID);
			}
			
			for(User u: userList){
				if(einsteinUserIds.contains(u.id)){
					transUserIds.add(u.Id);
				}
			}
			if(!einsteinUserIds.isEmpty()){
				einsteinUserList = [Select Id, Profile.Name, ProfileId from User where isActive = TRUE and Profile.UserLicense.LicenseDefinitionKey = 'SFDC' and Id in :transUserIds];
			}
		}
		
		List<Profile> pf = new List<Profile>([Select Id, Name from Profile where Name = 'System administrator' or Name = 'Standard Security']);
		
		for(Profile p: pf){
			profIdSet.add(p.Id);
		}
		
        if(einsteinUserList == null){
            for(User u: userList){
				User eUser = einsteinUserList.get(u.Id);
                if(trigger.isUpdate && (!profIdSet.contains(eUser.ProfileId)) && oldMap.get(u.id).ProfileId != u.ProfileId){
                    u.addError('Only system Administrator and security has access to edit profiles');
                }
            }  
        }
		
    }

And le me know if it works for you.

Regards,
Surojit