Tuesday, May 19, 2020

Calling Workflow using JavaScript


var clientUrl = Xrm.Page.context.getClientUrl();
var workflowId = "workflow guid";
var entityId = "entityID";

var requestUri = clientUrl + "/api/data/v9.0/workflows(" + workflowId + ")/Microsoft.Dynamics.CRM.ExecuteWorkflow";

var xhr = new XMLHttpRequest();
xhr.open("POST", requestUri, true);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.setRequestHeader("OData-MaxVersion", "4.0");
xhr.setRequestHeader("OData-Version", "4.0");
xhr.onreadystatechange = function () {
    if (this.readyState == 4) {
        xhr.onreadystatechange = null;
        if (this.status == 200) {
            var result = JSON.parse(this.response);
        } else {
            var error = JSON.parse(this.response).error;
        }
    }
};
xhr.send("{\"EntityId\":\"" + entityId + "\"}");


to get Workflow ID using Name
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="workflow">
    <filter type="and">
      <condition attribute="name" operator="eq" value="Enter Workflow Name Here!" />
    </filter>
  </entity>
</fetch>


Servicepointmanager does not support proxies with the https scheme


I started getting this in VS2015 on one of my machines. I had an additional package feed on myget which constantly asked me for credentials and failed even when entering correct credentials. 
What helped me was clearing the nuget cache and config by deleting these two nuget folders:
  1.          %APPDATA%\NuGet
  2.          %LOCALAPPDATA%\NuGet


After that I restarted Visual Studio and added my custom package source again.

Refresh CRM Page

Hard refresh CRM Page using the below scripts

window.location.reload(true);
//location.reload();

Wednesday, August 14, 2019

Set Default view in inline Lookup D365


function loadCustomDefaultView(){
debugger;
var subgrid= Xrm.Page.getControl("subgridname");
    if (subgrid != null || subgrid != 'undefined' || subgrid !='')) {
Xrm.Page.getControl("subgridname").setDefaultView("GUID");
   }
}

Update the content of Activated Quote in CRM


We got a situation as, the quote should activated for triggering a workflow and from workflow on based on some conditions we need to update the same quote. This is not possible as the quote becomes Read-Only immediately after you activate. 
 
If we want to update it, the option is revising the quote which closes the current quote and creates a new quote where we don't want to create new quotes.
 
After some search, we found a way for doing it and the way is very simple. Just change the state of quote to Draft, update the data and then change the quote state to Active which will not create any new quote.
 
We can do this using a system workflow and also from SDK.
 
System Workflow
 
SDK
// Change the Quote to Draft State
SetStateRequest draftQuote = new SetStateRequest();
draftQuote.EntityMoniker = new EntityReference("quote"newGuid(""));
draftQuote.State = new OptionSetValue(0); // Draft
draftQuote.Status = new OptionSetValue(1); // InProgress
objService.Execute(draftQuote);

// Update the Quote
Entity entQuote = new Entity("quote");
entQuote.Attributes["name"] = "testing1";
entQuote.Id = new Guid("DA5768B5-D62A-E511-80E0-FC15B4283778");
objService.Update(entQuote);

// Change the Quote to Active State.
SetStateRequest activateQuote = new SetStateRequest();
activateQuote.EntityMoniker = new EntityReference("quote"newGuid(""));
activateQuote.State = new OptionSetValue(1); // Active
activateQuote.Status = new OptionSetValue(2); // InProgress
objService.Execute(activateQuote);

Hope this helps.

Get AbsoluteURL from Document Locations in Dynamics 365

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

namespace DocumentLocationURL

{
    public class DocumentLocationURL : CodeActivity
    {
        #region "Parameter Definition"
        [Input("SharePointDocumentID")]
        [Default("")]
        public InArgument<string> SharePointDocumentID { get; set; }

        [Output("AbsoluteURL")]

        public OutArgument<string> AbsoluteURL { get; set; }

        protected override void Execute(CodeActivityContext executionContext)

        {
            #region "Load CRM Service from context"
            // Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)

                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            #endregion

            #region "Read Parameters"

            string _FieldName = this.SharePointDocumentID.Get(executionContext);
            EntityReference _Target = null;
            if (this.SharePointDocumentID.Get(executionContext) == null )
                throw new InvalidPluginExecutionException("Enter Target Input.");
           
            #endregion

            #region "AbsoluteURL Execution"

            RetrieveAbsoluteAndSiteCollectionUrlResponse retriveResponse = new RetrieveAbsoluteAndSiteCollectionUrlResponse();

            RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest

            {
                Target = new EntityReference("sharepointdocumentlocation", context.PrimaryEntityId)//this.SharePointDocumentID.Get(executionContext).Id)
            };
            retriveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)service.Execute(retrieveRequest);
            //throw new InvalidPluginExecutionException(retriveResponse.AbsoluteUrl.ToString());
            this.AbsoluteURL.Set(executionContext, retriveResponse.AbsoluteUrl.ToString());
            #endregion
            

        }

        #endregion

    }

}

Create  Workflow on Document Location entity Create/Update whichever meets to your requirement.

Click on "Add Step" and look for our assembly name i.e DocumentLocationURL, click on this DocumentLocationURL will appear, click on this to add step.

Add update step, and set AbsoluteURL field as DocumentLocationURL  (Output Parameter given in code).

Workflow is completed; save and activate it.

Tuesday, August 13, 2019

I came across a situation where we need to find the Custom Workflow activity across Processes, to achieve this we can add the Plugin assembly and look for dependencies that's not at all possible in all the cases. 

To overcome this, I had written below FetchXML and executed this in XRMToolBox-->FetchXML Tester

<fetch mapping="logical" count="5000" version="1.0" >
    <entity name="workflow" >
        <attribute name="name" />
        <attribute name="category" />
        <link-entity name="dependency" from="dependentcomponentobjectid" to="workflowid" alias="dep" >
            <link-entity name="plugintype" from="plugintypeid" to="requiredcomponentobjectid" alias="pt" >
                <attribute name="assemblyname" />
                <attribute name="description" />
                <attribute name="friendlyname" />
                <attribute name="name" />
                <attribute name="typename" />
                <attribute name="workflowactivitygroupname" />
                <attribute name="isworkflowactivity" />
            </link-entity>
        </link-entity>
    </entity>
</fetch>