Hi, I am trying to sort optionset items in alphabetical order through javascript, but it didn't work with "array.sort()", then how should I achieve this?
*This post is locked for comments
Hi, I am trying to sort optionset items in alphabetical order through javascript, but it didn't work with "array.sort()", then how should I achieve this?
*This post is locked for comments
Hi sdnd2000,
try this code.
function sortOptionSetText()
{
var activityType= Xrm.Page.getAttribute("activitytype").getOptions();
var text = new Array();
for(var i=35;i<activityType.length;i++)
{
if(i <= 148 || i>= 172)
{
text.push(activityType[i].text);
}
}
text.sort();
Xrm.Page.getControl("activitytype").clearOptions();
for(var j=0;j<text.length;j++)
{
for(var k=0;k<activityType.length;k++)
{
if(text[j] == activityType[k].text)
{
Xrm.Page.getControl("activitytype").addOption(activityType[k], [j])
}
}
}
}
If mark as verified if it helps.
Hi sdnd2000,
try this:
var optionsarray = []; var activityOption = Xrm.Page.getAttribute("recoserv_activitytype").getOptions(); for (var i = 0; i < activityOption.length; i++) { if (i <= 34 || (i >= 148 && i <= 172)) continue; var newoption = document.createElement("option"); newoption.value = activityOption[i].value; newoption.text = activityOption[i].text; optionsarray.push(newoption); } optionsarray.sort(function compare(a, b) { var x = a.getText().toLowerCase(); var y = b.getText().toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; }); activityOption.clearOptions(); for (var j = 0; j < optionsarray; j++) { activityOption.addOption(optionsarray[j]); }
Please note i didnt test it, maybe it need some adjustments.
Let me know if works.
If you found the answer helpful, please mark as Verified
Join my network on LinkedIn Follow me on Twitter
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY
Independent Contractor
Hi, Francesco,
It didn't work for me. Basically, I am trying to remove some option set items on load event, then reorder in alphabetical order, here is my code,
var activityOption = Xrm.Page.getAttribute("activitytype").getOptions(); for (var i = 0; i<=34; i++) { Xrm.Page.getControl("activitytype").removeOption(activityOption[i].value);} for (var i = 148; i<=172; i++) { Xrm.Page.getControl("activitytype").removeOption(activityOption[i].value);} Xrm.Page.getAttribute("recoserv_activitytype").getOptions().sort(function compare (a, b) { var x = a.text.toLowerCase(); var y = b.text.toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; });
Hi sdnd2000,
try this:
var options = Xrm.Page.getAttribute("new_optionset").getOptions(); options.sort(function (a, b) { var x = a.getText().toLowerCase(); var y = b.getText().toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; });
Please let me know if you solve.
If you found the answer helpful, please mark as Verified
Join my network on LinkedIn Follow me on Twitter
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY
Independent Contractor
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,269 Super User 2024 Season 2
Martin Dráb 230,198 Most Valuable Professional
nmaenpaa 101,156