Pages

Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Monday, 10 March 2014

Delete a Record in CRM using OData Query

Hi Folks,

To Delete a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library 
--------------------------->
 function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();  //Getting id throug lookup
    var id = lookupObject[0].id;
    var set="new_entity5Set";
    updateRecord(id, set);
}
    function updateRecord(id, odataSetName) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Deleted successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    alert("Error while deletion " + errorThrown);
  
    }
    });
    }

--------------------------->

Update a Record in CRM using OData Query

Hi Folks,

To Update a record in CRM, we require JQuery min 1.4 &  JSON2 script file which we can get from SDK from this specific path
SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Now add the following code to your webresource,
Also add JSon.js and Jquery1.4min.js to the Library
------------------>
function update()
{
    var lookupObject = Xrm.Page.getAttribute("new_lookup").getValue();        //getting the id through lookup
    var id = lookupObject[0].id;
    var entity = new Object();
    entity.new_name="Hello";
    var set="your entity schema nameSet";              //entity name with Set
    updateRecord(id, entity, set);
}
    function updateRecord(id, entityObject, odataSetName) {
    var jsonEntity = window.JSON.stringify(entityObject);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'"+id+"')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
    },
    success: function (data, textStatus, XmlHttpRequest) {
    alert("Updated successfully");
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
    if (XmlHttpRequest && XmlHttpRequest.responseText) {
    alert("Error while updating " + odataSetName+ " ; Error – " + XmlHttpRequest.responseText);
    }
    }
    });
    }

----------------------->

Tuesday, 11 February 2014

On Selecting one lookup field another lookup field sets automatically

Hi Folks,
Client had a requirement in which if we select one lookup field then the another lookup field must get set automatically. for doing this we have three entities. Lets call A, B and C as three different entities. A entity has a lookup of B entity, so A entity is mapped to B entity. Now C entity has a lookup of A and lookup of B, so here the requirement is that if we select a record in A lookup then the B lookup must set automatically(Since A entity and B entity are mapped by a lookup).

For doing these we can use OData Query. You can download the Odata Query Designer from above link

http://crm2011odatatool.codeplex.com/

The above snippet code can be used for doing these operations
------------------->
function customentity_onchange() {

debugger;
    var lookupObject = Xrm.Page.getAttribute("A_type lookup fied schema name").getValue();
    var z=lookupObject[0].id; // Guid of lookup
    if (z != null) {
        var entityA="A_entitynameSet";            //Entity name of A and "Set" is used in Odataquery           
        var field = "B-type lookup schema name in A entity";        //Schema name of a lookup name in A entity
        var a = GetAdata(z,entityA,field);               //calling function to get the details
        if(a!=null)
        {
        alert(a[0]["B-type lookup schema name in A entity].Name);
        var lookupData = new Array();
        var lookupItem = new Object();
        lookupItem.id=a[0]["B-type lookup schema name in A entity"].Id;
        alert(lookupItem.id);
        lookupItem.name = a[0]["B-type lookup schema name in A entity"].Name;
        alert(lookupItem.name);
        lookupItem.entityType ="B-Entity Schema name";
        alert(lookupItem.entityType);
        lookupData[0] = lookupItem;
        Xrm.Page.getAttribute("B type lookup field schema name in C entity").setValue(lookupData);
             }
        }
    }
function GetAdata(z,entityA,field) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/"+entityA+"(guid'" + z + "')?$select="+field;                  //retrieving B type lookup field details of a particular record using GUID
    var jSonArray = new Array();
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.           
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            //alert(hai);
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            //alert("Error :  has occured during retrieval of the A details");
    }
    });

    return jSonArray;
}
--------------->

Monday, 10 February 2014

Calculate the sum of a particular field of all records in an Entity

In this Scenario, I have Three Entities viz.,

1)   "Finance" ,
2)   "Accounts_Payable" and 
3)   "Accounts_Receivable".

 In "Accounts_Payable" Entity I have a field named as "hrm_BillAmount",  similarly in "Accounts_Receivable" Entity I have a field named as "hrm_AmountSpend". In "Finance" entity I have fields "hrm_totalamountpayable" and "hrm_totalamountrecievable".

Now in "hrm_totalamountpayable" feild of Finance entity, I want sum of a particular field(hrm_BillAmount)  of all the records from  Accounts_Payable entity.
Similarly in "hrm_totalamountrecievable" feild of Finance entity, I want sum of a particular field(hrm_AmountSpend) of all the records from  Accounts_Receivable entity.

 The below Snippet code can be used for doing so.,

----------------->
function pay()
{
debugger;
for(var i=1;i<=2;i++)                         //I want to fill two feilds in finace entity so using "for-loop" for doing so
{
if(i==1)
{
var r=recordRetrive();                    //Calling for using Odata Query to get the record details
var d=common(r);                         //Calculating the sum of the fields
Xrm.Page.getAttribute("hrm_totalamountpayable").setValue(d);                       
}
else
{
var r=recordRetrivee();
var d=commonn(r);
Xrm.Page.getAttribute("hrm_totalamountrecievable").setValue(d);
}
}
}
function recordRetrive()
{
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/hrm_accountspayableSet?$select=hrm_BillAmount";        //You can get this query by using Odata Query Designer
var jSonArray = new Array();
   jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.           
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the contacts");
        }
    });

    return jSonArray;
}
function recordRetrivee()
{
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/hrm_accountsrecievableSet?$select=hrm_AmountSpend";
var jSonArray = new Array();
   jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataUri,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.           
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null) {
                jSonArray.push(data.d);
            }
        },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the contacts");
        }
    });

    return jSonArray;
}
function common(r)
{
    if(r!=null && r[0].results.length>0)
    {
        var i = new Array();
        var d=0;
        for (var count = 0; count < r[0].results.length; count++) {
                i[count]=r[0].results[count].hrm_BillAmount.Value;
                d=d+parseFloat(i[count]);                  //Converting to float since my field is of Currency type
                }
                return d;
    }
}
function commonn(r)
{
    if(r!=null && r[0].results.length>0)
    {
        var i = new Array();
        var d=0;
        for (var count = 0; count < r[0].results.length; count++) {
                i[count]=r[0].results[count].hrm_AmountSpend.Value;
                d=d+parseFloat(i[count]);            
//Converting to float since my field is of Currency type
                 }
                return d;
    }
}

-------------->