0

I am currently working on an upgrade project Dynamics 2011->2016 and for the following piece of code, it seems that in Dynamics 2016 the addOption() function does not work if the value is not already defined in the optionset. Using the code below does add the value to the optionset but the option cannot be selected! Is there a way to work around this issue without having to add the options statically in the field's optionset ?

PopulateCaseTemplateRecords = function () {
    //Fetch case template records
    var fetchXml = 
    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" 
        + "<entity name='new_casetemplate'>" 
            + "<attribute name='new_name' />" 
            + "<filter type='and'>" 
                + "<condition attribute='statecode' operator='eq' value='0' />" 
                + "<condition attribute='ownerid' operator='eq-userteams' />" 
            + "</filter>" 
        + "</entity>" 
    + "</fetch>";

    //retrieve case templates using asynch call to webapi v8.0
    return WebAPI.Proxy.Fetch("new_casetemplates", fetchXml).then(function (retrievedCaseTemplate) {
        if (retrievedCaseTemplate) {
            if (retrievedCaseTemplate.length >= 1) {
                Xrm.Page.getControl("new_case_template").clearOptions();

                //Populate the Case template option set on Case
                for (var i = 0; i < retrievedCaseTemplate.length; i++) {
                    var option = new Option();
                    option.text = retrievedCaseTemplate[i].new_name;
                    option.value = i;
                    //Add the new option, Does not work in Dynamics 2016 if option not defined in optionset!
                    Xrm.Page.getControl("new_case_template").addOption(option); 
                }
            }
        }
    });
}
jcjr
  • 1,503
  • 24
  • 40
  • Also on 2016 and running into this same issue. You've always been able to add options to optionsets even if they weren't defined in the schema, so this caught me off guard. Digging into this a bit more, there seems to be a difference between what entity you're on. I can successfully add dynamic options and then select them on a contact form, but not an activity form. – Polshgiant Mar 16 '16 at 14:26

1 Answers1

2

It doesn’t make sense to add options that can’t be saved. According to MSDN:

This method doesn’t check that the values in the options you add are valid. If you add invalid options they will not work correctly. You must only add options that have been defined for the specific option set attribute that the control is bound to. Use the attribute getOptions or getOption methods to get valid option objects to add using this method.

You can leverage the CRM 2016 new functionality auto-complete for the text fields: https://msdn.microsoft.com/en-us/library/mt607648.aspx

Or, if you don’t want to save the value, you can use small iframe with html <SELECT> element interacting with the CRM form.

jcjr
  • 1,503
  • 24
  • 40
  • Unfortunately, I can't get this to work on a new CRM 2016 online environment. All the online samples use essentially the same code, so it's possible I'm somehow butchering it. Currently using an existing simple string field. – zabby Feb 17 '16 at 19:06