You need to sign in to do that
Don't have an account?
TR 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!
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!
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
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
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
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