Introduction
Sometimes we have a requirement to change activity status using a custom button. Here we are going to discuss how we can complete an activity using WebAPI and action using a custom ribbon button placed on the subgrid.
Solution
We are going to complete an activity using a custom action and then we will be calling our custom action using WebAPI. We will add a button on the subgrid to call this WebAPI method, so let’s first design our custom action. We are going to create action from our solution but you can also created it by navigating to Process directly and can create custom action. Keep in mind when you will create a process outside of your solution, will be using the default prefix (new_) and in case you want to deploy this to another environment you have to add it to the solution, so it is better to create it in solution to make sure it will use your custom prefix and you can export it from there. So let’s start!
We are going to close the appointment activity.
Step 1
Navigate to Process -> New and select configure like below and click on Ok
Step 2
Next, we need to click on Add Step -> select Change Status and configure it like below.
Step 3
Activity your action and keep a note of the name of the action in our case it is him_CloseAppointmentActivity
Step 4
Create a web resource and use following code, here we are calling our custom action using WebAPI
- if (typeof (HIMBAP) == "undefined") {
- var HIMBAP = { __namespace: true };
- }
-
- HIMBAP.AppointmentJSLibrary =
- {
- CloseActivity:function(SelectedRecords,formContext)
- {
-
- for(let i=0; i<SelectedRecords.length; i++)
- {
-
- var parameters = {};
- var entity = {};
- entity.id = SelectedRecords[i];
- entity.entityType = "appointment";
- parameters.entity = entity;
-
- var req
- = {
- entity: parameters.entity,
-
- getMetadata: function() {
- return {
- boundParameter: "entity",
- parameterTypes: {
- "entity": {
- "typeName": "appointment",
- "structuralProperty": 5
- }
- },
- operationType: 0,
- operationName: "him_CloseAppointmentActivity"
- };
- }
- };
-
- Xrm.WebApi.online.execute(req).then(
- function success(result) {
- if (result.ok) {
- var gridContext = formContext.getControl("Appointments");
- gridContext.refresh();
- }
- },
- function(error) {
- alert(error.message);
- }
- );
- }
- }
-
- }
In our code, we are calling our custom action and once we got the result we are refreshing the sub-grid to reflect changes.
Step 5
We can use this method in our ribbon command button. You can open this solution with the appointment entity and our javascript web resource using the Ribbon Workbench tool.
First, let’s add a command, as shown below:
Now add an enable rule to show only this button when at least one record is selected.
Step 6
Now we can refer this command into our command button which we need to add under the Subgrid section like below.
Once the above customization is done, publish your button changes and now, we can use our button to complete appointment activity, as shown below.
Summary
This is how we can add a button on the subgrid with the rule to the command button to make it available when only a single record is selected and use WebAPI to call a custom action.
Hope this will help someone!
Keep learning, keep sharing!!