web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

JavaScript – Set optionset values by name

Neil Parkhurst Profile Picture Neil Parkhurst 10,727 User Group Leader

A simple but hopefully useful JavaScript function. Pass the name of an option set (pick list) and text value to set the value based on the text.

For example;

SetOptionSet(“new_country”, “England”);

// *** Sets the Optionset value based on text
function setOptionSet (fieldName, setText) {

        var control = Xrm.Page.getAttribute(fieldName);

        if (control.getAttributeType() == 'optionset') {
            if (setText == "" || setText == null || setText == undefined) {
					control.setValue(null);
            }
            else {
                var controlOpts = control.getOptions();
                for (var i = 0; i <= controlOpts.length - 1; i++) {
                    if (controlOpts[i].text.toLowerCase() == setText.toLowerCase()) {
						control.setValue(controlOpts[i].value);
                        return;
                    }
                }
            }
        }
        else {
            alert("Invalid field type or field not found. Field type should be OptionSet");
            return;
        }
};

Comments

*This post is locked for comments