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
mmaxtrammaxtra 

Attempt to de-reference a null object???Please help

Hi all:

Happy Holidays...

 

Basically something is wrong with my URL redirect that I keep getting the following error:

Attempt to de-reference a null object

 

 

public class newoppbutton { Contact contact{get;set;} public newoppbutton(ApexPages.StandardController stdController){ } public PageReference init() { String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; } else { // redirectUrl = '/006/e?nooverride=1'; redirectUrl = '/006/e?CF00N70000002RNNE='+contact.Name+'&nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } } }

 Could someone tell me wz up with my code...or the correct URL to use for an user to go to a regular Opp page with the contact name filled in?

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

OK,
I haven't tried this before, but it looks like we can grab the retURL parameter to see what the calling object was, worked in my test cases anyway.

 

Now my code looks like this, see if it will work for you:

 

 

public class newoppbutton { Contact mycontact{get;set;} boolean gotcontact=false; public newoppbutton(ApexPages.StandardController stdController){ string ret=ApexPages.currentPage().getParameters().get('RetURL'); ret=ret.substring(1,ret.length()); if(ret.startswith('003')){ try{ mycontact = [select id,name from contact where id=:ret]; gotcontact=true; }catch(QueryException q){ system.debug(q.getmessage()); } } } public PageReference init() { String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; }else if(gotcontact) { redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1'; }else{ redirectUrl = '/006/e?nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } }

 

 

 

All Answers

VornoVorno

On first glance, contact.Name is the error.

 

You're not populating the contact variable, and it would be null when you attempt to read the Name property from it.

mmaxtrammaxtra

Thank you for replying back to me... How do I populate the contact ?

The user is doing this from the contact record clicking on New Opportunity.

 

So basically I am overriding the new button on the Opp with a VF page... But I need to carry the contact Name over to Opportunity custom Contact field...

 

Thanks

JimRaeJimRae

As Vorno mentioned, you are not populating the Contact record for use in the code.

You need to do something like this:

 

 

public class newoppbutton { Contact mycontact{get;set;} public newoppbutton(ApexPages.StandardController stdController){ mycontact=(Contact)stdController.getRecord(); } public PageReference init() { String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; } else { // redirectUrl = '/006/e?nooverride=1'; redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } } }

 

 I would also advise you to be careful creating a variable name the same as an object name, this can cause scoping issues, and sometimes difficult to debug problems.  I changed it in my example to mycontact for clarity sake.

 

MMA_FORCEMMA_FORCE

Thank you so much for helping me out...

When I do that I now get:

Invalid conversion from runtime type SOBJECT:Opportunity to SOBJECT:Contact

An unexpected error has occurred. Your development organization has been notified.

 

Which I do not understand cause I never seen this error before...

I am overriding the new button for Opportuniyt...

 

Creating the new Opportunity off the contact record... from the related list within Contact...

 

Weird..

Any suggestions?

 

Thanks

 

 

 

JimRaeJimRae

Would you post your VF page code also?

Not sure I understand how your Recordtype is getting passed in the URL.

Also, if you are going to override your New button, this will override it from every possible New Opportunity creation, including the sidebar, and any related list, view, etc.

 

You will need to code for the fact that you might not know who the contact is, depending on where you call the New Opportunity functionality.

MMA_FORCEMMA_FORCE

Yes I know that the New button will be overriden all over. The education is to always create new Opp through Contact.

 

<apex:page standardController="Opportunity" extensions="newoppbutton" action="{!init}"> </apex:page>

 The thing is I commented out the recordtype just to see if the link would work and it still came up with the same error:

 

public class newoppbutton { Contact mycontact{get;set;} public newoppbutton(ApexPages.StandardController stdController){ mycontact=(Contact)stdController.getRecord(); } public PageReference init() { String redirectUrl = ''; redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1'; PageReference newOpp = new PageReference(redirectUrl); return newOpp; } }

 

