尝试取消引用空对象 Salesforce Visual Force Page
Attempt to de-reference a null object Salesforce Visual Force Page
我有一个 PHP 站点,它通过 visualforce 页面调用 Salesforce 上的 REST 端点。在尝试验证 JSON 响应时,我确认了所有必填字段,但现在收到此错误:
{"success":false, "message":"System.NullPointerException: Attempt to de-reference a null object
这来自以下 Apex Class:
public class WebDirectController {
public class ApiException 扩展异常 {}
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 周时间尝试解决这个问题。
谢谢
以您在 PHP 应用程序中使用其凭据的用户身份登录 SF。打开开发者控制台(右上角,您的用户名旁边),执行来自 PHP 的调用。理想情况下,您会看到生成的调试日志,其中包含更详细的错误消息,包括行号和堆栈跟踪。
或者 - 转到设置 -> 调试日志并启用该集成用户的跟踪(这将为您提供可以查看和下载的文件;开发者控制台有它的怪癖,可能会很慢,但至少它会解析为您准备的日志,这样更容易阅读,您可以在其中设置检查点...)
很难说出了什么问题,堆栈跟踪会让你更幸运。我的疯狂猜测是 utility.duplicateAccountList
为空,然后如果您的代码执行 null.isEmpty()
,您显然会遇到错误 ;)
P.S。您是否考虑过将您的 Apex 部分公开为适当的 'REST service',而不是 VF 页面?
我想结束这个循环。通过将堆栈轨道添加到调试消息中,我能够在解决 Apex class 问题的设置中找到错误,实用程序 class 中的产品行项目条目指向错误的价格手册。
将 Stack Trace 添加到调试消息中就是诀窍。
谢谢
我有一个 PHP 站点,它通过 visualforce 页面调用 Salesforce 上的 REST 端点。在尝试验证 JSON 响应时,我确认了所有必填字段,但现在收到此错误:
{"success":false, "message":"System.NullPointerException: Attempt to de-reference a null object
这来自以下 Apex Class:
public class WebDirectController {
public class ApiException 扩展异常 {}
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 周时间尝试解决这个问题。
谢谢
以您在 PHP 应用程序中使用其凭据的用户身份登录 SF。打开开发者控制台(右上角,您的用户名旁边),执行来自 PHP 的调用。理想情况下,您会看到生成的调试日志,其中包含更详细的错误消息,包括行号和堆栈跟踪。
或者 - 转到设置 -> 调试日志并启用该集成用户的跟踪(这将为您提供可以查看和下载的文件;开发者控制台有它的怪癖,可能会很慢,但至少它会解析为您准备的日志,这样更容易阅读,您可以在其中设置检查点...)
很难说出了什么问题,堆栈跟踪会让你更幸运。我的疯狂猜测是 utility.duplicateAccountList
为空,然后如果您的代码执行 null.isEmpty()
,您显然会遇到错误 ;)
P.S。您是否考虑过将您的 Apex 部分公开为适当的 'REST service',而不是 VF 页面?
我想结束这个循环。通过将堆栈轨道添加到调试消息中,我能够在解决 Apex class 问题的设置中找到错误,实用程序 class 中的产品行项目条目指向错误的价格手册。
将 Stack Trace 添加到调试消息中就是诀窍。
谢谢