function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MRutterMRutter 

Problem showing detail page for a newly created contact

I am using VisualForce pages and Apex to convert a spouse to a contact.  A new button was added to the Contacts detail pages to call the VF page.  All is going fairly well, but I cannot get the detail page of the new contact to show.

 

Part of my custom controller code is shown below and is based on the 'The Opportunity Wizard Controller' section in the VF developers guide to show the detail page of the newly created contact.

 

//Save the new contact public PageReference NContact(){ Contact newcontact = new contact (LastName = SLNAME); insert newcontact; PageReference contactPage = new ApexPages.StandardController(contact).view(); contactPage.setRedirect(true); return contactPage;

I get the following error message:

System.NullPointerException: Argument 1 cannot be null

The bold line above is referenced.

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

Just a small thing really 

 

//Save the new contactpublic PageReference NContact(){ Contact newcontact = new contact (LastName = SLNAME); insert newcontact; PageReference contactPage = new ApexPages.StandardController(newcontact).view(); contactPage.setRedirect(true); 

return contactPage; 

 

I've never used the standard controller in this manner though. If you're still having issues I *think* this will work

 

PageReference contactpage = new PageReference(newcontact.id); 

 

Cheers,

Wes 

All Answers

wesnoltewesnolte

Hey

 

Just a small thing really 

 

//Save the new contactpublic PageReference NContact(){ Contact newcontact = new contact (LastName = SLNAME); insert newcontact; PageReference contactPage = new ApexPages.StandardController(newcontact).view(); contactPage.setRedirect(true); 

return contactPage; 

 

I've never used the standard controller in this manner though. If you're still having issues I *think* this will work

 

PageReference contactpage = new PageReference(newcontact.id); 

 

Cheers,

Wes 

This was selected as the best answer
MRutterMRutter

Thanks, Wes.  That worked.  It sure is hard for me to find this stuff out from the documentation.

 

Two solutions worked for me.  Any comments on which might be preferred:

 

Solution 1:   

PageReference contactPage = new ApexPages.StandardController(newcontact).view();

 

Solution 2: 

PageReference contactPage = new PageReference('/' + newcontact.id);

 

Mark

wesnoltewesnolte

I've never done it the first way before but to me it seems the best way.. simply because it's cleaner than my way.

 

There are cases in which the second method would be better e.g. you want to direct the user to a visualforce page that you've made, and need to specify parameters dynamically.

 

Thanks for teaching me something:)

 

Wes