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
Manish Kumar 61Manish Kumar 61 

Email2case not working

I wrote a trigger which is causing a side effect that email2case is not working. How do I debug this?

My code is
 
for (Case caseObj :Trigger.new) {
        if(caseObj.Origin == 'Email') {
            EmailMessage toEmail = [SELECT ToAddress FROM EmailMessage WHERE Incoming = true LIMIT 1];
            String te = String.valueOf(toEmail);
            System.debug('Email To Address is ' + toEmail);
            caseObj.Customer_Support_Email__c = te;
        }

    }

 
Best Answer chosen by Manish Kumar 61
StephenKennyStephenKenny
Hi Manish

I suspect the issue is line 4. You are trying to cast an SObjct as a string. If you replace your code with what is below, this should solve the issue:
for (Case caseObj :Trigger.new) {
        if(caseObj.Origin == 'Email') {
            EmailMessage toEmail = [SELECT ToAddress FROM EmailMessage WHERE Incoming = true LIMIT 1];
            String te = '';
            if(toEmail.ToAddress != null){
            	te = String.valueOf(toEmail.ToAddress);
            }
            System.debug('Email To Address is ' + toEmail);
            caseObj.Customer_Support_Email__c = te;
        }

    }
Please remember to mark this thread as solved with the answer that best helps you.

Kind Regards
Stephen

All Answers

StephenKennyStephenKenny
Hi Manish

I suspect the issue is line 4. You are trying to cast an SObjct as a string. If you replace your code with what is below, this should solve the issue:
for (Case caseObj :Trigger.new) {
        if(caseObj.Origin == 'Email') {
            EmailMessage toEmail = [SELECT ToAddress FROM EmailMessage WHERE Incoming = true LIMIT 1];
            String te = '';
            if(toEmail.ToAddress != null){
            	te = String.valueOf(toEmail.ToAddress);
            }
            System.debug('Email To Address is ' + toEmail);
            caseObj.Customer_Support_Email__c = te;
        }

    }
Please remember to mark this thread as solved with the answer that best helps you.

Kind Regards
Stephen
This was selected as the best answer
Manish Kumar 61Manish Kumar 61
Thx