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
Aniruddha.ManakeshwarAniruddha.Manakeshwar 

How to write trigger to get default Mail id while making Contact as Enable as portal user?

 

I want to capture the default mail id for Ex "Support@support.com" on E-mail field on Contact, while making the contact as Portal user? How to pre populate the Emailid ?
Can somebodys suggest/help?

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
AmitSahuAmitSahu

Probably you can create a trigger on Users object.

 

The trigger should fire when the profile is Portal User and set email as you want.

All Answers

AmitSahuAmitSahu

Probably you can create a trigger on Users object.

 

The trigger should fire when the profile is Portal User and set email as you want.

This was selected as the best answer
Aniruddha.ManakeshwarAniruddha.Manakeshwar

Hi,

 

I have created trigger on User object, but it is not working, i am writing Trigger for the First time,  can you please help me out on this.

Below is the trigger which i wrote, let me know if its wrong.

 

trigger getEmailOnPortalUserCreation on User(before insert, before update) {
String typeOfUser='';
    
    for (User u : [SELECT Id, UserType, ContactId From User]){
            System.debug('User Type is ' + u.UserType);
           typeOfUser = u.userType;
    
     }
for(User u:Trigger.new)
    //for case created by portal users only, get the userType to check for Portal user
      
         if(typeOfUser  == 'CSPLitePortal'){  
           
                u.Email = 'AgentSupport@support.com';
          }
 
}

AmitSahuAmitSahu

You can try this : (I am not sure what is the field on your user record that shows this is a portal use)

 

trigger getEmailOnPortalUserCreation on User(before insert, before update) {
String typeOfUser='';

for(User u:Trigger.new)

if(u.typeOfUser == 'CSPLitePortal'){

u.Email = 'AgentSupport@support.com';
}

}

Aniruddha.ManakeshwarAniruddha.Manakeshwar

Hi,

 

It is still not working for me. I did not get any compilation errors. When i create a portal user, Email id is not getting updated.

 

Is there anything that we can do ?we have just written Apex Trigger, will it be sufficient or it required any other ?

AmitSahuAmitSahu
You need nothing else... just trigger should be working. May be you need to check for the conditions. If we are comparing wrong field. Remember to check condition for the profile not for any other field. You are not using profile anywhere in the code.
Aniruddha.ManakeshwarAniruddha.Manakeshwar

Hi,

 

I have created new trigger, can you please let me know what is missing for this Trigger? 

 

 

trigger SetEmailIdOnPortalUserCreation on User(after insert) {
List<User> portalUsers = [Select Id, Email from User where Id IN :Trigger.new and UserType = 'CSPLitePortal' and Profile.Name='Overage High Volume Customer Portal User'];

for(User u : portalUsers){
u.Email = 'LiveGuideSupport@jci.com';
}

update portalUsers;
}

AmitSahuAmitSahu

Please try this and let me know the out put : 

 

trigger SetEmailIdOnPortalUserCreation on User(after insert) {
List<User> portalUsers = [Select Id, Email from User where Id IN :Trigger.new and UserType = 'CSPLitePortal' and Profile.Name='Overage High Volume Customer Portal User'];

List<user> lstUsr = new List<User>();

 

for(User u : portalUsers){
u.Email = 'LiveGuideSupport@jci.com';

lstUsr.add(u);
}

update lstUsr;
}

Aniruddha.ManakeshwarAniruddha.Manakeshwar

Hello,

 

I checked with updated Trigger,  but still email is not getting updated to default value as "LiveGuideSupport@jci.com" ?

I really do not have any idea, why salesforce is not updating the records, does Salesforce doesn't allow us to update the Email id on User Edit Screen 

( when Contact Enable as portal user, user edit screen displays)

AmitSahuAmitSahu

Have you checked the email inbox for any confirmation if you are getting eny. Usually when you change user's email address the email address does not change right away. You have to confirm that new email address.

 

Please check if you are getting any email messages to confirm the email.