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
ESTEST 

Defaulting in values from a parent object into a newly created child object

Hello, Forum:

 

I've read various posts around the forums concerning the defaulting in of values from a parent object into a newly created child object.  But I still don't see a solution out there.

 

I've created a VF page.  Using a topmost pageBlock, I've created a sort of boilerplate region where certain key information from the parent object is displayed.  This is a common technique, and it serves to orient the user in the child object with respect to the parent object.

 

But I'm having trouble implementing this technique.  It seems that newly created child objects do not have access to parent object values.  This is absolutely essential.  I need to fill in certain child fields based on values in the parent.  For example, in the boilerplate region, I need to display the project name of the parent object, not just its name.  The bid due date and time on the parent object needs to default in as the bid due date and time for the child object, etc.

 

Is there a way to accomplish this with JavaScript?  If we can't accomplish this, it would be a dealbreaker for us with SF.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
bmabma

In this case, you would need to override the "New" button of your child object with a VF page.

 

You would need to specify which fields you want on the page and etc. Here is a sample page example:

 

<apex:page standardController="child__c" extensions="childExtension" > <apex:form > <apex:pageBlock title="Child"> <apex:pageBlockButtons> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageblockButtons> <apex:pageblockSection > <!-- list other fields you want user to input data --> <apex:inputField value="{!child__c.parent_value__c}"/> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>

 

You see from the sample above I used an apex extension. We want to copy the value from the parent object to the child object inside there.

 

 

public class childExtension { public childExtension(ApexPages.StandardController controller) { Child__c child = (Child__c)controller.getRecord(); //if you always create the child object through relatedList //parent__c should not be null if (child.parent__c != null) { Parent__c parent = [select value__c from parent__c where id =: child.parent__c limit 1]; child.parent_value__c = parent.value__c; } } }

 

