+ Start a Discussion
jd123jd123 

How can we add fields dynamically to the child Object based on parent object input?

Hi Everyone,

my requirement is i have two objects, 1.Stattfing_Plan (Parent)     2. Staffing_Resorces__c(Child)  

Staffing plan is estimation to complete the Project (project is have only one Staffing plan).Staffing plan have multiple Staffing resources (Employees).

 

if suppose in my parent object (Staffing_Plan__c) i have given Start_Date__c 11/26/2012 and  weeks is 5.

then for all recosures dynamically i should add 5 input text or fields dynamically for all child records fo Staffing_Pan__c  

like this

 

 Staffing_Pan__c

 

Start_date__c -----11/26/2012

weeks---------------3

 

child records  (page block table)

----------------

                               

Name            11/26/2012       12/04/2012       12/11/2012

--------------------------------------------------------------------------------------------------------------------------------------------------------

mahender     inputtext              inputtext             inputtext 

surender        inputtext             inputtext             inputtext 

nareder            inputtext          inputtext            inputtext 

 

 

like it should create and when i am saving these values it should save in the database.

 

 

------------
Mahi
Jake GmerekJake Gmerek

There are several ways to approach this problem.  Here are a few:

 

The easy, but slightly less elegent, but supported by Salesforce way:  Create another custom object, say staffing_resources_weeks__c and write a trigger to insert the appropriate number of weeks upon the creation of each staffing_resource_record.  You could then create a custom VF page for the staffing resource that would display the data the way that you expect.

 

Another way:  Hack the salesforce METADATA API WSDL so that you can load it into APEX (this is not supported natively by Salesforce).  I have never done it, but I have seen people claim this is possible.  Then use it like you want to and add the fields dynamically.

 

Finally, add 52 "week" fields to the staffing_resource__c object.  Create a custom VD page that displays the correct number of weeks and a trigger that populates the fields correctly.  A limitation on this is that  a project could take at most one year.  Longer projects would have to be split up.

 

I can give you more information on any of these options if you are interested.

jd123jd123

Hi Jake,

 

Really very good expalanationThanks Jake.

 

    I think the third one is easy to implement it.Would you please send me some sample code how can i do it but it won't support if weeks are more than 52.

 

ca you send some sample code for frist one also.

Jake GmerekJake Gmerek

So even the third method requires a bit of coding but here is the idea:

 

Apex:

 

Public class myController{

 

 

  boolean showField {get; set;}

 

  public myController(){

     showField = True;

  }

 

}

 

 

<apex:Page controller = myController>

 

  <apex:outputField value = {!myObj.myField1__c}  rendered = {!showField} />

....

 

 

So there has to be alot more there obviously, but that should give you an idea on how to go forward.  As goes the showField value, then that field will be displayed or not displayed.  You could do it manually by having a boolean for each field, but a slicker way would be to use getDescribe to get the field names and use a comparison to show or not shoe the field.  Let me know if you have specific questions on this.