Calling a Dynamics 365 action using the Organisation Webservice

   Words by Paul McQuillan

   on 27/12/2018 11:17:00

Click Dimensions vs D365 Marketing

Dynamics allows us to invoke System or Custom Actions from an external App or internal Plugin using a small amount of code to call the Organisation Webservice. This can be useful for keeping our Business Logic in the Action, but re-using this Business Logic from our Custom App or Custom Code.

  Dynamics 365 performs a variety of ‘out-of-the-box’ System Actions that we can invoke in the same way, particularly the SetWordTemplate Action which can be useful to generate new Documents from the Templates in CRM.



If we look at how this would be invoked from a Workflow, we can see how this is similar from Custom Development:

Image Lost

Looking at how we invoke the System Action for SetWordTemplate

This allows us to gauge the Parameters expected for the Action and design our code accordingly – so for the SetWordTemplate Action, we have two Input Parameters that we would specify:

  • SelectedTemplate
    • Entity Reference to point to the ‘documenttemplate’ record that we want to use as our Template
  • Target
    • Entity Reference to the Record in Dynamics that we want to Generate the Document for

We can then apply this in our App or Plugin code quite easily:

OrganizationRequest actionRequest = new OrganizationRequest("SetWordTemplate");

actionRequest["SelectedTemplate"] = new EntityReference("documenttemplate", myTemplateId);

actionRequest["Target"] = new EntityReference(myCrmRecordType, myCrmRecordId);

OrganizationResponse actionResponse = service.Execute(actionRequest);

This code will then invoke the Action in the same way as calling from a Workflow or other Action would.

This works for our Standard Dynamics Actions, but works in the same fashion for Custom Actions that we may add to CRM.

If we take the following simple Custom Action:

Image Lost

Building a simple Custom Action that we can then invoke from Code

This Custom Action implements a single component of our Opportunity Sales Process to generate the Template Document for an Opportunity, Set the Proposal Date to Today and then send an Email to the Customer to inform them that the Proposal is ready for review.

We could invoke this Action as part of our wider Opportunity Business Process Flow, or alternatively invoke from our Custom Code in the same fashion as we saw for the System Action:

(but taking note of the different parameters and invoking accordingly)

OrganizationRequest customActionRequest = new OrganizationRequest("crmcs_ExampleCustomAction");

customActionRequest["OpportunityRecord"] = new EntityReference("opportunity", myCrmOpportunityRecordId);

customActionRequest["Target"] = new EntityReference("opportunity", myCrmOpportunityRecordId);

OrganizationResponse customActionResponse = service.Execute(actionRequest);

This will then invoke our Custom Action and the Steps of logic within – keeping the logic contained within the Action and helping us avoid duplicating the logic in multiple Plugins or Apps, and so helping us maintain one common Business Logic layer that other Apps or Plugins can invoke rather than duplicate.

Custom Actions can also be invoked from other Workflows or even other Actions to complete the concept of re-using our Business Logic from multiple Triggers:


Image Lost

Invoking our Example Custom Action from another Action

This gives us a range of options for how the logic can be invoked.



What is the Target Parameter?

One point we will notice here is the Target Parameter – this is automatically added to any Action we implement as the base record that we pass into the Action. 

By default, this is the Record of a particular Entity Type that we are running the Action from, and so identical to how we run a Workflow against a particular Record of the selected Entity Type.

But – the Target Parameter is similar to the Regarding Record for a Task or other Activity in Dynamics, allowing the ability for any Record to be passed into the Action as the Record that the Action is being run about.

So in our above example of the System Action, this is the Record (and essentially any Record) that we want to generate the Document for – as we could want to generate a Document for any possible Record of any possible Type in Dynamics.

We see this Target is any of the Custom Actions we create, or any of the System Actions we invoke, indeed any call to an Action will fail if the Target Parameter is not supplied as the Primary Record we are running the logic about.


Image Lost

The Target Parameter in Code is our Primary Entity in Workflow



