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

trigger to update field in other object by taking value from other object

I have written trigger on emailmessage object which will update a field in the USer object.
trigger EmailMessageAfterUpdate on EmailMessage (before insert) {
        Set<Id> ownerIds = new Set<Id>();
       for(EmailMessage eMsg : { 
              Map<Id, User> mapUsers = new Map<Id, User>([SELECT Id,Out_of_Office_Email_Address__c ,Name FROM User WHERE out_of_office__c = true]);
             List<Case> caseList = [SELECT id,ownerId,Owner.Name,assignee__c ,Description, caseNumber FROM Case WHERE Id = :eMsg.ParentId ];
               system.debug('caseList' + caseList);
                    for(Case cse :caseList )
                              User u = new User();
                        system.debug('inside for loop');
                         if(mapUsers.containsKey(cse.ownerId)) {
                           system.debug('inside mapusers');
                            system.debug('from' + emsg.FromAddress); 
                             eMsg.Fromaddress = cse.Assignee__c ;
                             eMsg.FromAddress = u.Out_of_Office_Email_Address__c;----not able to update this lie
                             system.debug('email' + u.Out_of_Office_Email_Address__c );
                              system.debug('from1' + emsg.FromAddress); 
                    update caseList;  
But i am not able to update.Any suggestions wll be helpful.
Hi Satish,

They way ur updating is wrong, just u created the user and instantiating with the email address and u assigned caselist to cse, how you will update caselist , you need to update cse right, use cse.out_of_office_Email_Address_c; and update cse;
Hi Thanks for the reply,

I am updating the field in  User object(Out_Of_Office_Email_Address) with the value present in the EmailMessage From Adress field

Thats my requirement
Hello Satish,

First you should correct your code. You wrote SOQL query insdie the "For" loop. This is not best practise to write the code.
Below are the answer of your question:
1. If you want to update Case field then you have to write : cse.Out_of_Office_Email_Address__c = eMsg.FromAddress then you have to update cse list. update cse;

Let me know if you need any additional information and choose this as best answer if it helps you.
Hi thanks for reply,

I have a field i the USer object called out_of_office__C . i am trying to update that field by populating the fromaddress value present in the emailmessage for the particluar case.I know this will not work but just need some idea as how to implement it.
Hi Satish,
Try this code which I written earlier, It will definately help you.