Resubmitting to make this clearer. I have a parent object Claims_Client_Risk_Assessment__c (CCRA) that calls a Visualforce component page using the object CCRA_LOB_NonStdAuto__c (LOB_NonStdAuto.) I'm having an issue when setting the Master Detail field on the component's supporting object from the parent done in the SaveTestInsert() method. I think there is a fundamental issue I'm missing. When I just save the CCRA parent object via stdController.save() the CCRA.id remains null. If I add upcert CCRA; before the save the CCRA.id is exposed and I am able to pass this CCRA.id and CCRA.Name to the component object.
Is there an alternative way to pass the parent id to a component? Short code snippets and Visualforce component below.
Parent Visualforce call to add component:
<apex:tab label="NonStdAuto" name="tabLOB2" id="tabLOB2" rendered="{!IF(CONTAINS(Claims_Client_Risk_Assessment__c.Lines_of_Business__c,'NonStdAuto'),"true","false")}" >
<apex:outputPanel >
<c:PROD_CCRA_LOB_NonStdAuto LOB_NonStdAuto="{!LOB_NonStdAuto}" />
</apex:outputPanel>
Parent Controller: (short version.)
public class ClaimsClientRiskAssessmentController {
//Properties
//Parent and components
public Claims_Client_Risk_Assessment__c CCRA { get; set; }
public CCRA_LOB_NonStdAuto__c LOB_NonStdAuto { get; set; }
//Protected Members
private final ApexPages.StandardController stdController;
//Constructor
public ClaimsClientRiskAssessmentController(ApexPages.StandardController controller) {
try {
CCRA = new Claims_Client_Risk_Assessment__c();
this.stdController = controller;
this.CCRA = (Claims_Client_Risk_Assessment__c) controller.getRecord();
this.LOB_NonStdAuto = new CCRA_LOB_NonStdAuto__c(); //instatiate the LOB to receive parent ID
//build up LOB fields to pass to components.
this.LOB_NonStdAuto = queryLOB_NonStdAuto(this.CCRA.Id);
catch (Exception ex) {
ApexPages.addMessages(ex);
}
}
//Public Methods
//**********************************************************
Public Void SaveTestInsert() {
try {
upsert CCRA;
stdController.save();
AttachLOB();
} catch (Exception ex) {
ApexPages.addMessages(ex);
}
}
//**********************************************************
Public void AttachLOB() {
try {
LOB_NonStdAuto.Claims_Client_Risk_Assessment__c = CCRA.id;
LOB_NonStdAuto.Name = CCRA.Name;
Upsert LOB_NonStdAuto;
} catch (Exception ex) {
ApexPages.addMessages(ex);
}
}
//**********************************************************
public void getLOB_NonStdAuto() {
try {
this.LOB_NonStdAuto = queryLOB_NonStdAuto(this.CCRA.Id);
} catch (Exception ex) {
ApexPages.addMessages(ex);
}
}
//**********************************************************************
// used to populate Viusalforce component with fields.
Private CCRA_LOB_NonStdAuto__c queryLOB_NonStdAuto(Id CCRA_ID) {
LOB_NonStdAuto = [
SELECT
Id
,Claims_Client_Risk_Assessment__c
FROM
CCRA_LOB_NonStdAuto__c
WHERE
IsDeleted = false
AND
Claims_Client_Risk_Assessment__c = :CCRA_ID
ORDER BY CreatedDate DESC NULLS FIRST LIMIT 1
];
return LOB_NonStdAuto;
}
//**********************************************************************
} //End Class
Visualforce Component
<apex:component >
<!-- Attribute Definitions -->
<apex:attribute name="NonStdAuto" type="CCRA_LOB_NonStdAuto__c" description="CCRA non standard auto LOB" required="true"/>
<!--Contoller Information variables -->
<apex:variable var="LOB" value="{!NonStdAuto}" />
<apex:stylesheet value="{!URLFOR($Resource.cssresource_ClaimCCRA, 'CSSresource_ClaimsCCRA.css')}"/>
<apex:pageMessages id="globalMessages" rendered="false"/>
<apex:messages title="data incorrectly entered." style="background-color:yellow; color: blue" />
<apex:form id="form1">
<apex:pageBlock id="block1" dir="" >
<apex:actionRegion immediate="true" >
<apex:outputPanel id="OutputTabPanel">
<apex:outputPanel Style="width:100%;" layout="block">
<apex:pageBlockSection id="Summary" title="" columns="2" showHeader="false" >
<!-- ********* -->
//..... content
<!-- ********* -->
</apex:pageBlockSection>
</apex:outputPanel>
</apex:outputPanel>
</apex:actionRegion>
</apex:pageBlock>
</apex:form>
<!-- End -->
</apex:component>