 Let me know if this meet your requirement or not.

 

 

All Answers

SteveBowerSteveBower
May I suggest you post your VF code and Apex controller.  This should be very doable.  -S
bmabma

How is the child objects created? Also, are the fields you need from the parent object queried ahead of time?

 

If a field isn't queried or isn't used in conjunction with a standard controller, the data for the field usually isn't available.

 

If you can post a small sample of your page and controller, I maybe able to get it to work for you.

ESTEST

Hello bma,

 

The child object is created from a related list on the parent object's page.  This is the only way the child object can be created.

 

Also, I have no controller to post here for review because I haven't created a controller.  I'm still wrestling with when to use a controller, and when to simply use JavaScript within a script tag.  I was able to obtain the parent's value using JavaScript and getElementById(), but have had no luck inserting that value into the child's input field.

 

Should I be creating a custom controller or controller extension?  When you say "query ahead of time", would I be doing that in the controller, or in JavaScript?

 

I'm a C# developer, and I haven't found any of the programming concepts on the SF platform terribly challenging.  But I have found it a challenge to figure out when to use which technology--Apex, JavaScript, CSS, etc. 

 

Thank you. 

ESTEST

Hello Steve,

 

Please see my reply to bma.

 

Thanks. 

bmabma

Do you need the values:

(1) when you are creating the child object, before the object is saved to the database, or

- you would need to create a VF page that use an apex controller extension

 

 

(2) if the user didn't supply a value, you would default it to to the parent's value

- you would need to write an Apex trigger on the child object

 

 

trigger testParentField on child__c (before insert) { Set<Id> parentIds = new Set<Id>(); for (Child__c child :Trigger.new){ //only add to the list of query if we need to default the value if (child.parent_value__c == null) { parentIds.add(child.parent__c); } } //perform a query on the parent object and the needed fields Map<Id, Parent__c> parentObjects = new Map<Id, Parent__c>([select value__c from parent__c where id in :parentIds]); for (Child__c child : Trigger.new) { if (child.parent_value__c == null) { Parent__c parent = parentObjects.get(child.parent__c); child.parent_value__c = parent.value__c; } } }

 

"Query ahread of time" - meaning the value of a field wouldn't be available unless you query it. You can do it through Apex or API. You can see from the example I posted. I need to query the value before I can assign it.

 

 

ESTEST

bma,

 

Thanks for the reply.  Sorry for the delay in getting back to you.

 

The problem with the approach you suggest is that the before insert trigger doesn't fire at the time necessary to default in a value in a meaningful way.  This trigger after the user clicks Save, but before the record is committed.  I need the user to see the default value right after the page loads.  What we need is an after load trigger.

 

Otherwise, here's what happens.  Our spec requires that we make the child object's due date and time required.  Upon creation of the new child, the bid due date and time field is blank.  The user tries to save, but the record fails validation for lack of a required field.  The user then proceeds to enter the child object's due date and time, but 80% of the time, this value is the same as that of the parent (the parent's due date and time).   If we make the due date and time a non-required field, let the user leave it blank, and then, using your suggestion to fill in the value upon saving (or using a workflow), the user may still think he has to fill in the due date and time.

 

Basically, we would like to handle default values the way every other platform handles them: upon loading the page or form or whatever the object is called for the platform.

 

Thank you. 

Rajesh ShahRajesh Shah

Hi,

 

I am assuming that when a user clicks on Create New Child Record button from Related list, he is taken to Visualforce page and user sees some values from the parent record on this page. When the record is saved, those details are also updated in child records. If this the case, you can do the following:

  1. Override the list button with Visualforce page.
  2. Pass in the parent id to the Visualforce page.
  3. In the controller, you can access the url parameters. Get the parent id, query the required fields of the parent in the constructor and display the details in the visualforce page.
  4. Optionally if you want to save the parent details in child records too, then write a custom save, which populates the child record with details from parent.  
bmabma

In this case, you would need to override the "New" button of your child object with a VF page.

 

You would need to specify which fields you want on the page and etc. Here is a sample page example:

 

<apex:page standardController="child__c" extensions="childExtension" > <apex:form > <apex:pageBlock title="Child"> <apex:pageBlockButtons> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageblockButtons> <apex:pageblockSection > <!-- list other fields you want user to input data --> <apex:inputField value="{!child__c.parent_value__c}"/> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>

 

You see from the sample above I used an apex extension. We want to copy the value from the parent object to the child object inside there.

 

 

public class childExtension { public childExtension(ApexPages.StandardController controller) { Child__c child = (Child__c)controller.getRecord(); //if you always create the child object through relatedList //parent__c should not be null if (child.parent__c != null) { Parent__c parent = [select value__c from parent__c where id =: child.parent__c limit 1]; child.parent_value__c = parent.value__c; } } }

 

 Let me know if this meet your requirement or not.

 

 

This was selected as the best answer
ESTEST

bma,

 

That's awesome!  That's exactly how I saw the logic in my head, but was at a loss as to how to put the pieces together.

 

Thank you!

RailCIORailCIO

Located this discussion and having a difficult time trying to replicate this in an actual environment. 

 

Actually trying to default values from both the standard object Account and Contracts in a common form that is in a Master Detail relationship with the Contract object. 

 

The issue is trying to construct the query to pull the proper data from the Contract(Master) and then to pull from the common referenced Account.  

 

I am not certain what to compare in the Contract(Master) to the Child record.  I have tried reference to the Contract ID, Order Number to the Child value used to define the controller.   Tried all the variants and I am sure it is probably something easy. teh System Debug statement show the Child value of the Order_Number__C which is the common field for the Master-Detail definition.

 

 

Statements appears as follows:

 

 public class ChildExtension {
           
    Account Account{get;set;}
    Contract Contract {get;set;}
   
    public ChildExtension(ApexPages.StandardController controller) {
        Child__C c  = (Child__c)controller.getRecord();
   
        system.debug(Child); 
       
        Contract C = [select Name, id, StartDate, EndDate, contractNumber
                      from Contract where contractNumber = C.Order_Number__C];

}

Madhuri TankMadhuri Tank
@ bma,
Your answer helped me a lot.
Thank you.