web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

replacing old code in a library

(0) ShareShare
ReportReport
Posted on by 185

Noob here...trying to find out what is wrong with a single field.

I'm using the following in a library to forcesubmit a calculated field that is read only:

crmForm.all.new_estprofit.ForceSubmit = true;

Since the latest update the calculated read only field is not saving. It looks find when I click save but when I open up the Opportunity later the field is blank.

 

I've read up on the issue and found I need to use the following:

Xrm.Page.data.entity.attributes.get("<fieldname>").setSubmitMode("always");

Can I use this object in the same library or do I have to create an entirely new library for it? Any assistance is appreciated. Since I am new to much of this please let me know if you need clarification.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    jlattimer Profile Picture
    24,564 on at

    You can update the code in your existing library. Doing this and keeping the same function names will prevent you from have to go back into the form and a reference to the new library and change all the bindings to events (OnChange, etc...).

  • Suggested answer
    Community Member Profile Picture
    on at

    You can replace the " crmForm.all.new_estprofit.ForceSubmit = true;" with the " Xrm.Page.data.entity.attributes.get("new_estprofit")setSubmitMode("always");"  in the existing library thats it.No need of creating a new library.

     

    Archana

  • dellis Profile Picture
    185 on at

    I received some error when used the above line. Would it be appropriate to paste my code or is that asking too much?

  • jlattimer Profile Picture
    24,564 on at

    Give it a shot

  • dellis Profile Picture
    185 on at

    /* Enable/Disable form fields based on Payment Type */

    function set_paymenttype()

    {

      switch (crmForm.all.new_paymenttype_optionset.DataValue)

      {

      case "100000000" /* Lease */:

        trigger_deal_economics(true);

        trigger_lease_economics(true);

        trigger_thirdpartyfinancing(false);

        crmForm.all.new_feevolume.disabled=true;

        break;

      case "100000001" /* Sale */:

        trigger_deal_economics(true);

        trigger_lease_economics(false);

        trigger_thirdpartyfinancing(false);

        crmForm.all.new_feevolume.disabled=true;

        break;

      case "100000002" /* Direct Lease-3rd Party */:

        trigger_deal_economics(true);

        trigger_lease_economics(false);

        trigger_thirdpartyfinancing(true);

        crmForm.all.new_feevolume.disabled=true;

        break;

      case "100000003" /* Fee-Third Party Financing */:

        trigger_deal_economics(false);

        trigger_lease_economics(false);

        trigger_thirdpartyfinancing(false);

        crmForm.all.new_salerevenue.disabled=false;

        crmForm.all.new_feevolume.disabled=false;

        break;

      default:

        trigger_deal_economics(false);

        trigger_lease_economics(false);

        trigger_thirdpartyfinancing(false);

        crmForm.all.new_feevolume.disabled=true;

      }

    }

    function trigger_thirdpartyfinancing(valid)

    {

     if (valid==true)

     {

       crmForm.all.new_thirdpartyleaseno.disabled=false;

       crmForm.all.new_thirdpartyleaseterm.disabled=false;

       crmForm.all.new_nbvremarketed.disabled=false;

     }

     else

     {

       crmForm.all.new_thirdpartyleaseno.disabled=true;

       crmForm.all.new_thirdpartyleaseterm.disabled=true;

     }

    }

    function trigger_lease_economics(valid)

    {

     if (valid==true)

     {

       crmForm.all.new_leasepymt.disabled=false;

       crmForm.all.new_leasepymtother.disabled=false;

       crmForm.all.new_leaseterm.disabled=false;

       crmForm.all.new_newcapx.disabled=false;

       crmForm.all.new_nbvremarketed.disabled=false;

       crmForm.all.new_finsoftcosts.disabled=false;

     }

     else

     {

       crmForm.all.new_leasepymt.disabled=true;

       crmForm.all.new_leasepymtother.disabled=true;

       crmForm.all.new_leaseterm.disabled=true;

       crmForm.all.new_newcapx.disabled=true;

       crmForm.all.new_nbvremarketed.disabled=true;

       crmForm.all.new_finsoftcosts.disabled=true;

     }

    }

    function trigger_deal_economics(valid)

    {

     if (valid==true)

     {

       crmForm.all.new_salerevenue.disabled=false;

       crmForm.all.new_estcosts.disabled=false;

     }

     else

     {

       crmForm.all.new_salerevenue.disabled=true;

       crmForm.all.new_estcosts.disabled=true;

     }

    }

    function Form_onsave()

    {

      if (crmForm.all.new_totalleasepayment.DataValue!=null) {

         crmForm.all.new_totalleasepayment.ForceSubmit = true;

      }

      if (crmForm.all.new_estprofit.DataValue!=null) {

         crmForm.all.new_estprofit.ForceSubmit = true;

      }

      if (crmForm.all.new_estprofitmargin.DataValue!=null) {

         crmForm.all.new_estprofitmargin.ForceSubmit = true;

      }

    }

    function new_leadsource_onchange()

    {

      crmSDKSample.filterChildField("new_leadsource", "new_leadsourcedetail");

    }

    function new_profit_onchange()

    {

      var field1 = crmForm.all.new_salerevenue.DataValue;

      field1 = (field1==null ? 0 : field1);

      var field2 = crmForm.all.new_estcosts.DataValue;

      field2 = (field2==null ? 0 : field2);

      crmForm.all.new_estprofit.DataValue = field1 - field2;

      if (field1 != 0)

      {

        crmForm.all.new_estprofitmargin.DataValue = (field1-field2)/field1;

      }

      else

      {

        crmForm.all.new_estprofitmargin.DataValue = 0;

      }

    }

    function new_payment_onchange()

    {

      var field1 = crmForm.all.new_leasepymt.DataValue;

      field1 = (field1==null ? 0 : field1);

      var field2 = crmForm.all.new_leasepymtother.DataValue;

      field2 = (field2==null ? 0 : field2);

      crmForm.all.new_totalleasepayment.DataValue = field1 + field2;

    }

    function Form_onload()

    {

      set_paymenttype();

      var CRM_FORM_TYPE_CREATE = 1;

      var CRM_FORM_TYPE_UPDATE = 2;

      /* Only enable the dependent picklist on a Create or Update form.  

      Disabled and read-only forms are not editable and so do not require dependent picklists. */

      switch (crmForm.FormType)

      {

       case CRM_FORM_TYPE_CREATE:

       case CRM_FORM_TYPE_UPDATE:

       var crmSDKSample = new Object();

        /* Map the dependencies START*/

        /*

        This sample maps 1 dependent picklist for this form.

          * new_leadsource > new_leadsourcedetail

        */

        var gArrDependentPicklists =

         [{

        "ParentFieldId": "new_leadsource",

        "ChildFieldId": "new_leadsourcedetail",

          "ChildFieldLabel": "Lead Source Detail",

          "OptionsGroup":

           [

           {"ParentValue": "200001", /* Trade Show */

            "ChildOptionValues": ["", "1001", /* ACA */

                                                 "1002", /* TASBO */

                                                 "1003", /* CASH */

                                                 "1004", /* CLCS */

                                                 "1005", /* CACSC */

                                                 "1006", /* GASCA */

                                                 "1007", /* MASBO */

                                                 "1008", /* NJCSC */

                                                 "1009", /* SCAPCS */

                                                 "1010", /* Region 5 */

                                                 "1011", /* OSFMA */

                                                 "1012", /* AACC */

                                                 "1013", /* CTASBO */

                                                 "1014", /* COASBO */

                                                 "1015", /* WASBO */

                                                 "1016", /* UAPCS */

                                                 "1017", /* FADSS */

                                                 "1018", /* FSBA */

                                                 "1019", /* SCASC */

                                                 "1020", /* NCSC */

                                                 "1021", /* NSA */

                                                 "1022", /* OKSCL */

                                                 "1023", /* FEPPA */

                                                 "1024", /* HFSE */

                                                 "1025", /* FSPMA */

                                                 "1026", /* LACSA */

                                                 "1027", /* GSNC */

                                                 "1028", /* IFMA */

                                                 "1029", /* LANIGP */

                                                 "1030", /* NYSCA */

                                                 "1031", /* ILASB */

                                                 "1032", /* OSBA */

                                                 "1033", /* AZCSA */

                                                 "1034", /* FCSC */

                                                 "1035", /* TACNIGP */

                                                 "1036", /* OCCA */

                                                 "1037", /* TASA-TASB */

                                                 "1038", /* FEFPA */

                                                 "1039", /* Green CA Schools Summit */

                                                 "1040", /* CA Charter School Show */

                                                 "1041", /* CASBO */

                                                 "100001042", /* CEFPI */

                                                 "100001043", /* GSA EXPO */

                                                 "100001044", /* NCAPCS */

                                                 "100001045", /* NCPSMA */

                                                 "100001046"  /* NE Build */ ]

           },

           {"ParentValue": "200002", /* Referral */

             "ChildOptionValues": ["", "2001", /* Manufacturer */

                                                  "2002", /* Cooperative Purchasing */

                                                  "2003", /* Vendor */

                                                  "2004", /* Customer */

                                                  "2005" /* MBI */ ]

           },

           {"ParentValue": "200003", /* Internet-Website */

             "ChildOptionValues": ["", "3001", /* Search Engine */

                                                  "3002", /* Link */

                                                  "3003", /* Click Thru */

                                                  "3004", /* Hubspot Form */

                                                  "3005", /* Request A Quote */

                                                  "3006", /* Education Presentation */

                                                  "3007", /* Corrections Presentation */

                                                  "3008", /* Healthcare Presentation */

                                                  "3009", /* P.IQ Presentation */

                                                  "3010", /* Green Resource Library */

                                                  "3011" /* Web Sessions */ ]

           },

           {"ParentValue": "200004", /* Printed Materials */

             "ChildOptionValues": ["", "4001", /* Brochure */

                                                  "4002", /* Flyer */

                                                  "4004", /* Postcard */

                                                  "4005" /* Advertisement */ ]

           },

           {"ParentValue": "200005", /* Bid-Lead Services */

             "ChildOptionValues": ["", "5001", /* Govdirections */

                                                  "5002", /* RFP Schoolwatch */

                                                  "5003", /* 360 Mobile */

                                                  "5004", /* Bid Clerk */

                                                  "5005", /* FindRfp */

                                                  "5006", /* CDC News */

                                                  "5007", /* FBO.gov */

                                                  "5008", /* Public Notice Ads */

                                                  "5009", /* Reed */

                                                  "5010", /* Mypublicnotices */

                                                  "5011", /* Bid Ocean */

                                                  "5012", /* NECO */

                                                  "5013", /* RFP Depot */

                                                  "5014", /* Onvia */

                                                  "5015", /* Ebuy */

                                                  "100005016" /* Central Bidding */ ]

           },

           {"ParentValue": "200008", /* Building Decal */

             "ChildOptionValues": [""]

           },

           {"ParentValue": "200009", /* Other */

             "ChildOptionValues": ["", "9001" /* 800 Call */]

           },

           {"ParentValue": "200010", /* News Article-Business Development Team */

             "ChildOptionValues": [""]

           },

           {"ParentValue": "200011", /* Repeat Customer */

             "ChildOptionValues": [""]

           },

           {"ParentValue": "200012", /* Email Blast */

             "ChildOptionValues": [""]

           },

           {"ParentValue": "200013", /* Third Party */

            "ChildOptionValues": ["", "100001301", /* Dealer Calls */

                                                  "100001302", /* New Dealer */

                                                  "100001303" /* Repeat Dealer */ ]

           },

           {"ParentValue": "100000000", /* Marketing Blitz Phone Calls */

             "ChildOptionValues": [""]

           }

          ]

         }

        ];  /* Map the dependancies END*/

      /* Add Child Field Options to the global dependancy array START*/

        for (var depPickList in gArrDependentPicklists)

        {

         var DependentPicklist = gArrDependentPicklists[depPickList];

         /*Get a reference to the child field*/

         var ChildField = crmForm.all[DependentPicklist.ChildFieldId];

         /* Use ExpectedChildOptions to compare the actual child field options to the

             mapped child field options */

         var ExpectedChildOptions = 1; //The empty default field.

         for (var og in DependentPicklist.OptionsGroup)

         {

          var OptionsGroup = DependentPicklist.OptionsGroup[og];

          /* Initialize an array to hold valid options */

          var aOptionGroup = new Array();

          /* Loop through the mapped options and compare to valid options.

             If the options match, add the option to aOptionGroup array. */    

          for (var value in OptionsGroup.ChildOptionValues)

          {

           for (var opt in ChildField.Options)

           {

            if (OptionsGroup.ChildOptionValues[value] == ChildField.Options[opt].DataValue)

            {

             aOptionGroup.push(ChildField.Options[opt]);

            }

           }

           /* Count the number of mapped child options */

           if (OptionsGroup.ChildOptionValues[value] != "")

           { ExpectedChildOptions++ }

          }

          /*Add the valid options to the gArrDependentPicklists[depPickList].OptionsGroup[og].Options */

          OptionsGroup.Options = aOptionGroup;

         }

      /*Display a message if the expected options do not match the available options.*/

         if (ChildField.Options.length != ExpectedChildOptions)

         {

          var sUnavailableOptionsErrorMessage = "Some " + DependentPicklist.ChildFieldLabel + " options are not being displayed.\n";

          sUnavailableOptionsErrorMessage += "The form Onload event script is not synchronized \n";

          sUnavailableOptionsErrorMessage += "for the current " + DependentPicklist.ChildFieldLabel + " options.";

          //sUnavailableOptionsErrorMessage += "Expected: " + ExpectedChildOptions + " Counted: " + ChildField.Options.length;

          alert(sUnavailableOptionsErrorMessage);

         }

        }

        /* Add Child Field Options to the global dependancy array END*/

        /*Attach the global dependancy array to the crmSdkSample object */

        crmSDKSample.gArrDependentPicklists = gArrDependentPicklists;

      /* Define a function that can be called from the Parent field OnChange Event START*/

        var filterChildFieldFunction = function filterChildField(paramParentFieldId, paramChildFieldId)

        {

         for (var depPickList in crmSDKSample.gArrDependentPicklists)

         {

          var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];

      /* Match the parameters to the correct dependent picklist mapping*/

          if ((DependentPicklist.ParentFieldId == paramParentFieldId) && (DependentPicklist.ChildFieldId == paramChildFieldId))

          {

          /* Get references to the related fields*/

           var ParentField = crmForm.all[paramParentFieldId];

           var ChildField = crmForm.all[DependentPicklist.ChildFieldId];

           /* Capture the current value of the child field*/

           var CurrentChildFieldValue = ChildField.DataValue;

           /* If the parent field is null the Child field can be set to null */

           if (ParentField.DataValue == null)

           {

            ChildField.DataValue = null;

            ChildField.ForceSubmit = true;

            ChildField.FireOnChange();

            ChildField.Disabled = true;

            return;

           }

           for (var og in DependentPicklist.OptionsGroup)

           {

            var OptionsGroup = DependentPicklist.OptionsGroup[og];

            /* Find the Options group that corresponds to the value of the parent field. */    

            if (ParentField.DataValue == OptionsGroup.ParentValue)

            {

            /*Enable the field and set the options*/

             ChildField.Disabled = false;

             ChildField.Options = OptionsGroup.Options;

             /*Check whether the current value is valid*/  

             var bCurrentValueIsValid = false;

             for (var validOptionIndex in ChildField.Options)

             {

              var OptionDataValue = ChildField.Options[validOptionIndex].DataValue;

              if (CurrentChildFieldValue == OptionDataValue)

              {

               bCurrentValueIsValid = true;

               break;

              }

             }

             /*

              If the value is valid, set it.

              If not, set the child field to null

             */

             if (bCurrentValueIsValid)

             {

              ChildField.DataValue = CurrentChildFieldValue;

             }  

             else

             {

              ChildField.DataValue = null;

              ChildField.ForceSubmit = true;

              ChildField.FireOnChange();

             }

             break;

            }

           }

          }

         }

        }

        /* Define a function that can be called from the Parent field OnChange Event END*/

        /*Attach the function to the crmSDKSample*/

        crmSDKSample.filterChildField = filterChildFieldFunction;

        /*Attach the crmSDKSample to the window so it is globally available*/

        window.crmSDKSample = crmSDKSample;

        /*Call the OnChange event for any Parent Fields*/

        for (var depPickList in crmSDKSample.gArrDependentPicklists)

        {  

         var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];

         crmForm.all[DependentPicklist.ParentFieldId].FireOnChange();

        }

        break;

      } // END switch (crmForm.FormType)

    }

  • Verified answer
    jlattimer Profile Picture
    24,564 on at

    Oh boy - that was a bit more than I was expecting :) 

    I converted some of the basic stuff but I suspect you will still run into problems. You might need to debug (see article: How to Debug JScript in Microsoft Dynamics CRM 2011) and find out exactly were you still have issues. if you have a lot of code to convert you may consider engaging a partner for assistance. 

    /* Enable/Disable form fields based on Payment Type */
    function set_paymenttype() {
        switch (Xrm.Page.getAttribute("new_paymenttype_optionset").getValue()) {
        case "100000000" /* Lease */
            :
            trigger_deal_economics(true);
            trigger_lease_economics(true);
            trigger_thirdpartyfinancing(false);
            Xrm.Page.getControl("new_feevolume").setDisabled(true);
            break;
        case "100000001" /* Sale */
            :
            trigger_deal_economics(true);
            trigger_lease_economics(false);
            trigger_thirdpartyfinancing(false);
            Xrm.Page.getControl("new_feevolume").setDisabled(true);
            break;
        case "100000002" /* Direct Lease-3rd Party */
            :
            trigger_deal_economics(true);
            trigger_lease_economics(false);
            trigger_thirdpartyfinancing(true);
            Xrm.Page.getControl("new_feevolume").setDisabled(true);
            break;
        case "100000003" /* Fee-Third Party Financing */
            :
            trigger_deal_economics(false);
            trigger_lease_economics(false);
            trigger_thirdpartyfinancing(false);
            Xrm.Page.getControl("new_salerevenue").setDisabled(false);
            Xrm.Page.getControl("new_feevolume").setDisabled(false);
            break;
        default:
            trigger_deal_economics(false);
            trigger_lease_economics(false);
            trigger_thirdpartyfinancing(false);
            Xrm.Page.getControl("new_feevolume").setDisabled(true);
        }
    }
    function trigger_thirdpartyfinancing(valid) {
        if (valid == true) {
            Xrm.Page.getControl("new_thirdpartyleaseno").setDisabled(false);
            Xrm.Page.getControl("new_thirdpartyleaseterm").setDisabled(false);
            Xrm.Page.getControl("new_nbvremarketed").setDisabled(false);
        } else {
            Xrm.Page.getControl("new_thirdpartyleaseno").setDisabled(true);
            Xrm.Page.getControl("new_thirdpartyleaseterm").setDisabled(true);
        }
    }
    function trigger_lease_economics(valid) {
        if (valid == true) {
            Xrm.Page.getControl("new_leasepymt").setDisabled(false);
            Xrm.Page.getControl("new_leasepymtother").setDisabled(false);
            Xrm.Page.getControl("new_leaseterm").setDisabled(false);
            Xrm.Page.getControl("new_newcapx").setDisabled(false);
            Xrm.Page.getControl("new_nbvremarketed").setDisabled(false);
            Xrm.Page.getControl("new_finsoftcosts").setDisabled(false);
        } else {
            Xrm.Page.getControl("new_leasepymt").setDisabled(true);
            Xrm.Page.getControl("new_leasepymtother").setDisabled(true);
            Xrm.Page.getControl("new_leaseterm").setDisabled(true);
            Xrm.Page.getControl("new_newcapx").setDisabled(true);
            Xrm.Page.getControl("new_nbvremarketed").setDisabled(true);
            Xrm.Page.getControl("new_finsoftcosts").setDisabled(true);
        }
    }
    function trigger_deal_economics(valid) {
        if (valid == true) {
            Xrm.Page.getControl("new_salerevenue").setDisabled(false);
            Xrm.Page.getControl("new_estcosts").setDisabled(false);
        } else {
            Xrm.Page.getControl("new_salerevenue").setDisabled(true);
            Xrm.Page.getControl("new_estcosts").setDisabled(true);
        }
    }
    function Form_onsave() {
        if (Xrm.Page.getAttribute("new_totalleasepayment").getValue() != null) {
            Xrm.Page.getAttribute("new_totalleasepayment").setSubmitMode("always");
        }
        if (Xrm.Page.getAttribute("new_estprofit").getValue() != null) {
            Xrm.Page.getAttribute("new_estprofit").setSubmitMode("always");
        }
        if (Xrm.Page.getAttribute("new_estprofitmargin").getValue() != null) {
            Xrm.Page.getAttribute("new_estprofitmargin").setSubmitMode("always");
        }
    }
    function new_leadsource_onchange() {
        crmSDKSample.filterChildField("new_leadsource", "new_leadsourcedetail");
    }
    function new_profit_onchange() {
        var field1 = Xrm.Page.getAttribute("new_salerevenue").getValue();
        field1 = (field1 == null ? 0 : field1);
        var field2 = Xrm.Page.getAttribute("new_estcosts").getValue();
        field2 = (field2 == null ? 0 : field2);
        Xrm.Page.getAttribute("new_estprofit").setValue(field1 - field2);
        if (field1 != 0) {
            Xrm.Page.getAttribute("new_estprofitmargin").setValue((field1 - field2) / field1);
        } else {
            Xrm.Page.getAttribute("new_estprofitmargin").setValue(0);
        }
    }
    function new_payment_onchange() {
        var field1 = Xrm.Page.getAttribute("new_leasepymt").getValue();
        field1 = (field1 == null ? 0 : field1);
        var field2 = Xrm.Page.getAttribute("new_leasepymtother").getValue();
        field2 = (field2 == null ? 0 : field2);
        Xrm.Page.getAttribute("new_totalleasepayment").setValue(field1 + field2);
    }
    function Form_onload() {
        set_paymenttype();
        var CRM_FORM_TYPE_CREATE = 1;
        var CRM_FORM_TYPE_UPDATE = 2;
        /* Only enable the dependent picklist on a Create or Update form.
    
        Disabled and read-only forms are not editable and so do not require dependent picklists. */
        switch (Xrm.Page.ui.getFormType()) {
        case CRM_FORM_TYPE_CREATE:
        case CRM_FORM_TYPE_UPDATE:
            var crmSDKSample = new Object();
            /* Map the dependencies START*/
            /*
    
            This sample maps 1 dependent picklist for this form.
    
             * new_leadsource > new_leadsourcedetail
    
             */
            var gArrDependentPicklists = [{
                    "ParentFieldId" : "new_leadsource",
                    "ChildFieldId" : "new_leadsourcedetail",
                    "ChildFieldLabel" : "Lead Source Detail",
                    "OptionsGroup" : [{
                            "ParentValue" : "200001",
                            /* Trade Show */
                            "ChildOptionValues" : ["", "1001", /* ACA */
                                "1002", /* TASBO */
                                "1003", /* CASH */
                                "1004", /* CLCS */
                                "1005", /* CACSC */
                                "1006", /* GASCA */
                                "1007", /* MASBO */
                                "1008", /* NJCSC */
                                "1009", /* SCAPCS */
                                "1010", /* Region 5 */
                                "1011", /* OSFMA */
                                "1012", /* AACC */
                                "1013", /* CTASBO */
                                "1014", /* COASBO */
                                "1015", /* WASBO */
                                "1016", /* UAPCS */
                                "1017", /* FADSS */
                                "1018", /* FSBA */
                                "1019", /* SCASC */
                                "1020", /* NCSC */
                                "1021", /* NSA */
                                "1022", /* OKSCL */
                                "1023", /* FEPPA */
                                "1024", /* HFSE */
                                "1025", /* FSPMA */
                                "1026", /* LACSA */
                                "1027", /* GSNC */
                                "1028", /* IFMA */
                                "1029", /* LANIGP */
                                "1030", /* NYSCA */
                                "1031", /* ILASB */
                                "1032", /* OSBA */
                                "1033", /* AZCSA */
                                "1034", /* FCSC */
                                "1035", /* TACNIGP */
                                "1036", /* OCCA */
                                "1037", /* TASA-TASB */
                                "1038", /* FEFPA */
                                "1039", /* Green CA Schools Summit */
                                "1040", /* CA Charter School Show */
                                "1041", /* CASBO */
                                "100001042", /* CEFPI */
                                "100001043", /* GSA EXPO */
                                "100001044", /* NCAPCS */
                                "100001045", /* NCPSMA */
                                "100001046" /* NE Build */
                            ]
                        }, {
                            "ParentValue" : "200002",
                            /* Referral */
                            "ChildOptionValues" : ["", "2001", /* Manufacturer */
                                "2002", /* Cooperative Purchasing */
                                "2003", /* Vendor */
                                "2004", /* Customer */
                                "2005" /* MBI */
                            ]
                        }, {
                            "ParentValue" : "200003",
                            /* Internet-Website */
                            "ChildOptionValues" : ["", "3001", /* Search Engine */
                                "3002", /* Link */
                                "3003", /* Click Thru */
                                "3004", /* Hubspot Form */
                                "3005", /* Request A Quote */
                                "3006", /* Education Presentation */
                                "3007", /* Corrections Presentation */
                                "3008", /* Healthcare Presentation */
                                "3009", /* P.IQ Presentation */
                                "3010", /* Green Resource Library */
                                "3011" /* Web Sessions */
                            ]
                        }, {
                            "ParentValue" : "200004",
                            /* Printed Materials */
                            "ChildOptionValues" : ["", "4001", /* Brochure */
                                "4002", /* Flyer */
                                "4004", /* Postcard */
                                "4005" /* Advertisement */
                            ]
                        }, {
                            "ParentValue" : "200005",
                            /* Bid-Lead Services */
                            "ChildOptionValues" : ["", "5001", /* Govdirections */
                                "5002", /* RFP Schoolwatch */
                                "5003", /* 360 Mobile */
                                "5004", /* Bid Clerk */
                                "5005", /* FindRfp */
                                "5006", /* CDC News */
                                "5007", /* FBO.gov */
                                "5008", /* Public Notice Ads */
                                "5009", /* Reed */
                                "5010", /* Mypublicnotices */
                                "5011", /* Bid Ocean */
                                "5012", /* NECO */
                                "5013", /* RFP Depot */
                                "5014", /* Onvia */
                                "5015", /* Ebuy */
                                "100005016" /* Central Bidding */
                            ]
                        }, {
                            "ParentValue" : "200008",
                            /* Building Decal */
                            "ChildOptionValues" : [""]
                        }, {
                            "ParentValue" : "200009",
                            /* Other */
                            "ChildOptionValues" : ["", "9001" /* 800 Call */
                            ]
                        }, {
                            "ParentValue" : "200010",
                            /* News Article-Business Development Team */
                            "ChildOptionValues" : [""]
                        }, {
                            "ParentValue" : "200011",
                            /* Repeat Customer */
                            "ChildOptionValues" : [""]
                        }, {
                            "ParentValue" : "200012",
                            /* Email Blast */
                            "ChildOptionValues" : [""]
                        }, {
                            "ParentValue" : "200013",
                            /* Third Party */
                            "ChildOptionValues" : ["", "100001301", /* Dealer Calls */
                                "100001302", /* New Dealer */
                                "100001303" /* Repeat Dealer */
                            ]
                        }, {
                            "ParentValue" : "100000000",
                            /* Marketing Blitz Phone Calls */
                            "ChildOptionValues" : [""]
                        }
                    ]
                }
            ];
            /* Map the dependancies END*/
            /* Add Child Field Options to the global dependancy array START*/
            for (var depPickList in gArrDependentPicklists) {
                var DependentPicklist = gArrDependentPicklists[depPickList];
                /*Get a reference to the child field*/
                var ChildField = crmForm.all[DependentPicklist.ChildFieldId];
                /* Use ExpectedChildOptions to compare the actual child field options to the
    
                mapped child field options */
                var ExpectedChildOptions = 1; //The empty default field.
                for (var og in DependentPicklist.OptionsGroup) {
                    var OptionsGroup = DependentPicklist.OptionsGroup[og];
                    /* Initialize an array to hold valid options */
                    var aOptionGroup = new Array();
                    /* Loop through the mapped options and compare to valid options.
    
                    If the options match, add the option to aOptionGroup array. */
                    for (var value in OptionsGroup.ChildOptionValues) {
                        for (var opt in ChildField.Options) {
                            if (OptionsGroup.ChildOptionValues[value] == ChildField.Options[opt].getValue()) {
                                aOptionGroup.push(ChildField.Options[opt]);
                            }
                        }
                        /* Count the number of mapped child options */
                        if (OptionsGroup.ChildOptionValues[value] != "") {
                            ExpectedChildOptions++
                        }
                    }
                    /*Add the valid options to the gArrDependentPicklists[depPickList].OptionsGroup[og].Options */
                    OptionsGroup.Options = aOptionGroup;
                }
                /*Display a message if the expected options do not match the available options.*/
                if (ChildField.Options.length != ExpectedChildOptions) {
                    var sUnavailableOptionsErrorMessage = "Some " + DependentPicklist.ChildFieldLabel + " options are not being displayed.\n";
                    sUnavailableOptionsErrorMessage += "The form Onload event script is not synchronized \n";
                    sUnavailableOptionsErrorMessage += "for the current " + DependentPicklist.ChildFieldLabel + " options.";
                    //sUnavailableOptionsErrorMessage += "Expected: " + ExpectedChildOptions + " Counted: " + ChildField.Options.length;
                    alert(sUnavailableOptionsErrorMessage);
                }
            }
            /* Add Child Field Options to the global dependancy array END*/
            /*Attach the global dependancy array to the crmSdkSample object */
            crmSDKSample.gArrDependentPicklists = gArrDependentPicklists;
            /* Define a function that can be called from the Parent field OnChange Event START*/
            var filterChildFieldFunction = function filterChildField(paramParentFieldId, paramChildFieldId) {
                for (var depPickList in crmSDKSample.gArrDependentPicklists) {
                    var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];
                    /* Match the parameters to the correct dependent picklist mapping*/
                    if ((DependentPicklist.ParentFieldId == paramParentFieldId) && (DependentPicklist.ChildFieldId == paramChildFieldId)) {
                        /* Get references to the related fields*/
                        var ParentField = crmForm.all[paramParentFieldId];
                        var ChildField = crmForm.all[DependentPicklist.ChildFieldId];
                        /* Capture the current value of the child field*/
                        var CurrentChildFieldValue = ChildField.getValue();
                        /* If the parent field is null the Child field can be set to null */
                        if (ParentField.getValue() == null) {
                            ChildField.setValue(null);
                            ChildField.ForceSubmit = true;
                            ChildField.fireOnChange();
                            ChildField.Disabled = true;
                            return;
                        }
                        for (var og in DependentPicklist.OptionsGroup) {
                            var OptionsGroup = DependentPicklist.OptionsGroup[og];
                            /* Find the Options group that corresponds to the value of the parent field. */
                            if (ParentField.getValue() == OptionsGroup.ParentValue) {
                                /*Enable the field and set the options*/
                                ChildField.Disabled = false;
                                ChildField.Options = OptionsGroup.Options;
                                /*Check whether the current value is valid*/
                                var bCurrentValueIsValid = false;
                                for (var validOptionIndex in ChildField.Options) {
                                    var OptionDataValue = ChildField.Options[validOptionIndex].getValue();
                                    if (CurrentChildFieldValue == OptionDataValue) {
                                        bCurrentValueIsValid = true;
                                        break;
                                    }
                                }
                                /*
    
                                If the value is valid, set it.
    
                                If not, set the child field to null
    
                                 */
                                if (bCurrentValueIsValid) {
                                    ChildField.setValue(CurrentChildFieldValue);
                                } else {
                                    ChildField.setValue(null);
                                    ChildField.ForceSubmit = true;
                                    ChildField.fireOnChange();
                                }
                                break;
                            }
                        }
                    }
                }
            }
            /* Define a function that can be called from the Parent field OnChange Event END*/
            /*Attach the function to the crmSDKSample*/
            crmSDKSample.filterChildField = filterChildFieldFunction;
            /*Attach the crmSDKSample to the window so it is globally available*/
            window.crmSDKSample = crmSDKSample;
            /*Call the OnChange event for any Parent Fields*/
            for (var depPickList in crmSDKSample.gArrDependentPicklists) {
                var DependentPicklist = crmSDKSample.gArrDependentPicklists[depPickList];
                crmForm.all[DependentPicklist.ParentFieldId].fireOnChange();
            }
            break;
        } // END switch (Xrm.Page.ui.getFormType())
    }
    
  • dellis Profile Picture
    185 on at

    I don't know how much coding help is too much to ask so lesson learned. Thanks for the advice and help and I may by calling a partner but I at least am going in with more knowledge as to what the problem is. Thanks!

  • dellis Profile Picture
    185 on at

    Thanks again. That code worked perfectly!

  • dellis Profile Picture
    185 on at

    Can I ask you a follow up to a similar problem?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans