CRM 2013 Javascript Block Save

0 comments


Script:

function My_PreventSaveFunction(eContext, Message) {
        alert(Message);
        eContext.getEventArgs().preventDefault();
}

CRM 2013 Javascript Filter Custom Lookup View Field (PreFiltering Method)

0 comments


Issues:

Changes in category effect custom filtering, then the lookup display on form will corrupt but when u click on lookup more its ok.

Script:

var filter = '';

function FilterListingViewCategory(Value) {
    filter = "<filter type='and'>" +
              "<condition attribute='statecode' operator='eq' value='0' />" +
              "<condition attribute='vwlzs_category' operator='in'>" +
                "<value>" + RegistrationId + "</value>" +
              "</condition>" +
         "</filter>";

    preFilterLookupCategory(filter);
}

function preFilterLookupCategory(filter) {

    Xrm.Page.getControl("vwlzs_aidcategorylk").addPreSearch(function () {
        addLookupFilterCategory(filter);
    });
}

function addLookupFilterCategory(filter) {

    Xrm.Page.getControl("vwlzs_aidcategorylk").addCustomFilter(filter);
}

CRM 2013 Javascript Filter Custom Lookup View Field (FetchXML)

0 comments


Issues:

This part doesn't work when user click on lookup for more because i'm unable to set the default view fo the lookup. The user need to select the custom view by their own.

Script:

function FilterListingViewAidCategory(Value) {

    var RegistrationId = Xrm.Page.getAttribute("vwlzs_asnafcategory").getValue();
    if (RegistrationId == null && Value != null) {
        RegistrationId == Value;
    }
    var entityNumber = GetObjectTypeCode("vwlzs_aidsapplication");

    var FetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                      "<entity name='vwlzs_category'>" +
                        "<attribute name='vwlzs_categoryid' />" +
                        "<attribute name='vwlzs_name' />" +
                        "<attribute name='createdon' />" +
                        "<order attribute='vwlzs_name' descending='false' />" +
                        "<filter type='and'>" +
                          "<condition attribute='statecode' operator='eq' value='0' />" +
                          "<condition attribute='vwlzs_categoryasnaf' operator='in'>" +
                            "<value>" + RegistrationId + "</value>" +
                          "</condition>" +
                        "</filter>" +
                      "</entity>" +
                    "</fetch>";

    var layoutXml = "<grid name='resultset' object='10038' jump='vwlzs_name' select='1' preview='1' icon='1'>" +
                        "<row name='result' id='vwlzs_categoryid'>" +
                          "<cell name='vwlzs_name' width='300' />" +
                          "<cell name='createdon' width='150' />" +
                        "</row>" +
                      "</grid>";

    var viewId = guid();
    var viewDisplayName = "Filtered Based On Category";


    Xrm.Page.getControl("vwlzs_aidcategorylk").addCustomView(viewId, "vwlzs_category", viewDisplayName, FetchXML, layoutXml, true);
}


CRM 2013 Javascript Check CRM User Role in System

0 comments


Script:

function CheckorNotInterBranch() {
    var EntityId = Xrm.Page.ui.getFormType();
    if (EntityId == 1) {
        //Security.UserInRole.checkUserInRole(["HQ Staff", "System Customizer", "Custom Role Name"], function () {
        Security.UserInRole.checkUserInRole(["HQ Staff"], function () {
            //alert("valid"); // The user is in one of the specifed roles.
            Xrm.Page.getAttribute("vwlzs_interbranch").setValue(true);
        },
        function () {
            //alert("invalid"); // The user is not in one of the specifed roles.
            Xrm.Page.getAttribute("vwlzs_interbranch").setValue(false);
        });
    }
    //check new form
    // Check user role when user role = HQ Staff then inter branch = yes
    //var UserRoles = Xrm.Page.context.getUserRoles();
    // Check User role when user role = branch staff then inter branch = no
}

//If the Security namespace object is not defined, create it.
if (typeof (Security) == "undefined")
{ Security = {}; }
// Create Namespace container for functions in this library;
if (typeof (Security.UserInRole) == "undefined") {
    Security.UserInRole = {
        isInRole: null,
        roleIdValues: [],
        validFunction: null,
        invalidFunction: null,
        checkRoles: [],
        checkUserInRole: function (roles, validFunc, invalidFunc) {
            validFunction = validFunc;
            invalidFunction = invalidFunc;
            checkRoles = roles;
            Security.UserInRole.getAllowedSecurityRoleIds();
        },
        getAllowedSecurityRoleIds: function () {
            var filter = "";
            for (var i = 0; i < checkRoles.length; i++) {
                if (filter == "") {
                    filter = "Name eq '" + checkRoles[i] + "'";
                }
                else {
                    filter += " or Name eq '" + checkRoles[i] + "'";
                }
            }
            Security.UserInRole.querySecurityRoles("?$select=RoleId,Name&$filter=" + filter);
        },
        validateSecurityRoles: function () {
            switch (Security.UserInRole.isInRole) {
                //If the user has already been discovered in role then call validFunc
                case true:
                    validFunction.apply(this, []);
                    break;
                default:
                    var userRoles = Xrm.Page.context.getUserRoles();
                    for (var i = 0; i < userRoles.length; i++) {
                        var userRole = userRoles[i];
                        for (var n = 0; n < Security.UserInRole.roleIdValues.length; n++) {
                            var role = Security.UserInRole.roleIdValues[n];
                            if (userRole.toLowerCase() == role.toLowerCase()) {
                                Security.UserInRole.isInRole = true;
                                // Call function when role match found
                                validFunction.apply(this, []);
                                return true;
                            }
                        }
                    }
                    // Call function when no match found
                    invalidFunction.apply(this, []);
                    break;
            }
        },
        querySecurityRoles: function (queryString) {
            var req = new XMLHttpRequest();
            var url = "";
            // Try getClientUrl first (available post Rollup 12)
            if (Xrm.Page.context.getClientUrl) {
                url = Xrm.Page.context.getClientUrl();
            }
            else {
                url = Xrm.Page.context.getServerUrl();
            }
            req.open("GET", url + "/XRMServices/2011/OrganizationData.svc/RoleSet" + queryString, true);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState == 4 /* complete */) {
                    req.onreadystatechange = null; //Addresses memory leak issue with IE.
                    if (this.status == 200) {
                        var returned = window.JSON.parse(this.responseText).d;
                        for (var i = 0; i < returned.results.length; i++) {
                            Security.UserInRole.roleIdValues.push(returned.results[i].RoleId);
                        }
                        if (returned.__next != null) {
                            //In case more than 50 results are returned.
                            // This will occur if an organization has more than 16 business units
                            var queryOptions = returned.__next.substring((url + "/XRMServices/2011/OrganizationData.svc/RoleSet").length);
                            Security.UserInRole.querySecurityRoles(queryOptions);
                        }
                        else {
                            //Now that the roles have been retrieved, try again.
                            Security.UserInRole.validateSecurityRoles();
                        }
                    }
                    else {
                        var errorText;
                        if (this.status == 12029)
                        { errorText = "The attempt to connect to the server failed."; }
                        if (this.status == 12007)
                        { errorText = "The server name could not be resolved."; }
                        try {
                            errorText = window.JSON.parse(this.responseText).error.message.value;
                        }
                        catch (e)
                        { errorText = this.responseText }
                    }
                }
            };
            req.send();
        },
        __namespace: true
    };
}