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
noob123noob123 

Trouble referencing fields from a Contact

Two minor hurdles between me and my first functioning Visualforce mini-app, both related issues.

 

Within my extension class for a Contact controller I am trying to programmatically create a custom object that is related to a Contact. I am creating the custom object and then trying to populate it with certain values from the Contact record. It works for some fields, but not others.  I am trying to populate the Name of the object and a date field, like so:

 

 In my constructor I have:

 

CustomObject__c  customobj = new CustomObject__c();

contact = (Contact)controller.getRecord();

 

 

In my method I have:

 

customobj.Name = 'Account: ' + contact.Account.Name;

customobj.Date__c = contact.Last_Move_Date__c;

 

The first line returns "Account: null" and the second just never updates the Date__c field at all.

 

Any thoughts? Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Are you querying for these fields on your page?  If not, the controller would not be retreiving them.

At a minimum, even if you don't display them on the page, at least query them into a hidden field

 

<apex:inputHidden value="{!contact.Account.Name}" id="theAcctName"/>
<apex:inputHidden value="{!contact.Last_Move_Date__c}" id="theMvDate"/>

 

 

 Otherwise, you will need to do a second query for the fields you want, referecing theprimary contact's id as a filter.

 

Private Contact extContact = [select id,Last_Move_date__c,Account.name from Contact where id =:contact.id];//where contact.id is from your controller getrecord

 

 

 

Message Edited by JimRae on 02-18-2009 05:02 PM

All Answers

JimRaeJimRae

Are you querying for these fields on your page?  If not, the controller would not be retreiving them.

At a minimum, even if you don't display them on the page, at least query them into a hidden field

 

<apex:inputHidden value="{!contact.Account.Name}" id="theAcctName"/>
<apex:inputHidden value="{!contact.Last_Move_Date__c}" id="theMvDate"/>

 

 

 Otherwise, you will need to do a second query for the fields you want, referecing theprimary contact's id as a filter.

 

Private Contact extContact = [select id,Last_Move_date__c,Account.name from Contact where id =:contact.id];//where contact.id is from your controller getrecord

 

 

 

Message Edited by JimRae on 02-18-2009 05:02 PM
This was selected as the best answer
noob123noob123

Thanks for the quick reply, Jim.

 

That solved my issue for the Account.Name field, but now it throws an error on the second line is throwing an error.

 

System.DmlException: Update failed. First exception on row 0 with id 0038000000YJQKRAA5; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Last Move Date: value not of required type: Mon Feb 16 00:00:00 GMT 2009: [Last_Move_Date__c]

 

Both fields are standard "Date" Fields.  Any suggestions? I tried your second approach as well and still nothing.

JimRaeJimRae
Based on the error, it appears that one of the dates is a "date" type and the other is a "datetime" type.  Double check the field type on both objects.  If you need them to be the same, and can change one, that is fine, otherwise, if you actually need them to be different, you will need to convert one to the other.
noob123noob123
I just verified that they are both Date fields.  I had a formula of "Today()" in one of them, but even after removing that, clearing out the values and reinserting new values to be sure, the error still occurs.
JimRaeJimRae

I am not sure what else it could be, unless that field is null for some reason.

You could try using system.debug statements to see what the contact record looks like, and how the date field is formatted.

Put something like this above where you are inserting the date value and open the system log when you execute your page.

 

 

system.debug('n\n Contact: '+contact);

 

You could post the log back so we could see what the contact record looks like, and that might spark some ideas.