Thursday 8 August 2013

Formula filed functionality in MSCRM 2011 like Sales Force.

This needs Jquery  Library support


// This function is called in onchange of some  look up field.
function RetrieveAccountInfo() {

// Get the lookup filed id value
    customerAttribute = XP.data.entity.attributes.get("xxxid");

    if (customerAttribute != null) {

        customerValue = customerAttribute.getValue();
        if (customerValue != null && customerValue[0] != null) {
            customerType = customerValue[0].typename;
            customerId = customerValue[0].id;
            return retrieveRecord(customerId, "AccountSet", retrieveCompleted, null);
        }
    }
}

// Firing the Rest call to get the all the attributes of this look up record
function retrieveRecord(id, odataSetName, successCallback, errorCallback) {
    //var context = GetGlobalContext();

    var odataEndPoint = "/XRMServices/2011/OrganizationData.svc";
    if (!id) {
        alert("record id is required.");
        return;
    }
    if (!odataSetName) {
        alert("odataSetName is required.");
        return;
    }

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + odataEndPoint + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {

            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (successCallback) {
                successCallback(data.d, textStatus, XmlHttpRequest);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}

// Call Back method to parse values from response
function retrieveCompleted(data, textStatus, XmlHttpRequest) {
    /*Display the required fields and hide if the fields are null */
    entity = data;
    if (!IsNull(entity.xxxxfield. value)) {

        if (entity.xxxxfield.LogicalName == "pricelevel") {

            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = entity.xxxxfield.Id;
            lookupValue[0].name = entity.xxxxfield.Name;
            lookupValue[0].entityType = entity.xxxxfield.LogicalName;
            XP.getAttribute("pricelevelid").setValue(lookupValue);


        }
        else {
            XP.getAttribute("xxxxfiledvalue").setValue(null);
        }
    }
    else {
        XP.getAttribute("xxxxfiledvalue").setValue(null);
    }
}

Get Object type code for MSCRM entities


  •  Using SQL Query :-
Get Object Type codes by Sql Query
Query>
SELECT ObjectTypeCode,*
FROM
ENTITYVIEW
Using JScript  :-
  • The “ObjectTypeCode” can be extracted from Query String using JScript
  • “ObjectTypeCode” resides in “etc” query string  parameter
  • Below is the JScript statement to get “ObjectTypeCode”
var currEntityObjTypeCode= Xrm.Page.context.getQueryStringParameters().etc
Key Points
  • Type codes below 10,000 are reserved for OOB entities.
  • Custom entities have a value greater than or equal to 10,000.
Note:- Custom entity object type codes may change during import and are not guaranteed to be the same between systems.
Getting Object Type Code by ‘Entity Name’ using Jscript
Below script uses CRM inbuilt logic and return the entity type code (or) object type code for the given entity name.
function getObjectTypeCodeByName(entityName) {
try {
var lookupService = new RemoteCommand(“LookupService”, “RetrieveTypeCode”);
lookupService.SetParameter(“entityName”, entityName);
var result = lookupService.Execute();
if (result.Success && typeof result.ReturnValue == “number”) {
return result.ReturnValue;
} else {
return null;
}
} catch (e) {
alert(“Error while getting ETC by Name – ” + e.description);
}
}



BY  

Tuesday 6 August 2013

Parsing MSCRM Retrieved Data values in Plugin

Entity _Account = service.Retrieve(“account”, new Guid(“XXXXX”), new ColumnSet(new string[] { “name”, “accountcategorycode”, “new_collectiondate”, “creditlimit”, “parentaccountid”, “new_executivecommission”, “new_isbilled” }));
//To fetch string value
string Name = _Account["name"].ToString();
//To fetch optionset selected value
int OptionSetValue = ((OptionSetValue)_Account["accountcategorycode"]).Value;
//To fetch date time field value
DateTime CollectionDate = ((DateTime)_Account["new_collectiondate"]).Date;
//To fetch money field value
decimal Creditlimit = ((Money)_Account["creditlimit"]).Value;
//To fetch decimal field value
decimal Executivecommission = (decimal)_Account["new_executivecommission"];
//To fetch lockup field
Guid ParentAccountID = ((EntityReference)_Account["parentaccountid"]).Id;
//To fetch Boolean field
Boolean IsBilled=(Boolean)_Account["new_isbilled"];

BY Mahendarpal

Friday 2 August 2013

Code to Get the Sub grid row values in Mscrm 2011


    function GettheSubGridRowValues() {
   

       // Get the Grid conrttol using its name
      var  grid = document.getElementById("Events");
      // Chek the grid is loaded or not
       if (grid.readyState != "complete") {
        // delay one second and try again. to wait  for grid loading 
        setTimeout(SubGridRefresh, 1000);
        return;
    }
    if (grid) {
         var gridControl = document.getElementById("Events").control;
        //Get the Row ids 
         var  ids = gridControl.get_allRecordIds();
         var  IslogicExecutionRequired= false;
        //Iterate by row wise
        for (i = 0; i < ids.length; i++) {
              // Get the call value by its column name
            var cellValue = gridControl.getCellValue('sci_name', ids[i]);
             if (cellValue == "xxxxx") {
                  IslogicExecutionRequired= true;
            }          
        }
   
    }
}

Attaching Sub Grid Refresh Event In MSCRM 2011



var grid = null;
var gridControl = null;
var ids = null;
var IsScottsdaleShowdown = false;



// call this finction on form laod
function Form_onload() {

// time out set as the grod will load littile late asynchronously after form laod , so we are waiting 2.5 seconds
    setTimeout("SubGridRefresh();", 2500);
}


// Attach  Sub grid refresh event
function SubGridRefresh() {

     grid = document.getElementById("Events");

     // Chek gris controll loaded or not 
    if (grid.readyState != "complete") {
        // delay one second and try after one second whether grid is loaded or not  
        setTimeout(SubGridRefresh, 1000);
        return;
    }

    if (grid) {
    // Attach  grid refresh event
        grid.attachEvent("onrefresh", CustomLogic);

      //cALLING THE CUSTOM LOGIC WHICH NEED TO BE EXECUTED R SUB GRID REFRESH
        grid.control.add_onRefresh(CustomLogic);

    }

    function CustomLogic() {

// Write the custom Logic which need to be executed in Grid Refresh.
    }