Curd Operations Using Jquery

////////create////////////

var entity = {};
entity.new_Currency = { Value: parseFloat(213).toFixed(4) };
entity.new_Customer = {
    Id: "21321434",
    LogicalName: "account"
};
entity.new_DataandTime = new Date("10/13/2017").toLocaleString();
entity.new_DecimalNumber = parseFloat(2321).toFixed(2);
entity.new_FloatPointNumber = parseFloat(221.342).toFixed(2);
entity.new_Lookup = {
    Id: "4343535255435454",
    LogicalName: "account"
};
entity.new_MultiLineTextBox = "fdsffdgf";
entity.new_OptionSet = { Value: 1 };
entity.new_SIngleLineTExt = "gdfgfdgdf";
entity.new_TwoOption = true;
entity.new_WholeNumber = 232434;

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet",
    data: JSON.stringify(entity),
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        var result = data.d;
        var newEntityId = result.AccountId;
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});

////////////update/////////////
var entity = {};
entity.new_Currency = { Value: parseFloat(213).toFixed(4) };
entity.new_Customer = {
    Id: "21321434",
    LogicalName: "account"
};
entity.new_DataandTime = new Date("10/13/2017").toLocaleString();
entity.new_DecimalNumber = parseFloat(2321).toFixed(2);
entity.new_FloatPointNumber = parseFloat(221.342).toFixed(2);
entity.new_Lookup = {
    Id: "4343535255435454",
    LogicalName: "account"
};
entity.new_MultiLineTextBox = "fdsffdgf";
entity.new_OptionSet = { Value: 1 };
entity.new_SIngleLineTExt = "gdfgfdgdf";
entity.new_TwoOption = true;
entity.new_WholeNumber = 232434;
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'')",
    data: JSON.stringify(entity),
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
        XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        alert("Updated"); //Success - No Return Data - Do Something
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});

////retrieve////////////////

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'1234343554544456')?$select=new_Currency,new_Customer,new_DataandTime,new_DecimalNumber,new_FloatPointNumber,new_Lookup,new_MultiLineTextBox,new_OptionSet,new_SIngleLineTExt,new_TwoOption,new_WholeNumber",
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        var result = data.d;
        var new_Currency = result.new_Currency;
        var new_Customer = result.new_Customer;
        var new_DataandTime = result.new_DataandTime;
        var new_DecimalNumber = result.new_DecimalNumber;
        var new_FloatPointNumber = result.new_FloatPointNumber;
        var new_Lookup = result.new_Lookup;
        var new_MultiLineTextBox = result.new_MultiLineTextBox;
        var new_OptionSet = result.new_OptionSet;
        var new_SIngleLineTExt = result.new_SIngleLineTExt;
        var new_TwoOption = result.new_TwoOption;
        var new_WholeNumber = result.new_WholeNumber;
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});


////////////////retrievemul////////////////////////////
$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=new_Currency,new_Customer,new_DataandTime,new_DecimalNumber,new_FloatPointNumber,new_Lookup,new_MultiLineTextBox,new_OptionSet,new_SIngleLineTExt,new_TwoOption,new_WholeNumber&$top=10000",
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        var results = data.d.results;
        for (var i = 0; i < results.length; i++) {
            var new_Currency = results[i].new_Currency;
            var new_Customer = results[i].new_Customer;
            var new_DataandTime = results[i].new_DataandTime;
            var new_DecimalNumber = results[i].new_DecimalNumber;
            var new_FloatPointNumber = results[i].new_FloatPointNumber;
            var new_Lookup = results[i].new_Lookup;
            var new_MultiLineTextBox = results[i].new_MultiLineTextBox;
            var new_OptionSet = results[i].new_OptionSet;
            var new_SIngleLineTExt = results[i].new_SIngleLineTExt;
            var new_TwoOption = results[i].new_TwoOption;
            var new_WholeNumber = results[i].new_WholeNumber;
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});

///////////////////delete/////////////
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'1234567')",
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
        XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        alert("Deleted"); //Success - No Return Data - Do Something
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});

var association = {};
association.uri = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/TaskSet(guid'65444332421321')";


//////////Associate///////////
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'121323443454565667')/$links/",
    data: JSON.stringify(association),
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        alert("Associated"); //Success - No Return Data - Do Something
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});


/////////////Disassociate////////////////
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'121323443454565667')/$links/(guid'65444332421321')",
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
        XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        alert("Disassociated"); //Success - No Return Data - Do Something
    },
    error: function (xhr, textStatus, errorThrown) {
        alert(textStatus + " " + errorThrown);
    }
});

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#