+ Start a Discussion
Suresh RaghuramSuresh Raghuram 

Set AccountId to null on Case when created by Email to Case

Hello Community,

I had to set Account To NULL when a case is being created by Email to Case future.
Any work around using triggers. Please let me know. 
If this is a impossible thing, point me to the right direction .

Thanks,
Suresh.
Best Answer chosen by Suresh Raghuram
Suresh RaghuramSuresh Raghuram

This can be possible directly from EmailMessage object on After Insert where ParentId is a case Id .

so write trigger on EmailMessage after the finish of the Email to Case process the EmailMessage Object field ParentId will be populated with the CASE ID. Fetch the Id and Query the Case and update it.

All Answers

ShashankShashank (Salesforce Developers) 
You can write a trigger in the email message object to update a custom field in the case to makr that it is coming from email-to-case, and then write another trigger on cases to clear out the accountId and contactId on the case. I haven't tried it myself but you can ive it a try. Please also be aware that to clear out the accountId, you should clear out the contactId as well on the case, because standard functionality will repopulate the accountId if contactId is not cleared out or made null.
Suresh RaghuramSuresh Raghuram
@Shashank

I did the second half of your explination.
I placed some code in the after Insert on Case and checked the Case Origin and tried to update the AccountId to null but I am getting the following error.
Even after doiing the first part of you explination, I suspect I will run into the same issue.
the Issue is

Subject: Sandbox: Email-to-Case: Error(s) encountered while processing
 
The following errors were encountered while processing an incoming email:
 
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : CaseTrigger: execution of AfterInsert
 
caused by: System.NullPointerException: Attempt to de-reference a null object

Correct me if my assumption is wrong.
 
Deepak Kumar ShyoranDeepak Kumar Shyoran
It shows above that your are getting NullPointerException you can easily remove this error with the help of debug logs.
Suresh RaghuramSuresh Raghuram
@Deepak
During the process of Email to Case creation Case is being created by the internal salesforce.
I am not seeing any logs.
 
Suresh RaghuramSuresh Raghuram

This can be possible directly from EmailMessage object on After Insert where ParentId is a case Id .

so write trigger on EmailMessage after the finish of the Email to Case process the EmailMessage Object field ParentId will be populated with the CASE ID. Fetch the Id and Query the Case and update it.
This was selected as the best answer