+ Start a Discussion
Prabhjot KPrabhjot K 

Status update on email login via einstein activity capture

Hi All,

We have a requirement to update a status field on a custom object whenever email activity gets logged in or sent via einstein activity capture. Initially, the status is open when an email is sent the status updates to "Waiting for guest response" and when the guest responds the status updates to "Guest replied".

I am not sure how this functionality can be achieved. Can this be done via process builder or Apex trigger? Can someone please guide me in the right direction ad help with this functionality?

Thanks!
AnudeepAnudeep (Salesforce Developers) 
Hi Prabhjot - You can achieve this using a simple workflow rule or write an apex trigger on EmailMessage

Workflow Rule:
  • Click on  Setup | Create | Workflows & Approvals | Workflow Rules
  • Click on New Rule
  • Select Email Message as the Workflow Object
  • Name your Rule and give it a description
  • Evaluation Criteria should be set as created
  • In the simple formula editor we can set the field as Email Message: Is Incoming, Operator as equals and Value as True
User-added image
 
Just to explain how the above works, the “Is Incoming” field is an automatic read only field that you can’t do a lot with. This is because it is automatically populated whenever an email is passed through Salesforce to the case. This is the indicator we are going to use to know if an email is being sent by our agent or if it is incoming from a customer.

After the Workflow trigger has been set up, we need to add a field update to correspond with the case status that is updated when it is in the hands of the support agent (as the email is incoming).

This workflow will then need to be cloned but created with the Value = False and the Workflow field update to obviously correspond to when it is in the customers hands.

Apex Trigger:

This is just a sample trigger. Please modify the logic to update the status accordingly
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;
}

Please mark this answer as Best if you find this information shared helpful
 
AnudeepAnudeep (Salesforce Developers) 
Also, see this blog to learn more about triggers on EmailMessage
Prabhjot KPrabhjot K
Hi Anudeep,

Thanks for replying. I think we cannot use email message object when we are using einstein activity capture. EAC only captures emails in the activity timeline of the object. Also, EAC doesn't work with custom objects. In this scenario, do you think I can use a trigger on the activity object?

Thanks!

 
AnudeepAnudeep (Salesforce Developers) 
If EmailMessage trigger is not going to work then yes you can write a new Task trigger in Customize --> Activities --> Task triggers

Here is a sample code
 
trigger UpdateContactOnTaskInsertUpdate on Task (after insert,after update){
List<Id> contact_ids = new List<Id>();
List<Contact> update_contacts = new List<Contact>();

for(Task t : trigger.new){
//if the whoid is a contact and the status is completed
if(t.WhoId instanceOf Contact && t.Status == 'Completed'){
contact_ids.add(t.WhoId);
}
}

//if the id list is not empty
if(!contact_ids.isEmpty()){
loop through associated contacts with completed tasks
for(Contact c : [SELECT fields_to_update FROM Contact WHERE ID IN :contact_ids){
//do updates here and add to list for dml
update_contacts.add(c)
}
}

//do dml
update update_contacts;
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization

Let me know if it helps