1

I scheduled batch apex but get an error First error: Attempt to de-reference a null object, i have try to use if condition to catch the null parameter but no luck. I little bit curious here since i only use simple query to get the list of contact.

global class MonthlyEscrowStatementBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
    String query = 'SELECT Id FROM Contact WHERE Escrow_Account__c != null AND Account.Are_All_Opportunities_Closed__c = false';
    return Database.getQueryLocator(query);    
}

global void execute(Database.BatchableContext bc, List<Contact> ContactList){
    // process each batch of records
    if(ContactList.size()>0){
        for(Contact con : ContactList){
            if(con.id!=null)
                GenerateAndSubmitForApproval.GenerateAndSubmit(con.id, 'EscrowStatement', 'Escrow Monthly Statement', null);
        }  
    }

}    

global void finish(Database.BatchableContext bc){
}
}

Here is the GenerateAndSubmitApproval class to generate the document in salesforce

global without sharing class GenerateAndSubmitForApproval {
    webservice static string GenerateAndSubmit(string ContactID, string FileName, string type, string OppID)
    { 
        //Parameter initialisation
        PageReference pdf;
        if(type=='Monthly Escrow Statement'){
            pdf = Page.MonthlyEscrowStatement;
            pdf.getParameters().put('id',ContactID);
        }
        // the contents of the attachment from the pdf
        Blob body;

        //bypass test method for test classes
        if(!Test.isRunningTest())
            body = pdf.getContent();
        else
            body = Blob.valueOf('Some Text');

        try
        {
            //Generate content version with attached generated pdf
            ContentVersion v = new ContentVersion();
            v.versionData = body;
            v.title = FileName;
            v.pathOnClient ='/test.pdf'; //set the dummy path on client attribute (required)
            insert v;

            //if content version is create successfully

            if(v.id!=null) 
            {
                //get contact document id from created content verison
                List<ContentVersion> ContentVersions = [select ContentDocumentID from ContentVersion where id =: v.id];

                //if content document ids are present
                if(!ContentVersions.isEmpty())
                {
                    //share the content with related contact
                    ContentDocumentLink contentDocumentLinkObj = new ContentDocumentLink();
                    contentDocumentLinkObj.ContentDocumentId = ContentVersions.get(0).ContentDocumentID;
                    contentDocumentLinkObj.Visibility = 'AllUsers'; 
                    contentDocumentLinkObj.LinkedEntityId = ContactID;
                    contentDocumentLinkObj.ShareType='V';
                    insert contentDocumentLinkObj;

                    //Get Deposite Invoice ID
                    List<ContentWorkspace> CWList = [select id from ContentWorkspace where name = 'Generated Documents'];
                    if(!CWList.isEmpty())
                    {
                        //Share with Deposite Invoice
                        ContentWorkspaceDoc CWD = new ContentWorkspaceDoc(ContentWorkspaceId = CWList.get(0).id, ContentDocumentId = ContentVersions.get(0).ContentDocumentID);
                        insert CWD;

                        //Create Doc record
                        Doc__c D = new Doc__c(contact__c = ContactID, Content_Verison_Name__c = FileName, Content_ID__c = V.id, Status__c = 'Submitted For Approval');
                        if(type=='Monthly Escrow Statement'){
                            D.Type__c = 'Monthly Escrow Statement';
                        }

                        insert D;

                        return '1'; // PDF generated and submited for review successfully!
                    }
                }
            }
        }
        catch(Exception e)
        {
            //if exception found
            return e.getMessage();
        }
        return null;
    }
}
  • what is the line number? – Santanu Boral Jan 18 '18 at 5:49
  • @SantanuBoral, I only see the error in Apex Jobs. What should i do to get detailed error (line number)? – Febrian Tarigan Jan 18 '18 at 5:54
4

The exception occurs here:

body = pdf.getContent();

pdf is currently null because you are checking if the type value is 'Monthly Escrow Statement'.

//Parameter initialisation
PageReference pdf;
if(type=='Monthly Escrow Statement'){
    pdf = Page.MonthlyEscrowStatement;
    pdf.getParameters().put('id',ContactID);
}

You are setting the type to 'Escrow Monthly Statement' instead of 'Monthly Escrow Statement' in your batch class:

GenerateAndSubmitForApproval.GenerateAndSubmit(con.id, 'EscrowStatement', 'Escrow Monthly Statement', null);

This is a good example of when to use Constants or Custom Labels to prevent errors like this from occuring.

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.