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
satheesh8.k1.3890099515516848E12satheesh8.k1.3890099515516848E12 

inbound Emailhandler

HI,
I have this code for InboundEmailHandler, which is working fine but i need to update more fields in contact object from email how we can do it.
for example:- I need to update phone number field from Email. 

global class SampleCreateContactEmailExample implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        Contact contObj = new Contact();
        try {       
            contObj = [select id, email,Subject__c, Email_Body__c, Phone, lastname   from contact where email =: email.fromAddress  limit 1];
        }
        catch(Exception e)
        {
            System.debug('************'+e.getMessage());
        }
        

        if(contObj.Id != null) 
        {
            contObj.Subject__c = email.subject;
            contObj.Email_Body__c  = email.plainTextBody;
            contObj.Phone=email.Phone;
        }
        else
        {
            contObj.lastname = email.fromName;
            contObj.email = email.fromAddress;
            contObj.Subject__c = email.subject;
            contObj.Email_Body__c  = email.plainTextBody;
        }                 
        upsert contObj; 
        
        // Insert attachments
        List<Attachment> lstAtt = new List<Attachment>();
                        
        if( email.textAttachments != null )
        {            
            for (Messaging.Inboundemail.TextAttachment txtAttachment : email.textAttachments) {
              Attachment attachment = new Attachment();
             
              attachment.Name = txtAttachment.fileName;
              attachment.Body = Blob.valueOf(txtAttachment.body);
              attachment.ParentId = contObj.Id;
              lstAtt.add(attachment);
             
            }
        }
               
        if( email.binaryAttachments != null)
        {
            
            for (Messaging.Inboundemail.BinaryAttachment binaryAttachment : email.binaryAttachments) {
              Attachment attachment = new Attachment();
             
              attachment.Name = binaryAttachment.fileName;
              attachment.Body = binaryAttachment.body;
              attachment.ParentId = contObj.Id;
              lstAtt.add(attachment);
            }
        }
               
        if(lstAtt.size() > 0)
            insert lstAtt;
            
        result.success = true;
        return result;
    }
}


Thanks
Satheesh
AshwaniAshwani
Are you gettng phone number inforamtion in email.

If yes than oyu can simply extract t he inforamtion form Emil Body and poulate the contact "phone" field with and update.
satheesh8.k1.3890099515516848E12satheesh8.k1.3890099515516848E12
Hi Ashwani

is it possible to share code for this problem,  I tried this way  contObj.Email_Body__c  =email.Phone;  but which gives error:

Varible doesn't exit phoneError: Compile Error: Variable does not exist: Phone at line 20 column 37

how to solve my problem please help with code.

Thanks
Satheesh