I would appreciate your help with the following Scenario:
I have activated Email-to-Case and cases are working properly but the
The Account ID / Account name on the Cases is empty.
A case is created having the fields (Webemail and Case Origin)
- webemail = xx@example.com
- Case Origin = Email1
My Accounts don't have contacts attached to them so the Email-to-case is not finding the accounts. But the object account has a field called "Email". So how can I update the field Account ID on the Case object using a process builder.
In simple English;
The process builder should query and Update the Case.AccountID field based on the following conditions:
Case.webemail == Account.Email
&&
Case.caseorigin == Account.sitename
since we migh have several accounts with the same email address but different account.sitename.
Appreciate your help.
T.
7 risposte
Hi Terek,
Below is the trigger code. Its hard to explain if you are begginner. Based on what you mentioned I have constructed the logic but you need to test it and make sure it works for your scenario.
trigger CaseTrigger on Case (before insert, before update) {
Map<String,Account> siteAccMap=new Map<String,Account>();
set<String> emailIdSet=new set<String>();
for(Case c:trigger.new){
if(c.AccountId==null && c.SuppliedEmail!=null){ //Checking if Account is blank
emailIdSet.add(c.SuppliedEmail);
}
}
for(Account acc:[select id,Email__c,SiteId__c from Account where Email__c IN:emailIdSet AND SiteId__c!='']){//Querying Accounts with Above case emails
String key=acc.SiteId__c+'-'+acc.Email__c; //Creating a unikwy key with SiteId and Email
if(!siteAccMap.containsKey(key)){
siteAccMap.put(key,acc);
}
}
for(Case c:trigger.new){
String key=c.Origin +'-'+c.SuppliedEmail;
if(siteAccMap.get(key)!=null){
Account acc=siteAccMap.get(key); //Getting the account that matches the combination of Site Id and Email from case
c.AccountId=acc.Id; //Assigning the account to the case
}
}
}
I would strongly suggest you to start with the below trailhead which will help you understand the trigger.
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro