You need to sign in to do that
Don't have an account?
Pooja Nekkalapudi
how to solve this ??Attempt to de-reference a null object
Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page crosssellvf: Class.CrossSellRExtensionController.save: line 17, column 1
public class CrossSellRExtensionController {
public Account newPersonAccount {get; set;}
public CrossSellRExtensionController(ApexPages.StandardController controller) {
Cross_Sell_Opportunity__c csr = (Cross_Sell_Opportunity__c) controller.getRecord();
csr = [Select Id, Name from Cross_Sell_Opportunity__c where Id =: csr.Id];
RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
Account newPersonAccount = new Account();
newPersonAccount.Cross_Sell_Referral__c = csr.Id;
}
public PageReference save() {
insert newPersonAccount; //this is line 17
PageReference pageRef = new PageReference('/' + newPersonAccount.Cross_Sell_Referral__c);
pageRef.setRedirect(true);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'new *contact* created.'));
return pageRef;
}
public PageReference cancel() {
return new PageReference('/'+ newPersonAccount.Cross_Sell_Referral__c);
}
}
Error is in expression '{!save}' in component <apex:commandButton> in page crosssellvf: Class.CrossSellRExtensionController.save: line 17, column 1
public class CrossSellRExtensionController {
public Account newPersonAccount {get; set;}
public CrossSellRExtensionController(ApexPages.StandardController controller) {
Cross_Sell_Opportunity__c csr = (Cross_Sell_Opportunity__c) controller.getRecord();
csr = [Select Id, Name from Cross_Sell_Opportunity__c where Id =: csr.Id];
RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
Account newPersonAccount = new Account();
newPersonAccount.Cross_Sell_Referral__c = csr.Id;
}
public PageReference save() {
insert newPersonAccount; //this is line 17
PageReference pageRef = new PageReference('/' + newPersonAccount.Cross_Sell_Referral__c);
pageRef.setRedirect(true);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'new *contact* created.'));
return pageRef;
}
public PageReference cancel() {
return new PageReference('/'+ newPersonAccount.Cross_Sell_Referral__c);
}
}
All Answers
newPersonAccount = new Account();
You are createing new variable newPersonAccount with Account before that effectively making newPersonaccount in class and controller as different variables
Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]
Error is in expression '{!save}' in component <apex:commandButton> in page crosssellvf: Class.CrossSellRExtensionController.save: line 16, column 1
You will need to specify the firstname and lastname values on your person account before you insert it.
See this link for more details: http://help.salesforce.com/apex/HTViewSolution?id=000181428&language=en_US
Thanks,
Richard.
<apex:page standardcontroller="Cross_Sell_Opportunity__c" extensions="CrossSellRExtensionController">
<apex:sectionHeader title="Title" subtitle="Person Accounts"/>
<apex:form >
<apex:outputPanel >
</apex:outputPanel>
<apex:pageMessages />
<apex:pageBlock title="Please fill in the details for creating *Contacts*">
<apex:pageBlockSection >
<apex:inputField value="{!Cross_Sell_Opportunity__c.Name}" required="true"/><br/>
<apex:inputField value="{!newPersonAccount.Salutation}" />
<apex:inputField value="{!newPersonAccount.FirstName}" /><br/>
<apex:inputField value="{!newPersonAccount.LastName}" required="true"/><br/>
<apex:inputField value="{!newPersonAccount.Name}" required="true"/><br/>
<apex:inputField value="{!newPersonAccount.PersonEmail}" required="true"/><br/>
<apex:inputField value="{!newPersonAccount.Status__c}" /><br/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Even though I set input fields last name and first name it shows blank no way to edit
newPersonAccount.RecordTypeId = personAccountRecordType.Id;
Add this in controller and try
newPersonAccount.RecordTypeId = personAccountRecordType.Id;
newPersonAccount.RecordTypeId='012E0000000QtIm';
Have you looked into using Visual Flow to do this? - it feels like it would be a good use case. You will also find it easier for admins to use and configure. You can start your flow from a custom button or embed in a VF page.
https://www.youtube.com/watch?v=nkdc_133qHk
https://www.youtube.com/watch?v=R8fNHGWxW2I
Thanks,
Richard.