Global Actions

Typically when we create an Action, we supply the Type of Entity we want people to run this Action for.  This is a hold-over from Workflows that need to be run under the context of a particular Entity, and so the default behaviour of a new Action is similarly to be run from a particular Type of Entity in the same way.

However, as we have seen, the Target Parameter is neutral of any particular Entity Type and so we can define Actions that are similarly independent.

This is done by creating our Action and setting the Entity to ‘None (global)’ – which is an option that only appears when creating new Actions:


Image Lost

Creating a Global Action that is not tied to triggering from a particular Entity Type in Dynamics

This creates the same Action but without a default Primary Entity to work with inside Workflow – as Dynamics has no ability at Design-Time to know what type of record would be supplied prior to Run-Time:


Image Lost

The Global Action must invoke EntityReference Parameters with a known Entity Type at Design-Time for the Update Step to be usable

However, in code we can make strong use of Global Actions by supplying whatever Primary Record / Target as we need within code:

OrganizationRequest customActionRequest = new OrganizationRequest("crmcs_GlobalAction");
customActionRequest["Target"] = new EntityReference(someRecordType, someRecordId);
OrganizationResponse customActionResponse = service.Execute(customActionRequest);

This gives a flexible and manageable way of controlling how different elements of our Business Logic can be designed and invoked.


Further Reading

Want to know more? Here’s our Top 6 specialised services  - Dynamics 365 Consultancy | CRMCS Home

Share this Article

Search Articles

Filter Articles

CRM Tech DocMan

Recent Articles

HOW TO: Virtual Entities in Dynamics 365 Release Wave 2 New Feature: The Columns Button HOW TO: Search a date field in Microsoft Dynamics The Relevance Search COMING SOON to Power Apps Portals HOW TO: Manage Your Dynamics 365 Database Size (Video Included) Dynamics 365 Marketing vs ClickDimensions It’s time to pause, reflect and acknowledge a new era of inclusivity and collaboration. Part 2 - How to get the most from a Technology Expert – Asset Management Hub Property & Asset Management Hub Part 1 – Balancing CRM and Asset Management Scopes - Asset Management Hub Creating a Multi-Lingual PowerApps Portal How to Set Up a Microsoft Teams Site Using DocDrive365 Microsoft Teams - Adding a Microsoft Teams URL to a Dynamics Appointment Dynamics 365 Marketing – Customer Voice Survey Not Appearing In Emails? Using SQL Management Studio to connect to the Dynamics DB Calling a Power Platform AI Builder Model via oData How to use DocDrive365 to integrate permissions between Business Units in Dynamics with Sites in SharePoint Getting started with the Power Platform AI Builder. Power Apps Portal Information Hub DocDrive365 Security: Day One - Getting Started with Dynamics to SharePoint Permissions Part 5 - Power Apps Portals: How To Connect Azure B2C With Linked-In Part 4 – Power Apps Portals: Styling Azure B2C for Power Apps Portals The 3 Phases for Using Multi-Select Option Sets in Flow with Microsoft Forms Part 3 – PowerApps Portals: Azure B2C and Power Apps Portals – User Flow for Signup and Signin Part 2 - Power Apps Portals: New Application Registration in Azure B2C for our Power Apps Portal
Contact Us

Want expert advice or a demo?

Get in touch now and see how we can help your business grow.

  • Name
  • Email Address
  • Phone Number
 
Close

Understanding Your Challenges

Our strong understanding of CRM and emerging technologies within the Microsoft environment means we deliver the right solutions for you.

Proven Real-World Solutions

As a leader in the field of Dynamics solutions, our pedigree developing and delivering real-world solutions is unsurpassed.

Long Term Support

We provide support beyond our design, implementation and 'go-live' delivery using Sprints and continual updates to our AppSource apps.

CRMCS | Design by Thinktank Marketing | Citrus-Lime Limited

To improve your experience today and in the future, this site uses cookies. Read our full Privacy Policy & Cookie information here I Understand