0

I am trying to use the Multiselect option set and want to build it dynamically, the addOption() is populating the multiselect field correctly, but on save of record it is prompting the validation error.

On change of contact lookup, I am populating the abc_multiselect field. It is populating fine dynamically then user selects the required options from multiselect field, but on save of record (Ribbon Save button not custom save event of form) the CRM is not accepting the values.

MSCRM Version
Server version: 9.2.22081.00182 
Client version: 1.4.4647-2208.1
function polulate(executionContext){
   var formContext = executionContext.getFormContext();
   var multiselect = formContext.getControl("abc_multiselect");
   var high = {value : 895390001, text : "High"};
   multiselect.addOption(high);
}

The error is;

On Popup

One or more of the option values for this picklist are not in the range of allowed values.

Details

Exception Message: A validation error occurred. The value 895390001 of 'abc_multiselect' on record of type 'abc_ENTITY' is outside the valid range. Accepted Values: 

ErrorCode: -2147204326
HexErrorCode: 0x8004431a

ErrorDetails: 
    ApiExceptionSourceKey: Plugin/Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin
    ApiStepKey: fc743e7d-fbea-4695-bdb9-7d78334c8474
    ApiDepthKey: 1
    ApiActivityIdKey: 3907c6d7-ef4a-437e-946f-55e0f956fc3e
    ApiPluginSolutionNameKey: System
    ApiStepSolutionNameKey: System
    ApiExceptionCategory: ClientError
    ApiExceptionMessageName: PicklistValueOutOfRange
    ApiExceptionHttpStatusCode: 400

HelpLink: http://go.microsoft.com/fwlink/?LinkID=398563&error=Microsoft.Crm.CrmException%3a8004431a&client=platform

TraceText: 
    [Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
    [fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin]

Activity Id: 28d5f67f-bf24-4eca-9124-cf95cf06dc30

I also tried to make all option set values hard coded (Added during the multiselect field creation), it worked smoothly. No issue ! But on dynamically population; on save, the CRM is not accepting the values.

I have tried this , this, this and this but all in vain.

Any one can guide, what is missing?

Update 1

function polulate(executionContext){
   var formContext = executionContext.getFormContext();
   var multiselect = formContext.getControl("abc_multiselect");
   multiselect.clearOptions();
   var high = {value : 895390001, text : "High"};
   multiselect.addOption(high);
}

I also checked by changing the value from 895390001 to 895390000 and even to 100 and 101 but still same issue.

Muhammad
  • 13
  • 4
  • 1
    toyota Supra, thanks for your kind suggestion the error section is already added and replaced the image with error string. – Muhammad Sep 07 '22 at 08:55

2 Answers2

1

https://stackoverflow.com/a/48011975/5436880

This should solve your issue? most probably option set does not have 895390001 or it is already selected. you could also try to clear all option sets and then add

clear options

AnkUser
  • 5,421
  • 2
  • 9
  • 25
  • I already checked with the link you suggested, this is not resolving my issue. Please see the update 1 above, by .clearOptions(); it still prompting the same error. – Muhammad Sep 07 '22 at 07:26
  • `formContext.getAttribute("abc_multiselect").setValue([895390000]);` could you try something like this. – AnkUser Sep 07 '22 at 07:43
  • No no! , We cannot do this `formContext.getAttribute("abc_multiselect").setValue([895390000]);` this can only be work if there are values in the CRM multiselect field and `setValue()` will work. But in my case, there is not any value in Multiselect option set. I am populating it dynamically on `on change` of another field. – Muhammad Sep 07 '22 at 07:56
  • IDK why it is problem for you using setvalue, will still work on change (dynamic). coming back to your original addOption, `[{value : 895390001, text : "High"}];` or `{[value : 895390001, text : "High"]};` – AnkUser Sep 07 '22 at 08:02
  • `On change` of contact lookup, i am populating the `abc_multiselect` field. It is populating fine dynamically then user selects the required options from multiselect field, but `on save` of record the CRM is not accepting the values. There is no need to use `setValue()` in anyway. – Muhammad Sep 07 '22 at 08:15
  • did you managed to solve this? – AnkUser Sep 27 '22 at 12:21
  • Not yet. AnkUser – Muhammad Oct 10 '22 at 03:04
0

For using addOption those options need to be there in metadata first. You cannot add an option that is not present in metadata. Example can be Suppose you have Option A, 1 and 3 in Metadata. now you want to add another option 4 using Javascript "addOption", it is not possible.

In your case, get a maximum possible set or options in the optionset now onload of form or Onchange of field "removeOption " the options that are not required.

RR20
  • 28
  • 3