+ Start a Discussion
jd123jd123 

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

Hi,

 

    I have to objects

 

Parent__c and Child__c

I have fields in my parent Start_Date__c and No_Of_Fields__c

if I am giving as input  No_Of_Fields__c=3 in my parent object Parent__c  then in my child object 3 fields should create dynamically and it should show in pageblock table like

 

Name, Start_Date(if today) , today+7, today+14 and when I am clicking save it should save in my child object.

 

If I am giving 4 or 5 those many fields should create in my child object  and values should save.

 

Any help is Appreciated.

PremanathPremanath

How can you create fields using code .

 

If you have created the fields in child already .. You can able to update those fields easily.

 

Write a trigger in Parent object .. Based on some conditions you can write your code .. 

 

 

Regards

Prem

jd123jd123

Ok i ll expain a little bit,

i think we can carete fields by using code,

 

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.

 

i think you understand otherwise please letme know.

PremanathPremanath

Your creating the fields in child object Dynamically Right?

 

I don't know how to create fields using code.

If fields are already exist we can push values into child record easily.. Sorry for that

jd123jd123

Ok Forget about daynamically create fields.

take an example we have alredy 20 fields.

 

but how can we add these fields to pagblocktable based on the parent input value and these values should save in the database. 

PremanathPremanath

Absolutly possible,

Write a trigger on parent object.. .

Then take parent object fields  Start_Date__c and No_Of_Fields__c

Based on that Write query for child object , in where condition parent = parent id

Then  you retrive all fields from child object ,Then update whatever you want on that childs

You can start writing code on this i will help you.

jd123jd123

Thanks for your reply,

 

Sorry you didn't get my point.

 

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/27/2012 and  weeks is 3 and i ll save it.

(before only i have created 4 resources under this Staffingplan).when i click on Manage Resource button i am navigatting to visualforce page like this

 

                                                                      i have given in the parent object so it should create 2 fields dynamically with date

                                 Stratdate(parent object date)    Startdate+7(dyanmically create)    Startdate+14(dyanamically create)

---coloumn  names---Employee Name   11/27/2012                                      12/04/2012                                       12/11/2012                              

                                        mahender               i ll give value                                  (inpu text or field dynamically)       input text

                                                                        (inpu text or field dynamically) 

                                        suresh             i ll give value                                  (inpu text or field dynamically)       input text

                                    

                                        ganesh              i ll give value                                  (inpu text or field dynamically)       input text

 

the input values what ever i enterd it should save in the child object if i enter 7 hours, 9 hours, 11 hours it should save under mahender record.like suresh and ganesh also.

 

 

PremanathPremanath

Yes i got your point,

 

Create on VF page ,With that create one Button..

 

In that vf page,

 You get current Record id na,,,,

Based on that you can get values and you can able to save those records

Easy to write the code.. But you can try first ,, Then we can able to help you..

For example i have some requirement

 Create a textbox to enter Account Name. And 3 textareas(name Account description,contact description,..) below it and when hit save check for
     the account record , with the name if atleast 1 record exist then update description else insert account with description.For account and upsert
     Description for contact,opportunity under it based on record availability.[don’t use object variable in the ui instead use string variable and assign values

 

Code for above requirement

 

<apex:page controller="practice2">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection title="Fields" columns="1" rendered="{!showtable==false}">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account Name"/>
                    <apex:inputText value="{!accName}" required="true"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account Description"/>
                    <apex:inputTextarea value="{!accDesc}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Contact Description"/>
                    <apex:inputTextarea value="{!conDesc}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Opportunity Description"/>
                    <apex:inputTextarea value="{!oppDesc}"/>
                </apex:pageBlockSectionItem>
            </apex:pageblockSection>
            <apex:pageBlockSection title="Values Here" rendered="{!showtable==true}" columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account Name"/>
                    <apex:outputText value="{!accName}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account Description"/>
                    <apex:outputText value="{!accDesc}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Contact Description"/>
                    <apex:outputText value="{!conDesc}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Opportunity Description"/>
                    <apex:outputText value="{!oppDesc}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!cussave}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

public class practice2 {
    public String oppDesc {get; set;}
    public String conDesc {get; set;}
    public String accDesc {get; set;}
    public String accName {get; set;}
    public boolean showtable{get;set;}
    public void cussave(){
        try{
            Account acc=new Account();
            Contact con=new Contact();
            Opportunity opp=new Opportunity();
            List<Account> varacc=[select id,name,Description from Account Where Name=:accName limit 1];
            if(varacc.size()!=0){
                varacc[0].Description=accDesc;
                update varacc[0];
                List<Contact> varcon=[select id,Name,Description,AccountId from contact where AccountID=:varacc[0].id limit 1];
                if(varcon.size()!=0){
                    varcon[0].Description=conDesc;
                    update varcon[0];
                }
                else{
                    con.LastName=accName;
                    con.Description=conDesc;
                    con.AccountID=varacc[0].id;
                    insert con;
                }
                List<Opportunity> varopp=[select id,Name,Description,AccountId from Opportunity where AccountID=:varacc[0].id limit 1];
                if(varopp.size()!=0){
                    varopp[0].Description=oppDesc;
                    update varopp[0];
                }
                else{
                    opp.Name=accName;
                    opp.CloseDate=System.Today();
                    opp.StageName='Prospecting';
                    opp.AccountID=varacc[0].id;
                    opp.Description=oppDesc;
                    insert opp;
                }
            }
            else{
                acc.Name=accName;
                acc.Description=accDesc;
                insert acc;
                con.LastName=accName;
                con.Description=conDesc;
                con.AccountID=acc.id;
                insert con;
                opp.Name=accName;
                opp.CloseDate=System.Today();
                opp.StageName='Prospecting';
                opp.AccountID=acc.id;
                opp.Description=oppDesc;
                insert opp;
            }
            showtable=true;
        }
        catch(Exception e){
        }
    }

}

 

I hope it may helpful for you

 

Regards

Prem