locked
How to get field value of the parent record using Java Script? RRS feed

  • Question

  • Hi All,

    I am having relation between 2 entites , on one entity I have a name field in which value should be prepopulated as parent record name field value. this has to be done with javascript because it has to be concatenated with some text means "name <space > Text .

    Please give me the code for implementing this .

    Thanks in advance

    Thursday, May 15, 2014 11:03 AM

Answers

  • Hi,

    I think there is no need to use oData, since you have the parent fields name on the child records form. Use the below code on load of form or on change of the parent record field:

    function NameFunction() {
       //get the name of the parent record   
       var name = Xrm.Page.getAttribute("fieldname").getValue()[0].name;
       //set the name of the child
       Xrm.Page.getAttribute("name").setValue(name + " something");
    }

    Of course, this works if you want to retrieve the name (the primary field of the parent). So, if you want to use other fields you have to use the AJAX methods.

    BTW, i think you could use a workflow as well to handle this requirement.


    Iranian Dynamics CRM Association | انجمن متخصصان ایرانی

    Friday, May 16, 2014 7:59 AM
    Moderator

All replies

  • Hello,

    I can't give you the code, but this is my idea how to do it:

    +I suppose your relationship between Parent and Child is: 1-N.

    +Then in your Child form probably has a Lookup Field of Parent field.

    +So in your Lookup Field event OnChange, try to implement the REST service to retrieve the Parent record to get its name --> Fill your Child field value.

    +In case you create a Child record from Parent form, in Child form OnLoad event, call LookupField.fireOnChange() method.

    PS: OData link

    Thursday, May 15, 2014 11:29 AM
  • Hi,

    I think there is no need to use oData, since you have the parent fields name on the child records form. Use the below code on load of form or on change of the parent record field:

    function NameFunction() {
       //get the name of the parent record   
       var name = Xrm.Page.getAttribute("fieldname").getValue()[0].name;
       //set the name of the child
       Xrm.Page.getAttribute("name").setValue(name + " something");
    }

    Of course, this works if you want to retrieve the name (the primary field of the parent). So, if you want to use other fields you have to use the AJAX methods.

    BTW, i think you could use a workflow as well to handle this requirement.


    Iranian Dynamics CRM Association | انجمن متخصصان ایرانی

    Friday, May 16, 2014 7:59 AM
    Moderator