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
Yeshi Mohammed 9Yeshi Mohammed 9 

Update Custom Lead/Contact Field when sending email from Lead

Hello,
I have process builder that updates custom field called "First Activity" when the a Task is created under Lead or Contact. It works fine when the activities are of "log a call" or "creating new task" Type. But when a user sends an Email from Lead or Contact my process builder is not updating "First Activity" field. 

I reached out to Salesforce support they said this is not achivable thru workflow or process builder. They said to try Apex Trigger .... Please help
Any sugestion will be apprciated
-Yeshi 
Danish HodaDanish Hoda
Hi Yeshi,
That's correct, you need to write Apex trigger on EmailMessage object
AbhishekAbhishek (Salesforce Developers) 
Yes, You need to write an Apex trigger on the EmailMessage object.

Code snippet:-

trigger exampleTrigger on EmailMessage (after insert) {
    set<Id> caseIds = new set<Id>();
    map<Id,string> case2CustomValueMap = new map<Id,sting>();
    for(EmailMessage message : trigger.new){
        if(message.Incoming == false){
            string myCustomValue;
            //Some sort of logic that gets the value for this

            caseIds.add(message.ParentId);
            case2CustomValueMap.put(message.ParentId, myCustomValue)
        }
    }
    list<Case> casesToUpdate = [Select Id, Number, Custom_Field__c From Case Where Id in: caseIds];
    for(Case c : casesToUpdate){
        c.Custom_Field__c = case2CustomValueMap.get(c.Id);
    }

    update casesToUpdate;
}


For further reference check this too,

https://salesforce.stackexchange.com/questions/16795/trigger-on-case-emailmessage


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.

Thanks.