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
Robert Wambold 10Robert Wambold 10 

Trigger to update User Profile

Hello,

I have a simple trigger to update a few custom fields on the User Profile Object. Not sure what is missing...I want my trigger to fire when the User Profile is updated.

Thanks for your help!

 

trigger Update_User_Custom_Fields_Trigger on User (before insert, before update) {
for(User u:trigger.new){
u.Last_Login_for_PB_Flows__c = u.LastLoginDate;
u.Managers_Email_for_PB_Flows__c = u.Managers_Email_del__c;
u.AboutMe = '*** This is a Test ***'
}
}

 

Best Answer chosen by Robert Wambold 10
Andrew GAndrew G
Hi Robert

as a trigger, it works.  Did a quick test in my test env:
trigger Update_User_Custom_Fields_Trigger on User (before insert, before update) {
    for(User u:trigger.new){
    //u.Last_Login_for_PB_Flows__c = u.LastLoginDate;
    //u.Managers_Email_for_PB_Flows__c = u.Managers_Email_del__c;
    u.AboutMe = '*** This is a Test ***';
    u.MobilePhone = '04';
    }
}
The above works - i didn't test your custom fields.

When you say it doesn't work, what do you mean?  it doesn't fire? it updates some fields but not others?

Perhaps just check that the Trigger is active.

Regards
Andrew
 

All Answers

Andrew GAndrew G
Hi Robert

as a trigger, it works.  Did a quick test in my test env:
trigger Update_User_Custom_Fields_Trigger on User (before insert, before update) {
    for(User u:trigger.new){
    //u.Last_Login_for_PB_Flows__c = u.LastLoginDate;
    //u.Managers_Email_for_PB_Flows__c = u.Managers_Email_del__c;
    u.AboutMe = '*** This is a Test ***';
    u.MobilePhone = '04';
    }
}
The above works - i didn't test your custom fields.

When you say it doesn't work, what do you mean?  it doesn't fire? it updates some fields but not others?

Perhaps just check that the Trigger is active.

Regards
Andrew
 
This was selected as the best answer
Raj VakatiRaj Vakati
Your code looks good to me and can you please check your trigger is active or not?

When you said user profile, you mean user records ? or profile record?

 
Robert Wambold 10Robert Wambold 10

User record.

Trigger is active.

Andrew GAndrew G
trigger Update_User_Custom_Fields_Trigger on User (before insert, before update) {
    System.debug('@@DEBUG - In user Trigger');
    for(User u:trigger.new){
            System.debug('@@DEBUG - In trigger.new loop');
        //u.Last_Login_for_PB_Flows__c = u.LastLoginDate;
        //u.Managers_Email_for_PB_Flows__c = u.Managers_Email_del__c;
        u.AboutMe = '*** This is a Test ***';
        u.MobilePhone = '04';
    }
}

Update your trigger with some debug lines and turn on the debug logs and then test and review the logs.  Confirm that the trigger is being invoked and that the loop is happening.

Regards
Andrew
Robert Wambold 10Robert Wambold 10

My Custom Fields were the prodblem. Thank you both for taking time on a Sunday evening.

Kind regards,

Robert