Wednesday, 26 December 2018

Changing Record Status in MSCRM Using Javascripts

Chnaging The Record Status using Javascripts In MSCRM
============================================================


function changeRecordStatus() {

  var  vEntityName = Xrm.Page.data.entity.getEntityName();
    var guid = Xrm.Page.data.entity.getId();
    guid = guid.replace('{', '').replace('}', '');
    var url = "";
    if (Xrm.Page.context.getClientUrl) {
        
        url = Xrm.Page.context.getClientUrl();
    }
    else {
       
        url = Xrm.Page.context.getServerUrl();
    }
    
    var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    request += "<s:Body>";
    request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>EntityMoniker</c:key>";
    request += "<c:value i:type=\"a:EntityReference\">";
    request += "<a:Id>" + guid + "</a:Id>";
    request += "<a:LogicalName>" + vEntityName + "</a:LogicalName>";
    request += "<a:Name i:nil=\"true\" />";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>State</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + 1 + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>Status</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + 2 + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=\"true\" />";
    request += "<a:RequestName>SetState</a:RequestName>";
    request += "</request>";
    request += "</Execute>";
    request += "</s:Body>";
    request += "</s:Envelope>";
    //send set state request
    $.ajax({
        type: "POST",
        contentType: "text/xml; charset=utf-8",
        datatype: "xml",
        url: url + "/XRMServices/2011/Organization.svc/web",
        data: request,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
            XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            Xrm.Page.data.refresh();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

Plugin triggering on Actions in MSCRM

Plugin triggering on actions in mscrm
============================================================

 public class TestingActions:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
           

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            tracingService.Trace("triggered");
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            string firstname = context.InputParameters.Contains("Firstname") == true ? context.InputParameters["Firstname"].ToString() : string.Empty;
            tracingService.Trace(firstname);
            Entity TestEnity = service.Retrieve("new_testcrm", new Guid(firstname), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
            context.OutputParameters["SearchResults"] = TestEnity;

        }

    }

==========================

Action:



ActionsCallingfromWEPAPI Using Javascrips


ActionsCallingfromWEPAPI Using Javascrips
===================================================

function ActionscallingfromWEBAPi() {

    var Gudid = Xrm.Page.data.entity.getId();
    Gudid = Gudid.replace('{', '').replace('}', '');
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_testcrms(" + Gudid + ")/Microsoft.Dynamics.CRM.new_TestActions", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                debugger;
                /// lookupfield returnType:
                var results = JSON.parse(this.response);
                alert(results.accountid);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

Tuesday, 25 December 2018

Calling WEBAPI Using Javascrips Input/Out Parameters

=====================================================================

Calling WEBAPI Using Javascrips Input/Out Parameters
========================================================================


function ActionsCallingWebAPI() {

    var Gudid = Xrm.Page.data.entity.getId();
    Gudid = Gudid.replace('{', '').replace('}', '');
    var parameters = {};
    var accounts = {};
    accounts.accountid = "9083FB80-8844-E211-979C-00155D2EE30B"; 
    accounts["@odata.type"] = "Microsoft.Dynamics.CRM.account";
    parameters.Accounts = accounts;
    parameters.Personname = "TestingActionO/p";
    parameters.Countries = 2;
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_testcrms(" + Gudid + ")/Microsoft.Dynamics.CRM.new_TestActions", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));
}



Calling Actions Using WEBAPI in Javascript

Calling Actions Using WEBAPI in Javascript
========================================================================


function WebApiusingActions()
{
    debugger;
    var Gudid = Xrm.Page.data.entity.getId();
    Gudid = Gudid.replace('{', '').replace('}', '');
    //var parameters = {};
    var parameters = {};
    parameters.Countries = 2;
    var accounts = {};
    accounts.accountid = "9083FB80-8844-E211-979C-00155D2EE30B"; //Delete if creating new record
    accounts["@odata.type"] = "Microsoft.Dynamics.CRM.account";
    parameters.Accounts = accounts;
    parameters.Gendar = false;
    parameters.Personname = "TestingWEBAPI2";

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_testcrms(" + Gudid + ")/Microsoft.Dynamics.CRM.new_TestActions", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {

                debugger;
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(parameters));
}

Single Record Retrieving Using WebAPI in MSCRM

Single Record Retrieving Using WebAPI in MSCRM
==============================================================
function RetrievingRecords() {

    var lookup = Xrm.Page.data.entity.getId();
    var lookupID = lookup.replace('{', '').replace('}', '');
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_testcrms(" + lookupID + ")?$select=_new_accounts_value,new_countries,new_dob,new_gender,new_moneytype,new_moneytype_base,new_name,new_numbers,new_rates", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
           
                var result = JSON.parse(this.response);
                // Lookup field guid value
                var _new_accounts_value = result["_new_accounts_value"];
                //Lookupfield text value
                var _new_accounts_value_formatted = result["_new_accounts_value@OData.Community.Display.V1.FormattedValue"];
                //  lookup field  entityname
                var _new_accounts_value_lookuplogicalname = result["_new_accounts_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                // optionset value
                var new_countries = result["new_countries"];

                // optionset field text value
                var new_countries_formatted = result["new_countries@OData.Community.Display.V1.FormattedValue"];
                var new_dob = result["new_dob"];
                // two optionset true
                var new_gender = result["new_gender"];
                // two optionset text value  Yes
                var new_gender_formatted = result["new_gender@OData.Community.Display.V1.FormattedValue"];
                // money field values
                var new_moneytype = result["new_moneytype"];
                // money field values with symbols
                var new_moneytype_formatted = result["new_moneytype@OData.Community.Display.V1.FormattedValue"];
                var new_moneytype_base = result["new_moneytype_base"];
                var new_moneytype_base_formatted = result["new_moneytype_base@OData.Community.Display.V1.FormattedValue"];
                var new_name = result["new_name"];
                var new_numbers = result["new_numbers"];
                var new_numbers_formatted = result["new_numbers@OData.Community.Display.V1.FormattedValue"];
                var new_rates = result["new_rates"];
                var new_rates_formatted = result["new_rates@OData.Community.Display.V1.FormattedValue"];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

}

Detele Records Using WEBAPI


Detele Records Using WEBAPI
=================================================================

function DeteleRecord() {

    var lookup = Xrm.Page.data.entity.getId();
    var lookupID = lookup.replace('{', '').replace('}', '');
    var req = new XMLHttpRequest();
    req.open("DELETE", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_testcrms(" + lookupID + ")", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204 || this.status === 1223) {
                alert("deleted record successfully..");

            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();

}