Answered by:
Showing option in option set via javascript

Question
-
Hi,
In javascript I have removed some options in an option set based on the value of another field. The values are removed successfully but if the value is changed then the options associated with it don't get put back into the option set field. Is there a reverse of the removeoption command that i can use to show values?
Thanks
Tuesday, January 29, 2013 5:02 PM
Answers
-
Below example helps you:
function OnLoad() {
var optionsetControl = Xrm.Page.ui.controls.get("brd_statusreason");
var options = optionsetControl.getAttribute().getOptions();
var type = Xrm.Page.getAttribute("brd_status").getValue();
var reason = Xrm.Page.getAttribute("brd_statusreason").getValue();
// 'Active' is selected
if (type == 172100000) {
optionsetControl.clearOptions();
for (var i = 0; i < options.length - 1; i++) {
if (i == 0 || i == 1 || i == 2) {
optionsetControl.addOption(options[i]);
}
}
}
// 'Resolved' is selected
else if (type == 172100001) {
optionsetControl.clearOptions();
for (var i = 0; i < options.length - 1; i++) {
if (i == 3 || i == 4) {
optionsetControl.addOption(options[i]);
}
}
}
// 'Cancelled' is selected
else if (type == 172100002) {
optionsetControl.clearOptions();
optionsetControl.addOption(options[5]);
}
Xrm.Page.getAttribute("brd_statusreason").setValue(reason);
}
If the answer helped you, remember to mark it as answer.
- Edited by Payman BiukaghazadehEditor Tuesday, January 29, 2013 6:46 PM
- Proposed as answer by begli Wednesday, January 30, 2013 6:17 AM
- Marked as answer by Payman BiukaghazadehEditor Sunday, May 12, 2013 4:47 AM
Tuesday, January 29, 2013 6:45 PMModerator
All replies
-
Hello,
you need to use addOption to add those value back to option set.
refer: http://msdn.microsoft.com/en-us/library/51828fe3-f6ff-4f97-80ed-b06b3a354955#BKMK_addOption
Contact Me
Follow me on Twitter
My Facebook Page
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Tuesday, January 29, 2013 5:17 PM
Tuesday, January 29, 2013 5:17 PMModerator -
Hello,
To do that you will have to store the whole collection of options somewhere. For example:
window.allPicklistOptions = Xrm.Page.getAttribute('picklist schema name').getOptions();
and then restore back options using addOption method. For example:
Xrm.Page.getControl('picklist schema name').addOption(window.allPicklistOptions[0]);
Tuesday, January 29, 2013 5:20 PMModerator -
Below example helps you:
function OnLoad() {
var optionsetControl = Xrm.Page.ui.controls.get("brd_statusreason");
var options = optionsetControl.getAttribute().getOptions();
var type = Xrm.Page.getAttribute("brd_status").getValue();
var reason = Xrm.Page.getAttribute("brd_statusreason").getValue();
// 'Active' is selected
if (type == 172100000) {
optionsetControl.clearOptions();
for (var i = 0; i < options.length - 1; i++) {
if (i == 0 || i == 1 || i == 2) {
optionsetControl.addOption(options[i]);
}
}
}
// 'Resolved' is selected
else if (type == 172100001) {
optionsetControl.clearOptions();
for (var i = 0; i < options.length - 1; i++) {
if (i == 3 || i == 4) {
optionsetControl.addOption(options[i]);
}
}
}
// 'Cancelled' is selected
else if (type == 172100002) {
optionsetControl.clearOptions();
optionsetControl.addOption(options[5]);
}
Xrm.Page.getAttribute("brd_statusreason").setValue(reason);
}
If the answer helped you, remember to mark it as answer.
- Edited by Payman BiukaghazadehEditor Tuesday, January 29, 2013 6:46 PM
- Proposed as answer by begli Wednesday, January 30, 2013 6:17 AM
- Marked as answer by Payman BiukaghazadehEditor Sunday, May 12, 2013 4:47 AM
Tuesday, January 29, 2013 6:45 PMModerator -
I've tried to use addOption along with getting all the options from the option set, but while it does add options back in, all of them are labelled 'undefined'.Wednesday, January 30, 2013 9:43 AM
-
I've tried to use addOption along with getting all the options from the option set, but while it does add options back in, all of them are labelled 'undefined'.
If you add options in a way which I mentioned, first all of the options would be saved in a var, and then would be used again in addOption.If the answer helped you, remember to mark it as answer.
Wednesday, January 30, 2013 9:59 AMModerator -
Hi,
Kindly check wheather you are adding options like this
add options to optionset as follows
controlObj.addOption(option,[index]);
if you are not adding option name it ll return undefined only
(mark as answer and vote as helpful)
Thanks,
SASANK K
- Edited by SASANK K (MCTS) Wednesday, January 30, 2013 11:01 AM
Wednesday, January 30, 2013 11:00 AM