Wednesday, July 30, 2014

MSCRM Important Technical Links

http://balarajann.blogspot.in/2013/03/microsoft-dynamics-crm-2011-javascript.html

http://guruprasadcrm.blogspot.in/2011/11/how-to-debug-crm-2011-online-plugin.html

http://msdynamicscrmblog.wordpress.com/2013/03/10/get-login-user-role-names-in-javascript-in-dynamics-crm-2011/

http://rajeevpentyala.wordpress.com/2011/07/23/useful-crm-2011-jscript-syntaxes/

http://blog.lulutech.com/?p=498

OData system query options using the OData endpoint

http://msdn.microsoft.com/en-in/library/gg309461.aspx

http://garethtuckercrm.com/2011/03/16/jscript-reference-for-microsoft-dynamics-crm-2011/

Tuesday, July 29, 2014

Get and Set lookup value using javascript in Dynamics CRM 2013

In this article , I am going to explain how to set and get CRM lookup attribute value using javascript

Get a lookup value
var lookup = new Array();
lookup = Xrm.Page.getAttribute("attributename").getValue();
if (lookup != null) {
    var name = lookup[0].name;
    var id = lookup[0].id;
    var entityType = lookup[0].entityType;
}

Set a lookup value
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = recorid;
lookup[0].name = recordname;
lookup[0].entityType = entityname;
Xrm.Page.getAttribute("attributename").setValue(lookup);

Alternate method to set lookup value
Xrm.Page.getAttribute("attributename").setValue([{ id: recorid, name: recordname, entityType: entityname}]);

Wednesday, July 23, 2014

Get Login User Role name in JavaScript using OData

//Check login User has 'System Administrator' role
function CheckUserRole() {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
         var userRoleId = currentUserRoles[i];
    var userRoleName = GetRoleName(userRoleId);
        if (userRoleName == "System Administrator") {
            return true;
        }
    }
    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;
}

Set Default Currency



function setDefaultCurrency() {
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = '{0929D216-DDE7-E311-93EF-74D43599CE45}';  // your currency ID
lookup[0].name = 'Rupee';// Your currency name
lookup[0].entityType = 'transactioncurrency';
Xrm.Page.getAttribute('transactioncurrencyid').setValue(lookup);
}

Loop through Subgrid records

function RetrieveSubGridRecords() {

    if (document.getElementById("SubgridId")) {

        var grid = document.getElementById("SubgridId").control;
        var totalBilled = 0;
        var totalUnBilled =0;
        for (var rowNo = 0; rowNo < grid.GetRecordsFromInnerGrid().length; rowNo++)
        {
           
            if(grid.GetRecordsFromInnerGrid()[rowNo][3].cells[7].outerText.indexOf("Yes") >= 0)
            {
                var numvalue = grid.GetRecordsFromInnerGrid()[rowNo][3].cells[6].outerText.replace(new RegExp(',', 'g'), '');
                alert(numvalue);
                totalBilled = totalBilled + parseFloat(numvalue);
                alert("yes"+totalBilled);
            }
            else
            {
                var numvalue = grid.GetRecordsFromInnerGrid()[rowNo][3].cells[6].outerText.replace(new RegExp(',', 'g'), '');
                alert(numvalue);
                totalUnBilled = totalUnBilled + parseFloat(numvalue);
                alert("no"+totalUnBilled);
            }
               
        }
        Xrm.Page.getAttribute("new_totalbilledamount").setValue(totalBilled);
        Xrm.Page.getAttribute("new_totalunbilledamt").setValue(totalUnBilled);        

    }

    else {

        setTimeout("RetrieveSubGridRecords();", 2500);

    }


}

Bind look up field text to a attribute

function addtitle() {
    var issuetype = Xrm.Page.getAttribute("ccs_requesttype").getValue();
    if (issuetype == null) {
        return false;
    }
    var issuetypename = issuetype[0].name;
    Xrm.Page.getAttribute("title").setValue(issuetypename);
}

Get Logged in User Teams and Get Team details by TeamId

function getUserTeams() {
    var userId = Xrm.Page.context.getUserId().substr(1, 36);
    var serverUrl = "/" + Xrm.Page.context.getOrgUniqueName();
    var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl += "TeamMembershipSet?$filter=SystemUserId eq guid' " + userId + " '";
    var service = GetRequestObject();
    if (service != null) {
        service.open("GET", oDataEndpointUrl, false);
        service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
        service.setRequestHeader("Accept", "application/json,text/javascript, */*");
        service.send(null);
        var requestResults = eval('(' + service.responseText + ')').d;
        if (requestResults != null && requestResults.results.length > 0) {
            return requestResults.results;
        }
    }
}


function getUserTeamName(teamid) {
    var serverUrl = "/" + Xrm.Page.context.getOrgUniqueName();
    var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl += "TeamSet?$select=Name,TeamId&$filter=TeamId eq guid'" + teamid + "'";
    var service = GetRequestObject();
    if (service != null) {
        service.open("GET", oDataEndpointUrl, false);
        service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
        service.setRequestHeader("Accept", "application/json,text/javascript, */*");
        service.send(null);
        var requestResults = eval('(' + service.responseText + ')').d;
        if (requestResults != null && requestResults.results.length > 0) {
            return requestResults.results;
        }
    }
}


function GetRequestObject() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    } else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (ex) {
            return null;
        }
    }
}

Make attribute readonly

Make Attribute readonly


Xrm.Page.ui.controls.get("AttributeName").setDisabled(true);