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条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来