Answered by:
Option Set added to on the fly

Question
-
Is there a way to allow a user to add a new Option Set value on the fly?
Tim HaysThursday, November 3, 2011 1:43 PM
Answers
-
I have researched a possible solution. My notes are below:
Description: Add an Industry field to the Software Entity. The Industry field should be able to be added to on the fly.
Plan:1. Create a new option set for Industry.
2. Add the new option for Industry to the Software Entity.
3. Create a button on the Software to “Add Industry”.
4. Create code to do the following whenever the Add Industry Button is clicked.
a. Prompt the user for the name of the new industry using jQuery Impromptu. The user can enter the Industry name and click OK or Cancel out of the dialog.
b. Once a name is given, verify that the Industry is not already in the list. If it is in the list, alert the user about the issue.
c. Get a list of all Option Set values and determine the highest value used and then add 1 to it.
d. Save a new Option Set Name and Value to the Database.
e. Publish the Software Entity so the new Option Set is now available.
f. Add the new Option Set value to the form.
11/3/2011 10:39:24 AM - Researched some for step 4.f.Var oOption = document.createElement(“option”);
oOption.value = 10006; // This is whatever value you want to use for the option
oOption.text = ‘Blah’; // This is whatever text you want for the value.Xrm.Page.getControl(‘blah’).addOption(oOption); // Blah is the name of the Industry field.
Also found found:
1. Xrm.Page.getControl(‘blah’).removeOption(12345);
2. Xrm.Page.getControl(‘blah’).clearOptions();11/3/2011 10:42:53 AM - Researched some for step 4.c.
Var MaxValue = -1;
Var OptionsList = Xrm.Page.getAttribute(‘blah’).getOptions();
For (var x=0; x < OptionsList.length; x = x + 1)
{
If (OptionsList[x].value > MaxValue)
MaxValue = OptionsList[x].value;
}
11/3/2011 10:53:58 AM - Researched info for step 4.d. I found the following web page that indicates how to add an Option Set Value via Javascript.http://mileyja.blogspot.com/2011/03/working-with-optionset-values-in.html
11/3/2011 10:55:02 AM - Researched info for step 4.e. I found the following web page that indicates how to public an entity via Javascript.http://mileyja.blogspot.com/2011/03/publish-entity-in-jscript-in-crm-2011.html
Tim Hays- Proposed as answer by Philippe LEAL Friday, November 4, 2011 12:16 PM
- Marked as answer by Timothy Hays Friday, November 4, 2011 12:16 PM
Thursday, November 3, 2011 4:29 PM
All replies
-
There is no reason that you cannot do so, if the user has proper privileges in the system.
However, I wouldn't usually recommend that, since making customization changes is an expensive operation, which shouldn't be done by the end user frequently.
A better approach might be using a configuration data entity, and you use lookup field to hook them up.
Just my $0.02, hope it helps.
Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.Thursday, November 3, 2011 3:10 PM -
I have researched a possible solution. My notes are below:
Description: Add an Industry field to the Software Entity. The Industry field should be able to be added to on the fly.
Plan:1. Create a new option set for Industry.
2. Add the new option for Industry to the Software Entity.
3. Create a button on the Software to “Add Industry”.
4. Create code to do the following whenever the Add Industry Button is clicked.
a. Prompt the user for the name of the new industry using jQuery Impromptu. The user can enter the Industry name and click OK or Cancel out of the dialog.
b. Once a name is given, verify that the Industry is not already in the list. If it is in the list, alert the user about the issue.
c. Get a list of all Option Set values and determine the highest value used and then add 1 to it.
d. Save a new Option Set Name and Value to the Database.
e. Publish the Software Entity so the new Option Set is now available.
f. Add the new Option Set value to the form.
11/3/2011 10:39:24 AM - Researched some for step 4.f.Var oOption = document.createElement(“option”);
oOption.value = 10006; // This is whatever value you want to use for the option
oOption.text = ‘Blah’; // This is whatever text you want for the value.Xrm.Page.getControl(‘blah’).addOption(oOption); // Blah is the name of the Industry field.
Also found found:
1. Xrm.Page.getControl(‘blah’).removeOption(12345);
2. Xrm.Page.getControl(‘blah’).clearOptions();11/3/2011 10:42:53 AM - Researched some for step 4.c.
Var MaxValue = -1;
Var OptionsList = Xrm.Page.getAttribute(‘blah’).getOptions();
For (var x=0; x < OptionsList.length; x = x + 1)
{
If (OptionsList[x].value > MaxValue)
MaxValue = OptionsList[x].value;
}
11/3/2011 10:53:58 AM - Researched info for step 4.d. I found the following web page that indicates how to add an Option Set Value via Javascript.http://mileyja.blogspot.com/2011/03/working-with-optionset-values-in.html
11/3/2011 10:55:02 AM - Researched info for step 4.e. I found the following web page that indicates how to public an entity via Javascript.http://mileyja.blogspot.com/2011/03/publish-entity-in-jscript-in-crm-2011.html
Tim Hays- Proposed as answer by Philippe LEAL Friday, November 4, 2011 12:16 PM
- Marked as answer by Timothy Hays Friday, November 4, 2011 12:16 PM
Thursday, November 3, 2011 4:29 PM -
My company decided they didn't want to do this now, so I am abandoning it. But I do think that the solution I outlined would work.
Tim Hays- Edited by Timothy Hays Thursday, November 3, 2011 4:30 PM
Thursday, November 3, 2011 4:30 PM -
Tim, it's a confusing fact that addOption() function won't make actual customization changes. The function is provided so that after you have used removeOption() function, you can use addOption() function to add it back.
Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.Thursday, November 3, 2011 4:52 PM -
I understand the addOption will only make the change on the form. So if I only used that, it would look fine but when the user saved the form, there would be an error because the Option doesn't exist on the back-end. So I included the step of adding it to the Back-end Option Set and Publishing the change. I was planning on using an asynchronous call to publish the change on the back-end so that the user didn't have to wait for the publish to finish. Even if the user then selected the new Option and clicked save before the publish was done, the publishing process would already be running and it is my experience that the system pauses when publishing is happening so the client would have to wait at that time. Ultimately, if they select the new option and click save, by the time the record is saved, the new option will exist on the back-end, thus no errors.
Make sense?
Tim Hays- Edited by Timothy Hays Thursday, November 3, 2011 6:10 PM Clarification
Thursday, November 3, 2011 6:07 PM -
Thursday, November 3, 2011 6:32 PM