0

I'v created a email service class for email to lead.It should create lead if duplicate lead doesnt exist and if exist it should tag it under existing lead as ac ustom object "Email to Lead Activity" .it's working fine for insertion but fail at time of updation .

/*
    This Class is used for the email to creation of  Channel Partner lead  
    --------------------------------------------------------------------------------------
    Name                                Date                                Version                     
    --------------------------------------------------------------------------------------
    Rishi                             21/9/2016                              1.0   
    --------------------------------------------------------------------------------------
*/
global class KLCreateChannelPartnerLeadByEmail implements Messaging.InboundEmailHandler
{

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
    {
        string ChannelPartnerId = KLCommon.getRecordTypeId('Lead','Channel Partner');

        set<String> setEmailLeadFilter = KLCommon.getJunkEmailKeywords();

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        KL_Global_Settings__c objCustomSettng = KL_Global_Settings__c.getOrgDefaults();



        if(objCustomSettng != null && objCustomSettng.KL_Enable_Email_to_Lead_Inbound_Service__c)
        {
            Lead objLead;
            KL_Email_To_Lead_Activity__c objEmail = new KL_Email_To_Lead_Activity__c();
            objEmail.KL_Body__c = email.plainTextBody;
            objEmail.KL_From_Email__c = email.fromAddress;
            objEmail.KL_Subject__c = email.subject;
            objEmail.KL_Type__c = 'Lead';

            // Lead duplicity has been removed from Email to lead after discussion with Amitppt & Vinit on 22-02-2017
            // Comment was removed on 8 March after discussion with vinit on KL office
            List<Lead> lstLead = [select id from lead where RecordTypeId=:ChannelPartnerId and ( Email =: email.fromAddress 
                                  or KL_Email_2__c =: email.fromAddress )and  isConverted = : false ];

            if(lstLead != null && lstLead.size() > 0)
                objEmail.KL_Lead__c = lstLead[0].id;
            else
            {
                objLead = new Lead();
                objLead.Email = email.fromAddress;

                if(email.subject != null)
                    objLead.LastName = email.subject;

                objLead.Company = email.subject;
                objLead.Description = email.plainTextBody;
                objLead.KL_Lead_Type__c = 'Channel Partner';
                objLead.RecordTypeId = ChannelPartnerId;
                objLead.LeadSource = 'Email Activity';
                objLead.KL_Lead_Channel__c = 'Emails';
             }
             list<AssignmentRule> lstAR = [select id from AssignmentRule where SobjectType = 'Lead' and Active = true];

             if(lstAR != null && lstAR.size() > 0)
             {
                Database.DMLOptions dmlOpts = new Database.DMLOptions();  
                dmlOpts.assignmentRuleHeader.assignmentRuleId= lstAR[0].id;
                objLead.setOptions(dmlOpts); 
             }
            try 
            {             
                system.debug('@setEmailLeadFilter______'+setEmailLeadFilter);       
                system.debug('@email.plainTextBody______'+email.plainTextBody);
                system.debug('@result______'+setEmailLeadFilter.contains(email.plainTextBody));
                boolean isJunk = false;
                for(string eachJunkKeyword : setEmailLeadFilter)
                {
                    if(eachJunkKeyword != null && eachJunkKeyword.trim() != '')
                    {
                        if((email.plainTextBody.toLowerCase()).contains(eachJunkKeyword.trim()))
                            isJunk = true;
                    }
                }
                if(!isJunk)
                {
                    if(objLead != null)
                    {

                        insert objLead;
                        system.debug('@@@@@@@@@@@@objLead'+objLead);
                        objEmail.KL_Lead__c = objLead.id;
                    }
                }
                insert objEmail;
                result.success = true;
            }
            catch (exception e)
            {
                //create error log
                 KL_Error_Log__c objError = KLCommon.createErrorLog('ChannelPartnerLeadByEmail ', null, 'KLCreateChannelPartnerLeadByEmail', 'GET', 
                            'insert', '', 'Batch Case', 'Email2Lead','strResponse' ,e.getMessage()); 

                 insert objError;                    
                System.debug('@Error in email to lead :: '+e);
                result.success = false;
            }
        }
        else
            result.success = false;

        return result;
    }
}

I'm getting this error in debug log

Class.KLCreateChannelPartnerLeadByEmail.handleInboundEmail: line 60, column 1 01:10:41.0 (70483463)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

2
0

Since objLead is null, thats why it is giving NullPointerException

At the time of update, you need to assign objLead. I think in this block, line no. 37

if(lstLead != null && lstLead.size() > 0)
    objEmail.KL_Lead__c = lstLead[0].id;

Also put a check objLead !=null in this block before performing any operations on objLead

if(lstAR != null && lstAR.size() > 0 && objLead !=null)
{
    Database.DMLOptions dmlOpts = new Database.DMLOptions();  
    dmlOpts.assignmentRuleHeader.assignmentRuleId= lstAR[0].id;
    objLead.setOptions(dmlOpts); 
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.