You need to sign in to do that
Don't have an account?
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!
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
All Answers
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
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.
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);