Search This Blog

Wednesday, July 12, 2017

make whole form fields readonly using javascript in CRM 2015

function MarkAllFieldReadOnly() {// this function will make whole form fields readonly

Xrm.Page.ui.controls.forEach(function (control, index) {

try{

control.setDisabled(true);

}

catch (e)

{

}

});

}

Tuesday, August 30, 2016

Group Row based on Multiple related records to show single record

Add following expression:
=Microsoft.VisualBasic.Strings.Join(LookupSet(Fields!COurseActivtyiD.Value, Fields!COurseActivtyiD.Value, Fields!invigilator.Value, "DataSet1"), ", ")

Now Edit in row Group:



Tuesday, March 15, 2016

Get Record's User Security Roles in CRM 2015 using Javascript

Get Record's User Security Roles in CRM 2015 using Javascript

function GetUserRoles() {
    var roles = Xrm.Page.context.getUserRoles();

    for (var i = 0; i < roles.length; i++) {
        GetRole(roles[i]);
    }
}

function GetRole(roleid) {
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetRoleData(this);
    };
    retrieveReq.send();
}

function GetRoleData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            if(retrieved.results[0].Name="System Administrator")
            {
            Xrm.Page.getAttribute("cms_showdialogbutton").setValue(0);
            }
            else
            {
            Xrm.Page.getAttribute("cms_showdialogbutton").setValue(1);
            }
            alert(retrieved.results[0].Name);//Security Role Name
        }
    }
}

Get Current Login User Security Role Name in CRM 2015 using javascript

 Get Current Login User Security Role Name in CRM 2015 using javascript

function CheckUserRole() {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
         var userRoleId = currentUserRoles[i];
    var userRoleName = GetRoleName(userRoleId);
           alert(userRoleName );// Security role name popout
    }
    return false;
}

//Get Rolename based on RoleId
function GetRoleName(roleId) {
    //var serverUrl = Xrm.Page.context.getServerUrl();
    var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
    var roleName = null;
    $.ajax(
        {
            type: "GET",
            async: false,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                roleName = data.d.results[0].Name;
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
        }
    );
    return roleName;
}

Friday, October 23, 2015

Change status using javascript xrmservicetoolkit in CRM 2015

function ChangeStatusReason_toReject() {
XrmServiceToolkit.Soap.SetState("entityname", Xrm.Page.data.entity.getId(), statecode, statuscode,false);
Xrm.Page.data.refresh(true).then(null, null);//for refreshing form
}

Sunday, September 27, 2015

Associate child record with parent using xrmservicetolkit in crm 2013 javascript

In this example I associate contact with an account

function associateRec()
{
var stdId = Xrm.Page.data.entity.getId();//get Account record's GUID
XrmServiceToolkit.Rest.Associate(
    stdId,
    "AccountSet",
    "0F383E15-1047-E511-80D7-3863BB3C5778",//contact record's GUID which u want to associate
    "ContactSet",
    "contact_customer_accounts",//account and contact relationship name
    function () {
         alert("Associated"); //Success - No Return Data - Do Something
    },
    function (error) {
         alert(error.message);
    },
    true
);
}

Thursday, September 24, 2015

Retrieve multiple records using javascript xrmservicetoolit in crm 2013 and crm 2015

function retrieve_multiple()
{

var stdId = Xrm.Page.data.entity.getId();
XrmServiceToolkit.Rest.RetrieveMultiple(
    "new_departmentSet",
    "?$select=new_Description&$filter=new_School/Id eq guid'"+stdId+"'",
    function (results) {
    var ab="";
    for (var i = 0; i < results.length; i++) {
         var Description = results[i].new_Description;
         //var new_School = results[i].new_School;
         //alert(Description);
          var des = Description == null ? 0 :  Description;
         ab=des+"-"+ab;
         if (ab!=null)
            {
            Xrm.Page.getAttribute("new_description").setValue(ab);
            Xrm.Page.getAttribute("new_description").setSubmitMode("always");
            }
            else
            {
            Xrm.Page.getAttribute("new_description").setValue("");
            Xrm.Page.getAttribute("new_description").setSubmitMode("always");   
            }
       
    }
    },
    function (error) {
         alert(error.message);
    },
    function () {
         //On Complete - Do Something
    },
    true
);
}