dongtidai6519 2017-10-04 23:04
浏览 190
已采纳

尝试取消引用空对象Salesforce Visualforce Page

I have a PHP site that calls a REST end point on Salesforce via a visualforce page. When trying to validate the JSON response I confirmed all required fields but am now getting this error:

{"success":false, "message":"System.NullPointerException: Attempt to de-reference a null object

This is coming from the following Apex Class:

public class WebDirectController {

public class ApiException extends Exception {}

public String response {get;set;}

private static final String PRIVATE_APPLICATION_KEY = '123';

public WebDirectController(){}

public Pagereference safeAction(){

    Savepoint sp = Database.setSavepoint();

    String businessUnit;

    try {

        Map<String, String> params = ApexPages.currentPage().getParameters();

        for (String key : params.keySet()){
            system.debug(key +' -> '+ params.get(key));

        }

        if (params.containsKey('Business_Unit__c') == false){
            throw new ApiException('Unauthorized!');
        }

        businessUnit = params.get('Business_Unit__c');

        if (params.containsKey('applicationKey') == false){
            throw new ApiException('Unauthorized!');
        }
        if (params.get('applicationKey') != PRIVATE_APPLICATION_KEY){
            throw new ApiException('Unauthorized!');
        }

        // throws ApiException
        validateRequiredFields(params);


        WebDirectUtility utility = new WebDirectUtility(businessUnit);

        utility.duplicateCheck(params);

        if (utility.duplicateAccountList.isEmpty() == false){

            // if last opp is needs RX attach attachments to opp and flag opportunity

            if (utility.duplicateAccountList.get(0).Opportunities.isEmpty() == false 
                && utility.duplicateAccountList.get(0).Opportunities.get(0).Status__c == 'Needs Rx' 
                && utility.hasAttachments(params) == true){

                // save attachment.

                utility.createAttachmentList(params);

                utility.addAttachmentParentId(utility.duplicateAccountList.get(0).Opportunities.get(0).Id);

                insert utility.attachmentList;

                // set status to  'Attention- Call Center'

                Opportunity opp = utility.duplicateAccountList.get(0).Opportunities.get(0);

                opp.Status__c = 'Attention- Call Center';

                update opp;

                //utility.sendEmail(params, utility.duplicateAccountList.get(0), opp);

                response = '{"success":true}';
                return null;

            }else{

                //utility.sendEmail(params, utility.duplicateAccountList.get(0));
                response = '{"success":true}';
                return null;
            }
        }

        utility.createAll(params);



        insert utility.patientAccount;

        utility.addAttachmentParentId(utility.patientAccount.Id);
        //system.debug('attachmentList -> '+ utility.attachmentList);
        insert utility.attachmentList;

        utility.addOpportunityAccountId(utility.patientAccount.Id);
        //system.debug('patientOpportunity -> '+ utility.patientOpportunity);
        insert utility.patientOpportunity;

        utility.addLineItemOpportunityId(utility.patientOpportunity.Id);
        //system.debug('lineItemList -> '+ utility.lineItemList);
        insert utility.lineItemList;


        //send success email to kelly
        Opportunity opp = [SELECT Opportunity_Url__c FROM Opportunity WHERE Id =:utility.patientOpportunity.Id];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setSubject(businessUnit + ' Direct Success Email');
  mail.setHtmlBody('<p>'+ businessUnit +' Direct created this opportunity: <a href="'+ opp.Opportunity_Url__c +'">'+ opp.Opportunity_Url__c +'</a></p>');
  mail.setToAddresses(new String[]{'pedrick@symplmed.com', 'childs@symplmed.com'});
        Messaging.sendEmail(new Messaging.Email[] {mail});


        response = '{"success":true}';

    }catch(Exception e){

      ErrorEmail.sendEmail(e);

        system.debug(e);
        system.debug(e.getMessage());
        system.debug(e.getStackTraceString());

        response = '{"success":false, "message":"'+ e.getMessage() +'"}';

        Database.rollback(sp); 
    }
    return null;
}


public void validateRequiredFields(Map<String, String> params){

    Set<String> requiredParams = new Set<String>{'FirstName',
                                                 'LastName',
                                                 'PersonBirthdate',
                                                 'Phone',
                                                 'PersonEmail',
                                                 'BillingStreet',
                                                 'BillingCity',
                                                 'BillingState',
                                                 'BillingPostalCode'};

    for (String rp :requiredParams){
        if (params.get(rp) == '' || params.get(rp) == null){
            throw new ApiException(rp +' is required!');
        }
    }
}

}

Not sure what I am missing but I am going on 2 weeks of trying to figure this out.

Thank You

  • 写回答

2条回答 默认 最新

  • dongliufa6380 2017-10-06 17:49
    关注

    I wanted to close the loop on this. By adding the stack track to the debug msg I was able to locate an error in the setup that resolved the Apex class issue, The product line item entry in the Utility class was pointing to the wrong pricebook.

    Adding the Stack Trace to the debug msg was the trick.

    Thank you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作