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
TR NicholsTR Nichols 

Can I use a Trigger to update User Object from a Custom Object?

Can't do with Workflow or Process Builder...can I do this with a Trigger? New to Triggers

Trying to update a custom field (Call_Copy_Last_Reported_Date__c) on User object with a date (Week_Ending__c) from custom object (Call_Copy_Reporting__c) when a new Call Copy Report is created. 

Custom Object - Call Copy Reporting
Owner (Lookup)
Week_Ending__c (Date)

User Object
Call_Copy_Last_Reported_Date__c (Date)

Any help would be appreciated!
Best Answer chosen by TR Nichols
Maharajan CMaharajan C
Hi Nichols,

Please use the below Trigger and Apex Class:

Apex Class : 

public class CallCopyReportingTriggerHelper {
    Public static void updateOwner(List<Call_Copy_Reporting__c> newList){
        Map<Id,User> userMaptoUpdate = new Map<Id,User>();
        
        for(Call_Copy_Reporting__c ccrRec : newList){
            if(ccrRec.Week_Ending__c != null){
                user u = new user(Id = ccrRec.OwnerId, Call_Copy_Last_Reported_Date__c = ccrRec.Week_Ending__c);
                userMaptoUpdate.put(u.Id, u);
            }
        }
        
        if(!userMaptoUpdate.IsEmpty())
            update userMaptoUpdate.Values();
    }
}


Apex Trigger:

trigger CallCopyReportingTrigger on Call_Copy_Reporting__c (after insert) {
    CallCopyReportingTriggerHelper.updateOwner(Trigger.New);
}


While testing if you got any error message like (MIXED_DML_OPERATION) then just add the @future in the top of updateOwner Method Name in apex class like below otherwise leave the @future. 

public class CallCopyReportingTriggerHelper {
@future
    Public static void updateOwner(List<Call_Copy_Reporting__c> newList){
}
}

Thanks,
Maharajan.C

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Yes,you can write the trigger to update the field.Please find the sample code and change the logic according to your business requirement.

https://developer.salesforce.com/forums/?id=906F0000000921zIAA

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
TR NicholsTR Nichols
Hello Shirisha! Thanks for your quick response!

It appears that your Trigger is firing when a new User is created and then creates a new Record on a Custom Object. Please confirm if I am reading that right.

I would need a Trigger that fires when a Custom Object Record is created and only updates a Field on an existing User Record. The Owner of the Custom Object Record would be the User Record that needs the Field updated.

Please let me know if you can help!
THANKS!
TR Nichols 
Maharajan CMaharajan C
Hi Nichols,

Please use the below Trigger and Apex Class:

Apex Class : 

public class CallCopyReportingTriggerHelper {
    Public static void updateOwner(List<Call_Copy_Reporting__c> newList){
        Map<Id,User> userMaptoUpdate = new Map<Id,User>();
        
        for(Call_Copy_Reporting__c ccrRec : newList){
            if(ccrRec.Week_Ending__c != null){
                user u = new user(Id = ccrRec.OwnerId, Call_Copy_Last_Reported_Date__c = ccrRec.Week_Ending__c);
                userMaptoUpdate.put(u.Id, u);
            }
        }
        
        if(!userMaptoUpdate.IsEmpty())
            update userMaptoUpdate.Values();
    }
}


Apex Trigger:

trigger CallCopyReportingTrigger on Call_Copy_Reporting__c (after insert) {
    CallCopyReportingTriggerHelper.updateOwner(Trigger.New);
}


While testing if you got any error message like (MIXED_DML_OPERATION) then just add the @future in the top of updateOwner Method Name in apex class like below otherwise leave the @future. 

public class CallCopyReportingTriggerHelper {
@future
    Public static void updateOwner(List<Call_Copy_Reporting__c> newList){
}
}

Thanks,
Maharajan.C
This was selected as the best answer
TR NicholsTR Nichols
Thanks Maharajan! Works perfect!