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
irlrobinsirlrobins 

Login details are not emailed to user after new user created

Hey all,

 

I'm using Apex to create a customer portal user. Code is below. Just before this method is called I've created an appropriate account and contact. The user is created correctly as expected. However, the login email is never sent. If I reset the password via the org, that email is sent fine. This apex code is executed by a user with a Customer Portal Manager Custom licence. No exceptions are thrown by the method. Any ideas as to why the login email is not sent?

 

    @future static void createUser(String contactId, String email,String firstname, String lastname, String profileId,String deliveryAddress1, String deliveryAddress2,  String deliveryTownCity, String phoneNumber,String approver,Boolean authapprover,String profile,Boolean displayprices,String employeeid,String departmentid,String costcentre,String payrollcode){
        try{
	Database.DMLOptions dmo = new Database.DMLOptions();
	dmo.EmailHeader.triggerUserEmail = true; //sent email with new password, etc
	User u = new User(alias = 'standt', email=email, 
	            emailencodingkey='UTF-8', firstname = firstname, lastname=lastname, languagelocalekey='en_US', 
	            localesidkey='en_IE_EURO', profileid = profileId, contactId=contactId,
	            timezonesidkey='Europe/Dublin', username=email+'.round2',
	            CompanyName = userCompanyName,
	            Approver__c=approver,Authorised_Approver__c=authapprover, Profile__c=profile, 
	            Display_Prices__c=displayprices,
				Employee_ID__c=employeeid, Department_ID__c=departmentid, Cost_Centre__c=costcentre,
			Payroll_Code__c=payrollcode); //create customer portal user
	        
	        u.setOptions(dmo);
	        insert u;

	        insertDeliveryAddress(u,deliveryAddress1, deliveryAddress2,deliveryTownCity, phoneNumber); //insert default delivery address for user
        }catch(System.DmlException e){
								
				ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,e.getMessage());
        }
        	
    }	
Pradeep_NavatarPradeep_Navatar

Tryout this sample code login details. Make sure that the custom portal setting is enabled in the org.

 

Database.DMLOptions dlo = new Database.DMLOptions();

dlo.EmailHeader.triggerUserEmail= true;                                

Database.saveresult sr = Database.insert(u3,dlo);

irlrobinsirlrobins

How does that differ from the way I'm currently doing it?

irlrobinsirlrobins

Never got this working using the method above. So what I did instead as a work around is the code below, called just after the new user is inserted.

ResetPasswordResult resetPasswordResult = System.resetPassword(u.id, false); //reset password
String newpassword = resetPasswordResult.getPassword();
				
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); //create email to sent new password to user
mail.setToAddresses(new String[]{u.email});
mail.setOrgWideEmailAddressId('0D220000000ffz8BCAS');
mail.setSubject('Login details');
String body = 'Dear '+u.firstname+',\n\n Your registration request has been approved.';
body = body + '\n Your login and temporary password information is below.';
body = body + 'Username: '+u.username+'\n';
body = body + 'Password: '+newpassword;
mail.setPlainTextBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 A bit longwinded but it works!