Thursday, November 3, 2011

Getting attibute value in CRM 2011


This is a generic function which will get you value of any attribute on the form.
Only two things you need to remember, for an optionset it will get the text for the selected option set and for a lookup it will get the first value in the lookup. So it will not get you values for thesystem lookups which can have multiple values.
function GetAttributeValue(attribute) {

    var attrib = Xrm.Page.data.entity.attributes.get(attribute);
    var type = attrib.getAttributeType(); 
    var value;
    switch (type) {
        case "boolean":
        case "decimal":
        case "double":
        case "integer":
        case "memo":
        case "string":
        case "money":
            value = attrib.getValue();
            break;
        case "optionset":
            value = attrib.getSelectionOption().text;
            break;
        case "datetime":
            value = attrib.getValue().toLocaleString();
            break;
        case "lookup":
            var lookupArray = attrib.getValue();
            value = lookupArray[0].name;
            break;
        default:
            value = null;
            break;
    }

    return value;


}
Happy CRMing!

No comments:

Post a Comment