 Thanks

 

 

 

JimRaeJimRae

You are going to need to figure out where you are calling the code from, so you can figure out if the contact is available or not to leverage.

Not sure exactly how to do that, but am interested in finding out.

Sorry I couldn't help more.

MMA_FORCEMMA_FORCE

yeah.. no problem but thank you so much for brainstorming with me... Happy Holidays...

This is a weird scenario because:

in the URL the contact shows up but it gives that error hence not placing the opportunity page...

 

https://c.cs0.visual.force.com/apex/newbutopp?CF00N70000002RNO0=Ray+Coons&CF00N70000002RNO0_lkid=003T000000MEhfy&scontrolCaching=1&retURL=%2F003T000000MEhfy&sfdc.override=1

 

Weird...

MMA_FORCEMMA_FORCE

Hi Happy Holidays:

Would anyone tell me what is wrong with my URL or code...

 https://c.cs0.visual.force.com/apex/newbutopp?CF00

N70000002RNO0=Ray+Coons&CF00N70000002RNO0_lkid=003T000000MEhfy&scontrolCaching=1&retURL=%2F003T000000MEhfy&sfdc.override=1

 

 I keep getting this

Invalid conversion from runtime type SOBJECT:smileysurprised:pportunity to SOBJECT:Contact

An unexpected error has occurred. Your development organization has been notified.

 

JimRaeJimRae

It is in the code, The constructor you are using,

 

public newoppbutton(ApexPages.StandardController stdController){ mycontact=(Contact)stdController.getRecord(); }

 

 Won't work for this example.  When I sent that example, it was before I understood you were trying to override the new button.

 

You will need to take out the getRecord method call, as I mentioned in my previous post, you will need to determine where you are calling the page from, and then use that information to determine if you can get a contact name/ID or not.

 

That is definately what is causing your Invalid conversion error to occur.

 

JimRaeJimRae

OK,
I haven't tried this before, but it looks like we can grab the retURL parameter to see what the calling object was, worked in my test cases anyway.

 

Now my code looks like this, see if it will work for you:

 

 

public class newoppbutton { Contact mycontact{get;set;} boolean gotcontact=false; public newoppbutton(ApexPages.StandardController stdController){ string ret=ApexPages.currentPage().getParameters().get('RetURL'); ret=ret.substring(1,ret.length()); if(ret.startswith('003')){ try{ mycontact = [select id,name from contact where id=:ret]; gotcontact=true; }catch(QueryException q){ system.debug(q.getmessage()); } } } public PageReference init() { String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; }else if(gotcontact) { redirectUrl = '/006/e?CF00N70000002RNNE='+mycontact.Name+'&nooverride=1'; }else{ redirectUrl = '/006/e?nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } }

 

 

 

This was selected as the best answer
MMA_FORCEMMA_FORCE

Awesome... I wish I saw this before I wrote this.. but I used the same parameter as you did...

 

public class newoppbutton { String retURL; String oppId; public newoppbutton(ApexPages.StandardController stdController){ } public PageReference init() { retURL= ApexPages.currentPage().getParameters().get('retURL'); oppId = retURL.substring(1); Contact c = [Select lastname,firstname from Contact where id =:oppId]; String redirectUrl = ''; String recordTypeId = System.currentPageReference().getParameters().get('RecordType'); if (recordTypeId != null) { redirectUrl = '/006/e?retURL=/006/o&RecordType=' + recordTypeId + '&nooverride=1'; } else { redirectUrl = '/006/e?CF00N70000002RNNE='+c.firstname+'+'+c.lastname+'&nooverride=1'; } PageReference newOpp = new PageReference(redirectUrl); return newOpp; } } }

 but cool...

 

 

mmaxtrammaxtra

Hi:

   Does anyone have an example of how to write a test class for the above code...

I do not know how to write testmethods for URL's and get there value from it ?

Thanks

 

mmaxtrammaxtra

Well I started the Testmethod... which is in another board...

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=23828&jump=true#M23828

 

 

Any help would be appreciated thanks

Happy Holidays...