Select your Language

Email to Lead

 For customization you need to write the code as below:

======================================================

global class International_Lead implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 

                                                           Messaging.InboundEnvelope env)

    {

        // Create an InboundEmailResult object for returning the result of the  Apex Email Service 

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

        map<String, Id> mapUnitCodeToId= new map<String,Id>();

        map<String, Id> mapUnitNameToId= new map<String,Id>();

        list<lead>allLead = new list<lead>();// list for inserting all leads

        // getting record type id of International patient intimation 

        String recordTypeId = Schema.getGlobalDescribe().get('lead').getDescribe().getRecordTypeInfosByName().get('International Patient Intimation').getRecordTypeId();    

        system.debug('subject'+email.subject);

        system.debug('plain text body'+email.plainTextBody);

        string MailBody = email.plainTextBody;

        string bodyAfterFirstname= mailBody.substringAfter('First Name');// body after first name

        system.debug('Email bodyAfterFirstname: '+bodyAfterFirstname);

        string[] splitted = bodyAfterFirstname.split(',');

       system.debug('first name is: '+splitted[0].subStringAfter(': '));

        system.debug('last name is: '+splitted[1].subStringAfter(': '));

        system.debug('Mobile no is: '+splitted[2].subStringAfter(': '));

        system.debug('Hospital name is: '+splitted[3].subStringAfter(': '));

        system.debug('doctor name is: '+splitted[4].subStringAfter(': '));

       system.debug('passport number: '+splitted[6].subStringAfter(': '));

        system.debug('Partner Name: '+splitted[8].subStringAfter(': '));

        // Creating Lead record

        try{

        Lead newLead = new Lead();

        newLead.RecordTypeId=recordTypeId;

        newLead.FirstName=splitted[0].subStringAfter(': ');

        newLead.LastName=splitted[1].subStringAfter(': ');

        newlead.MobilePhone=splitted[2].subStringAfter(': ');

        newlead.Hospital_Name__c=splitted[3].subStringAfter(': ');

        newlead.Doctor_name_of_Disease_Speciality__c=splitted[4].subStringAfter(': ');

            if(splitted[5].subStringAfter(': ').contains('mailto'))

            {

                 system.debug('Email is: '+splitted[5].subStringAfter(': ').substringBefore('<mailto'));

                 newlead.Email=splitted[5].subStringAfter(': ').substringBefore('<mailto');

            }

            else

            {

                system.debug('Email is: '+splitted[5].subStringAfter(': '));

                newlead.Email=splitted[5].subStringAfter(': ');

            }

        newlead.Passport_Number__c=splitted[6].subStringAfter(': ');

        newLead.Unit_Code__c=splitted[7].subStringAfter(': ');

        newLead.Nationality__c= splitted[8].subStringAfter(': ');

        newLead.Partner_s_Name__c= splitted[9].subStringAfter(': ');

        newlead.LeadSource='Intimation Without Referral';

        newlead.Run_Assignment_Rules__c = False;

        // assigning unit lookup based on unit code

        for(Unit__c unit:[select id,name,Unit_code__c from unit__c])

        {

            mapUnitCodeToId.put(unit.Unit_code__c,unit.id);

            mapUnitNameToId.put(unit.Name,unit.id);

        }

        system.debug('unit code map:'+mapUnitCodeToId);

        system.debug('unit name map: '+mapUnitNameToId);

        User currentUser =[Select Id,Name,Email,UserRoleId,UserRole.name from User where Id=:userinfo.getuserId()];

        String unit;

        unit = UserFinderUtility.getUserUnitName(currentUser); 

        if(newlead.Lead_Owner_Unit__c==null){

            if(newlead.Unit_code__c!=null){

                unit = newlead.Unit_code__c;

                system.debug('Unit code: '+unit);

                if(mapUnitCodeToId.containsKey(unit)){

                    system.debug('UNit id: '+mapUnitCodeToId.get(unit));

                    newlead.Lead_Owner_Unit__c = mapUnitCodeToId.get(unit);     

                }

            }

            else if(mapUnitNameToId.containsKey(unit)){

                newlead.Lead_Owner_Unit__c = mapUnitNameToId.get(unit);     

            }

            

        }

        // assignment Rule run while inserting the record

        Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule= true;

        newlead.setOptions(dmo); 

        allLead.add(newlead);

        System.debug('New Lead record: ' + newLead );  

        if(allLead.size()>0){insert allLead;}

        }

        catch(Exception e)

        {

            system.debug('Exception '+e);

            Logger__c l = new Logger__c();

            l.Job_ID__c = 'International Email-To-Lead Error Log';

            l.Description__c= string.valueOf(e);

            l.Summary__c= MailBody;

            insert l;

        }

        system.debug('All leads: '+allLead);

        // Set the result to true. No need to send an email back to the user      

        // with an error message   

        result.success = true;

        // Return the result for the Apex Email Service 

        return result;

    }

    }


====================================================

Email send in below formate to the routing address which generated by email service :

First Name: Test  001 ,

Last Name: March 9 ,

Mobile: 889924647,

Hospital Name: hospital,

Doctor Name: Rajesh Kumar,

Email: ankitvermaom@gmail.com,
Passport no: 5057,  
Unit Code: 1111,
Nationality: Indian,

Partner Name: Healing Med Healthcare Services Pvt. Ltd. ,



No comments:

Post a Comment