+ Start a Discussion
Abdul Subahani BS 8Abdul Subahani BS 8 

If Email Address dosent exist create a contact and then case

global class inboundEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        contact con =[SELECT Id,AccountId,Email,FirstName,LastName,Name FROM Contact WHERE email= :email.fromaddress LIMIT 1];
        
        if(con != null){
            case cs=new case();
            cs.Status='New';
            cs.Origin='Phone';
            cs.contactid=con.id;
            cs.subject=email.subject;
            cs.description=email.plaintextbody;
            
            try{
                insert cs;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
            }
        }else{
/*in place of a print message I would like to create a new contact or a case?*/
             System.debug('contact dosent exist');  
        }
        
        return result;
    }
    
}

 
Best Answer chosen by Abdul Subahani BS 8
Maharajan CMaharajan C
Hi Subahani,

Please use the list instead of contact.
 
global class inboundEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        List<contact> con =[SELECT Id,AccountId,Email,FirstName,LastName,Name FROM Contact WHERE email= :email.fromaddress LIMIT 1];
        
        if(!con.isEmpty()){
            case cs=new case();
            cs.Status='New';
            cs.Origin='Phone';
            cs.contactid=con[0].id;
            cs.subject=email.subject;
            cs.description=email.plaintextbody;
            
            try{
                insert cs;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
            }
        }else{
            System.debug('contact dosent exist');  
            Contact c = new Contact();
            c.lastName = 'AbdulSubahani';
            c.email = 'AbdulSubahani@gmail.com';
            //Add all the Mandatory Fields to create contact
            try{
                insert c;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
                
            }
            
            return result;
        }
        
    }
}

Thanks,
Maharajan.C

All Answers

PriyaPriya (Salesforce Developers) 
global class inboundEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        contact con =[SELECT Id,AccountId,Email,FirstName,LastName,Name FROM Contact WHERE email= :email.fromaddress LIMIT 1];
        
        if(con != null){
            case cs=new case();
            cs.Status='New';
            cs.Origin='Phone';
            cs.contactid=con.id;
            cs.subject=email.subject;
            cs.description=email.plaintextbody;
            
            try{
                insert cs;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
            }
        }else{
/*in place of a print message I would like to create a new contact or a case?*/
             System.debug('contact dosent exist');  
            Contact c = new Contact();
            c.lastName = 'test';
            c.email = 'pranjan@gmail.com';
            //include all the necessary field to create contact
            try{
                insert c;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;

        }
        
        return result;
    }
    
}

Kindly try the above. 

If it helps, Kindly mark it as the best answer.

Regards,

Priya Ranjan

Abdul Subahani BS 8Abdul Subahani BS 8
Hi, Thank you for your quick response, following error occured 

List has no rows for assignment to SObject
Maharajan CMaharajan C
Hi Subahani,

Please use the list instead of contact.
 
global class inboundEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        List<contact> con =[SELECT Id,AccountId,Email,FirstName,LastName,Name FROM Contact WHERE email= :email.fromaddress LIMIT 1];
        
        if(!con.isEmpty()){
            case cs=new case();
            cs.Status='New';
            cs.Origin='Phone';
            cs.contactid=con[0].id;
            cs.subject=email.subject;
            cs.description=email.plaintextbody;
            
            try{
                insert cs;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
            }
        }else{
            System.debug('contact dosent exist');  
            Contact c = new Contact();
            c.lastName = 'AbdulSubahani';
            c.email = 'AbdulSubahani@gmail.com';
            //Add all the Mandatory Fields to create contact
            try{
                insert c;
                result.success=True;
            }catch(DMLException e){
                system.debug('Following error occured'+e);
                result.success=false;
                
            }
            
            return result;
        }
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer