尝试取消引用空对象Salesforce Visual Force Page

时间:2017-10-04 23:04:08

标签: php salesforce visualforce

我有一个PHP站点,通过visualforce页面调用Salesforce上的REST端点。在尝试验证JSON响应时,我确认了所有必填字段,但现在收到此错误:

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

这来自以下Apex类:

public class WebDirectController {

public class ApiException扩展了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!');
        }
    }
}

}

不确定我错过了什么,但是我要花2周的时间来解决这个问题。

谢谢

2 个答案:

答案 0 :(得分:1)

我想在此关闭循环。通过将堆栈轨道添加到调试消息,我能够在解决Apex类问题的设置中找到错误,Utility类中的产品系列条目指向错误的价格手册。

将堆栈跟踪添加到调试消息是一个绝招。

谢谢

答案 1 :(得分:0)

以您在PHP应用程序中使用其凭据的用户身份登录SF。打开Developer Console(右上角,用户名旁边),执行PHP调用。理想情况下,您会看到生成的调试日志包含更详细的错误消息,包括行号和序列号。栈跟踪。

或者 - 转到设置 - &gt;调试日志并启用对该集成用户的跟踪(这将为您提供可以查看和下载的文件;开发者控制台有它的怪癖,可能会很慢,但至少它会为您解析日志,所以它更容易阅读,你可以设置检查点......)

很难说可能出现什么问题,你可以在堆栈跟踪中获得更多好运。我的猜测是utility.duplicateAccountList为空,然后如果您的代码执行null.isEmpty(),您显然会被错误打错;)

P.S。您是否考虑过将您的Apex暴露为正确的REST服务,而不是VF页面?