Showing posts with label CRM 2016. Show all posts
Showing posts with label CRM 2016. Show all posts

Friday, 28 April 2017

How to Retrieve record(s) from Dynamics CRM using FetchXml and Web Api

Following JavaScript shows how to retrieve record(s) from Dynamics CRM using FetchXml and Web Api.

function RetrieveUsingFetch() {
var date = new Date();
var today = date.DateFormat("yyyy-MM-dd"); \\ Click here to see function details.
var marketCondition = "";
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='contractdetail'>" +
"<attribute name='contractid' />" +
"<attribute name='title' />" +
"<attribute name='contractdetailid' />" +
"<filter type='and'>" +
"<condition attribute='statuscode' operator='in'>" +
"<value>2</value>" +
"<value>1</value>" +
"</condition>" +
"<condition attribute='activeon' operator='on-or-before' value='" + today + "' />" +
"<condition attribute='expireson' operator='on-or-after' value='" + today + "' />" +
"<condition attribute='customerid' operator='eq' value='" + Xrm.Page.getAttribute("dbc_customerid").getValue()[0].id + "' />" +
"</filter>" +
"<link-entity name='contract' from='contractid' to='contractid' alias='aa'>" +
" <attribute name='title' />" +
"<filter type='and'>" +
"<condition attribute='statuscode' operator='eq' value='3' />" +
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";

var uri = "/contractdetails?fetchXml=" + encodeURIComponent(fetchXml);
var clientUrl = Xrm.Page.context.getClientUrl();
var webApiPath = "/api/data/v8.1";
uri = clientUrl + webApiPath + uri;

var request = new XMLHttpRequest();
request.open("GET", encodeURI(uri), false);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("OData-MaxVersion", "4.0");
request.setRequestHeader("OData-Version", "4.0");
request.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
request.setRequestHeader("Prefer", "odata.maxpagesize=10");
request.onreadystatechange = function () {
if (request.readyState === 4 /* complete */) {

if (request.status === 200) {
request.onreadystatechange = null; 
var data = JSON.parse(request.response);
if (data.value.length === 1 || data.value.length > 1) {
alert(data.value[0].contractdetailid);
}
} else {
var error = JSON.parse(request.response).error;
alert(error.message);
}
}
};
request.send();
}

Note: Make sure that you have right version of WebApi in your code.

Happy Coding

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)

Please check my other (non-CRM) blog here: Programming Blogs

Thursday, 14 April 2016

How to retrieve SLA Pause States (On Hold Case Status) in C# code


Following code shows how to retrieve SLA Pause States from Dynamics CRM using C#. I needed code a functionality to calculate case elapsed minutes and I thought it is worth finding a way to load paused statuses from system rather than hard-coding them.


        private static string[] GetSlaPauseStates(IOrganizationService service)
        {
            QueryExpression qe = new QueryExpression("organization")
            {
ColumnSet = new ColumnSet(new string[] { "organizationid", "slapausestates" })
            };

            EntityCollection orgs = service.RetrieveMultiple(qe);

            if (orgs != null && orgs.Entities.Count > 0)
            {
                var org = orgs[0];
                var slaPauseStates = orgs[0]["slapausestates"];
                string[] states = slaPauseStates.ToString().Split(';');
                return states;
            }

            return null;
        }

In Plugin or Custom Workflow we can retrieve the OrganizationId from contact and then we can directly retrieve the Organization by Id. Following is the code to retrieve organizationId.


    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    IOrganizationServiceFactory serviceFactory =      executionContext.GetExtension<IOrganizationServiceFactory>();
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    var organizationId = context.OrganizationId;

    // Get Organization Business Closure Calendar Id
    var organization = service.Retrieve("organization", context.OrganizationId, new ColumnSet("slapausestates"));
    var slaPauseStates = organization["slapausestates"];

    string[] states = slaPauseStates.ToString().Split(';');


Happy Coding

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)
Premjit Singh
Attendite Ltd.
Twitter

Wednesday, 3 February 2016

How to reset Auto-numbering in Dynamics CRM Online


In past I have shown how to reset auto-numbering un-supported way. Today I am going to discuss how we can reset auto-numbering by Microsoft supported way.

Number greyed-out so it can't be changed through UI. But we can write a small console application to update it using SDK.



Following code can be used to update the number. In this example I am updating CurrentKbNumber but we can also update CurrentCaseNumber, CurrentContractNumber, CurrentQuoteNumber, CurrentOrderNumber and CurrentInvoiceNumber. However we don't need to worry about prefix and they can be updated through UI.

    // QueryExpression to Retrieve Organization
    QueryExpression qe = new QueryExpression("organization")
    {
        ColumnSet = new ColumnSet(new string[] {"organizationid", "name", "currentkbnumber"})
    };
    EntityCollection orgs = service.RetrieveMultiple(qe);

    if (orgs != null && orgs.Entities.Count > 0)
    {
        var org = orgs[0];
        var organizationId = (Guid)org["organizationid"];

        // Creating a new Object to update. Set the CurrentKbNumber to your desired number
        Organization organizationToUpdate = new Organization { CurrentKbNumber = 100000, Id = organizationId};
        service.Update(organizationToUpdate);
    }    

Once updated, please go to Settings > Administration > Auto-numbering to verify.

Happy Coding

P. S. Hayer

Premjit Singh
Attendite Ltd.
Twitter