Of course people will suggest to use EmailMessage trigger but if I update case from there then the problem is in case auto response rule as our requirement is mainly to fire the autoreply after the case trigger insert the case original recipient email.
Code
trigger CaseTrigger on Case (after insert) {
List<Case> caselist = new List<Case>();
EmailMessage emailMsg= [SELECT ToAddress FROM EmailMessage
WHERE ParentId IN : trigger.newMap.keyset() ];
//this says no Sobject error
for(Case caseData : trigger.new)
{
Case origcase = new Case(Id=caseData.Id, Original_Recipient__c = 'kamran.jabbar@test.com'); //This will work.
Case origcase = new Case(Id=caseData.Id, Original_Recipient__c = emailMsg.ToAddress); //This is not working.
caselist.add(origcase);
}
update caselist;
}
I want the email message ToAddress field value dynamically to set in case field.
Is there some trick to fix this problem? If it is not possible then we need to change a big infra for that, please help. thank you.