Friday, January 30, 2015

CRM 2011 Javascript null check

if (Xrm.Page.getAttribute('myfield') != null) {
    var text = '';
    if (Xrm.Page.getAttribute('myfield').getValue() != null) {
        text = Xrm.Page.getAttribute('myfield').getSelectedOption().text;
    }
}



function SectionExistsOnForm(tabName, sectionName) {
    var section = Xrm.Page.ui.tabs.get(tabName).sections.get(sectionName);
    return section != null;
}

function FieldExistsOnForm(field) {
    var fieldControl = Xrm.Page.getAttribute(field);
    return fieldControl != null;
}

function GetOptionSetText(field) {
    if (FieldExistsOnForm(field)) {
        var text = '';
        if (Xrm.Page.getAttribute(field).getValue() != null) {
            text = Xrm.Page.getAttribute(field).getSelectedOption().text;
        }
        return text;
    }
}

function GetLookupValue(lookup) {
    var name = '';
    var lookupid;
    var lookupObject = Xrm.Page.getAttribute(lookup);

    if (lookupObject != null) {
        var lookUpObjectValue = lookupObject.getValue();
        if ((lookUpObjectValue != null)) {
            name = lookUpObjectValue[0].name;
            lookupid = lookUpObjectValue[0].id;
        }
    }

    return lookupid;
}

function SetLookupValue(lookup, name, id, entityType) {
    if (FieldExistsOnForm(lookup)) {
        var lookupid = null;
        var lookupObject = Xrm.Page.getAttribute(lookup);
        var value = new Array();
        value[0] = new Object();
        value[0].id = id;
        value[0].name = name;
        value[0].entityType = entityType;

        Xrm.Page.getAttribute(lookup).setValue(value);
    }
}

No comments:

Post a Comment