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
RSTRST 

System.NullPointerException: Attempt to de-reference a null object...welcome mail on user creation.

CLASS :-


public class UserDML { public static void sendEmailtoUser(List <User> newUsersTrigList) { list<EmailTemplate> Temp = new list<EmailTemplate >([SELECT id FROM EmailTemplate WHERE DeveloperName = 'User_Welcome_Email_Template_1' LIMIT 1]); //Collects all Users Ids to be used on query Set <Id> newUserIds = new Set <Id>(); for (User u : newUsersTrigList){ if ( u.Send_Welcome_Email_to_User__c ) { newUserIds.add(u.Id); } } //Retrieve list of users created/updated from Trigger list <User> newUsersList = [Select Id, ProfileID, Send_Welcome_Email_to_User__c From User Where Id in : newUserIds]; SystemEventLogDML sLogDML = new SystemEventLogDML(); list<System_Event_Log__c> SysEventLogsList = new list<System_Event_Log__c>(); try { if (newUsersList.size() > 0){ Send_Email_to_Users_Settings__c EmailUserSettings = new Send_Email_to_Users_Settings__c(); list <Messaging.Singleemailmessage> msglist = new list <Messaging.Singleemailmessage>(); list <User> upUsersList = new list <User>(); for(User u :newUsersList){ //Send Email to Users based on its Profile EmailUserSettings = Send_Email_to_Users_Settings__c.getValues(u.ProfileId); Messaging.Singleemailmessage msg = new Messaging.Singleemailmessage(); If(EmailUserSettings != null){ msg.setTemplateId(EmailUserSettings.Email_Template_ID__c); } else { msg.setTemplateId(Temp[0].id); } msg.setTargetObjectId(u.Id); msg.setSaveAsActivity(false); msglist.add(msg); //Sets flag back to unchecked after sending the message to the user's email u.Send_Welcome_Email_to_User__c = false; upUserslist.add(u); } //Sends email to Users if (EmailUserSettings.Email_Template_ID__c != null){ Messaging.sendEmail(msglist); } //Updates all users' Send Email checkbox update upUserslist; } } catch (Exception e ){ SysEventLogsList.add(sLogDML.generateSysLog('UserDML.sendEmailtoUser', e.getMessage(), '', slogdml.EvtType[0], UserInfo.getUserId())); } }

 Hi,

 

Getting this error while creating user. i have a checkbox field on user object send_mail_c . if its true then it sends a welcome mail template to user. i have created a profiles market and sales in custom settings and attached a template ID to it. but my requirement is to send a default mail when the profile doesnt exists in custom setting.

 

Trigger below :-

 

 

trigger UserMasterTrigger on User(Before insert, Before update, After Update, After insert)
{
    if(trigger.isBefore)
    {
        if(trigger.isInsert)
        {
            //call the handler for the before insert trigger event
            //population of Federation Id with SSO value
            //population of Manager lookup based on Manager SSO value
            UserTriggerHandler handler = New UserTriggerHandler();
            handler.onBeforeInsert(trigger.New);
        }
        
        if(trigger.isUpdate)
        {
            //call the handler for the before update trigger event
            //population of Federation Id with SSO value
            UserTriggerHandler handler = New UserTriggerHandler();
            handler.onBeforeUpdate(trigger.newMap, trigger.oldMap);
        }
    }
    if(trigger.isAfter){
        if(trigger.isInsert)
        {
            //call the handler for the after update trigger event
            //sending of Email to newly created Users
            UserTriggerHandler handler = New UserTriggerHandler();
            handler.onAfterInsert(trigger.newMap, trigger.oldMap);
        }
        if(trigger.isUpdate)
        {
            //call the handler for the after update trigger event
            //population of Manager lookup based on Manager SSO value of records outside of the trigger batch
            //sending of Email to updated users
            UserTriggerHandler handler = New UserTriggerHandler();
            handler.onAfterUpdate(trigger.newMap, trigger.oldMap);
        }
    }
}
RSTRST

Requirement :-

i have a custom field on user Send_Welcome_Email_to_User__c when checked to TRUE while creating a new user then it sends a welcome mail template say template 1 based on profiles like admin,sales,marketing.

These profiles are created in custom settings and a template ID of template1 is assigned to these profiles.

 

now, i need to send another welcome template say template2 ,while creating new user and the profile to these user doesn't exist in custom setting i.e other than admin,sales,marketing. profiles.

 

custom setting name is Send_Email_to_Users_Settings__c