我有一個PHP站點通過Visualforce頁面調用Salesforce上的REST終端。當試圖驗證JSON響應我確認所有必填字段,但我現在收到此錯誤:嘗試解除引用空對象Salesforce Visual Force頁面
{"success":false, "message":"System.NullPointerException: Attempt to de-reference a null object
這是從下面的Apex類來:
public class WebDirectController {
公共類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[]{'[email protected]', '[email protected]'});
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!');
}
}
}
}
不知道我很想念,但我GOI試圖找出這兩個星期。
謝謝
標記你自己的答案被接受,你會得到一些聲譽:) – eyescream