The following trigger will insert a new user record when ever contact record is created and send email notification with password.
- trigger PortalUserCreation on Contact (after insert) {
- List<User> userList=new List<User>();
- Profile pf = [SELECT Id FROM profile WHERE name='Standard User' limit 1];
- for(Contact objectContact:System.Trigger.new)
- {
- User mockUser=new User();
- mockUser.contactId=objectContact.Id;
- mockUser.username=objectContact.Email;
- mockUser.firstname=objectContact.FirstName;
- mockUser.lastname=objectContact.LastName;
- mockUser.email=objectContact.Email;
- mockUser.communityNickname = (objectContact.LastName +string.valueof(Math.random()).substring(4,9));
- mockUser.alias = string.valueof(objectContact.LastName.substring(0,1) +
- string.valueof(Math.random()).substring(4,9));
- mockUser.profileid = pf.Id;
- mockUser.emailencodingkey='UTF-8';
- mockUser.languagelocalekey='en_US';
- mockUser.localesidkey='en_US';
- mockUser.timezonesidkey='America/Los_Angeles';
- // Mail alert Coding with temporary password
- Database.DMLOptions dlo = new Database.DMLOptions();
- dlo.EmailHeader.triggerUserEmail = true;
- dlo.EmailHeader.triggerAutoResponseEmail= true;
- mockUser.setOptions(dlo);
- userList.add(mockUser);
- }
- insert userList;
- }
No comments:
Post a Comment