I'm attempting to build a trigger that pulls the FullPhotoUrl from the User profile and set a field (FullPhotoUrl__c) on the SFDC_Employee__c object to the url. Here's the error I'm getting I'll paste the code underneath it.
System.NullPointerException: Attempt to de-reference a null object Trigger.EmployeeProfileImageHandler: line 16, column 1
trigger EmployeeProfileImageHandler on SFDC_Employee__c (before update) {
SFDC_Employee__c[] employees = (SFDC_Employee__c[])Trigger.new;
Set<String> employeelist = new Set<String>();
for (SFDC_Employee__c employee : employees) {
employeelist.add(employee.User_ID__c);
}
User[] photos = [Select Name, FullPhotoUrl From User where ID IN :employeelist];
Map<String, User> photomap = new Map<String, User>();
for (User photo : photos) {
photomap.put(photo.Name,photo);
}
for (SFDC_Employee__c employee : employees) {
employee.FullPhotoUrl__c = photomap.get(employee.Name).FullPhotoUrl;
}
}
I'm also getting an error on the test class which I've never run into
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): SFDC_Employee__c, original object: UserRole: []
Here's the test class code.
@IsTest
public class EmployeeProfileTest {
@TestSetup
static void TestEmployeeProfile() {
UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
insert r;
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User u = new User();
u.FirstName = 'Test';
u.LastName = 'UserLastName';
u.ProfileId = profileId.Id;
u.Email = '[email protected]';
u.Username = '[email protected]' + System.currentTimeMillis();
u.CompanyName = 'TEST';
u.Title = 'title';
u.Alias = 'alias';
u.TimeZoneSidKey = 'America/Los_Angeles';
u.EmailEncodingKey = 'UTF-8';
u.LanguageLocaleKey = 'en_US';
u.LocaleSidKey = 'en_US';
u.UserRoleId = r.Id;
insert u;
SFDC_Employee__c e = new SFDC_Employee__c();
e.Name = 'Test UserLastName';
e.Employee_User__c = u.Id;
e.User_ID__c = u.Id;
insert e;
}
@IsTest
static void methodTest() {
Test.startTest();
Test.stopTest();
}
}