You need to sign in to do that
Don't have an account?
sandhubals
Fieldsets and VF Pages
I can't find a way to insert the field values of the parent object that are being used in the fieldset on the child object. Below is my code and I get the null values. I can only successfully insert the values in the Job object. Not sure if my approach is correct to use the parent object fields in the child fieldset.
public with sharing class fieldsetJobApplication
{
// Job object has a lookup to the Candidate object
// The fieldset is on the Job object and is referring few fields on the Candidate object
// From the VF page on insert the values into the Job object are inserted fine but the fieldset values on the Candidate object remain null
public Job__c jA {get;set;}
public Candidate__c jC {get;set;}
public fieldsetJobApplication()
{
jA = new Job__c();
jC = new Candidate__c();
}
public PageReference save()
{
assignValues();
// Insert Candidate record
insert jC;
// Provide the parent Id to child record for reference
jA.Candidate__c = jC.Id;
// All below show as null values on System.debug
System.debug('jA.Interviewer__c : ' + jA.Candidate__r.Interviewer__c);
System.debug('jA.Start_Date__c : ' + jA.Candidate__r.Start_Date__c);
System.debug('jA.Education__c : ' + jA.Candidate__r.Education__c);
System.debug('jC.Interviewer__c : ' + jC.Interviewer__c);
System.debug('jC.Start_Date__c : ' + jC.Start_Date__c);
System.debug('jC.Education__c : ' + jC.Education__c);
insert jA;
return new pagereference('/'+jA.id);
}
public List getFldsJA()
{
return SObjectType.Job__c.FieldSets.JA_Fieldset.getFields();
}
}
// VF Markup
<apex:page controller="fieldsetJobApplication">
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Job Application">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Job Application and Candidate Fields" >
<apex:repeat value="{!FldsJA}" var="j">
<apex:inputField value="{!jA[j]}" />
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class fieldsetJobApplication
{
// Job object has a lookup to the Candidate object
// The fieldset is on the Job object and is referring few fields on the Candidate object
// From the VF page on insert the values into the Job object are inserted fine but the fieldset values on the Candidate object remain null
public Job__c jA {get;set;}
public Candidate__c jC {get;set;}
public fieldsetJobApplication()
{
jA = new Job__c();
jC = new Candidate__c();
}
public PageReference save()
{
assignValues();
// Insert Candidate record
insert jC;
// Provide the parent Id to child record for reference
jA.Candidate__c = jC.Id;
// All below show as null values on System.debug
System.debug('jA.Interviewer__c : ' + jA.Candidate__r.Interviewer__c);
System.debug('jA.Start_Date__c : ' + jA.Candidate__r.Start_Date__c);
System.debug('jA.Education__c : ' + jA.Candidate__r.Education__c);
System.debug('jC.Interviewer__c : ' + jC.Interviewer__c);
System.debug('jC.Start_Date__c : ' + jC.Start_Date__c);
System.debug('jC.Education__c : ' + jC.Education__c);
insert jA;
return new pagereference('/'+jA.id);
}
public List getFldsJA()
{
return SObjectType.Job__c.FieldSets.JA_Fieldset.getFields();
}
}
// VF Markup
<apex:page controller="fieldsetJobApplication">
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Job Application">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Job Application and Candidate Fields" >
<apex:repeat value="{!FldsJA}" var="j">
<apex:inputField value="{!jA[j]}" />
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
If it answers to your problem then Please mark this accepted answer so it benifits others.
All Answers
In that case the fields for the related object fields should not be contained within the fieldset of the child object. That being said, that defeats the purpose of having the relational fields (parent fields) in the child object fieldsets if we can't bind/save them from the child fieldsets using VF/Apex.
So does that mean, have the fieldset created with fields of respective object only and then bind the parent and child input object fields separately.
If it answers to your problem then Please mark this accepted answer so it benifits others.
Could not resolve field 'Job_Comments__c' from <apex:inputField> value binding '{!jC[SUBSTITUTE(j,'Candidate__r','')]}' in page
Job_comments__c is the field in the fieldset of the child object. Do we need to put the conditional if for the Substitute as well?
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Job Application">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Job Application and Candidate Fields" >
<apex:repeat value="{!FldsJA}" var="j">
<apex:inputField value="{!jA[j]}" rendered="{!IF(NOT(CONTAINS(j,'__r')), true, false)}"/>
<apex:inputField value="{!jC[SUBSTITUTE(j,'Candidate__r','')]}" rendered="{!IF(CONTAINS(j,'__r'), false, true)}"/>
</apex:repeat>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
{
public SFDC_Job__c jA {get;set;}
public SFDC_Candidate__c jC {get;set;}
public fieldsetJobApplication()
{
jA = new SFDC_Job__c();
jC = new SFDC_Candidate__c();
}
public List<Schema.FieldSetMember> getFldsJA()
{
return SObjectType.SFDC_Job__c.FieldSets.JA_Fieldset.getFields();
}
public PageReference save()
{
insert jC;
jA.Candidate__c = jC.Id;
insert jA;
return new pagereference('/'+jA.id);
}
}
<apex:inputField value="{!jC[IF(CONTAINS(j,'__r'), SUBSTITUTE(j,'SFDC_Candidate__r.',''), j)]}" rendered="{!IF(CONTAINS(j,'__r'), false, true)}"/>
API Names for Candidate and Job are:
SFDC_Candidate__c
SFDC_Job__c
Use apex:outputField in VFP to see the values and then substitute accordingly.