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
Jasmeen Kaur 22Jasmeen Kaur 22 

Email to Case: Subject

Hi Experts
Here is a question regarding Email to Case. In my org, we use Email to Case. The Subject of email-to-case is like : "Credit Hold : (ABCDEFGH ) : Sales Order : 1236589kmjl456" Where ABCDEFGH = Account name.
On Case Page layout, we have a field = Account name. Is there a way that This field can be auto filled from Subject by finding the string in ()? Because the Account name in subject is always in ( ).
I am an Admin so, I don't know much coding. Any help will be highly appreciated.
Regards
Riley Griffin 123Riley Griffin 123
Yes this is possible.

Please reference here: https://developer.salesforce.com/forums/?id=9060G000000BirVQAS
 
Jasmeen Kaur 22Jasmeen Kaur 22
Hi Riley
I am aiming to populate the account lookup, not just a text field with a name in it. Is it posible and how?
Thanks 
Riley Griffin 123Riley Griffin 123
Yes that is possible. But when putting the data inside the lookup field you need to send the account Id
Jasmeen Kaur 22Jasmeen Kaur 22
Yes, Thats correct.
To be honest, I never created any apex class. Can you please help me with this by explaining bit more? Or if it is possible by using Flows?

 
Riley Griffin 123Riley Griffin 123
I don't know about flows. But you can do it with the process builder
Jasmeen Kaur 22Jasmeen Kaur 22
Is it?
I can't find it out, can you please elaborate it if you have ever did this?
Riley Griffin 123Riley Griffin 123
Yeah I did this once, not with accounts but with sic codes. Look through here: https://help.salesforce.com/articleView?id=000337963&language=en_US&mode=1&r=https%3A%2F%2Fwww.google.com%2F&sfdcIFrameOrigin=null&type=1
Jasmeen Kaur 22Jasmeen Kaur 22
But I dont think that  this will help in my case. In my case There is a Subject, and I need to substring that subject and then populate a lookup value in Account Field.
Well, Thanks for investigating through.
 
Riley Griffin 123Riley Griffin 123
Oh my apologies. I thought they were separate questions. I believe would have to use apex to use apex to do that
Jasmeen Kaur 22Jasmeen Kaur 22
Thats alright. But I never created any apex class. Do you mind to help me in creating it?
 
Deepak DineshDeepak Dinesh
Hi Jasmeen,
Good Day!

Please let me know whether the sample code meets your requirements:

Trigger CaseTrigger on Case(after insert){
    Public Static final STR1='(';
    Public static final STR2 = ')';
    set<String>accNameSet = new Set<String>();
    Map<String,String>accNameMap = new Map<String,String>();
    set<Id>accId= new Set<Id>();
    List<Case>caseUpdateList = new List<Case>();
    for(Case c: trigger.new){
        if(c.origin.equalsIgnoreCase('Give the Case origin you have set on Email-to-Case Settings') && !string.isBlank(c.Subject)){
            string subject = string.valueof(c.Subject);
            string accName = subject.substringBetween(STR1,STR2);
           accNameSet.add(accName);
           caseUpdateList.add(c);
        }
    }
    for(Account acct:[Select Id,Name from Account where Name IN :accCaseMap.keyset()])
    {
       accNameMap.put(acct.Name,acct.id);
    }
if(!caseUpdateList.isEmpty() && !accId.isEmpty())
{
    List<Case>finalCaseList = new List<Case>();
    for(Case c1 : caseUpdateList){
        string caseSubject = string.valueof(c.Subject);
        string acctName = caseSubject.substringBetween(STR1,STR2);
        c1.Account = accNameMap.get(acctName);
        finalCaseList.add(c1);
    }
    Database.update(finalCaseList,false);
}
}

The only Potential risk I can see in the above solution, if there are more than one Accounts with same name, we might end up mapping wrong account to the Case.

So, please make sure to adjust the duplicate Account Name issue in the org.

Please mark this as best answer if this solves your requirement.