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.
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
http://www.francescopicchi.com
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; });
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.
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Andrés Arias as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Aric Levin - MVP 2 Moderator
MA-04060624-0 1