Triggering Custom Action Without Modifying the Record in Dynamics 365 using JavaScript - CloudFronts

Triggering Custom Action Without Modifying the Record in Dynamics 365 using JavaScript

In Dynamics 365, custom actions allow you to perform specific business logic, such as sending emails, making calculations, or calling external services. Often, these actions are triggered based on field updates or changes to records. However, there may be situations where you want to trigger a custom action through a button click, without modifying any record fields or triggering unnecessary updates.

In this blog, we’ll explore how to use a button to trigger a custom action for a creating a specific task record in Dynamics 365 using JavaScript, that too, without modifying the record.

Here’s the JS code used to trigger the action:

function createFollowUpTaskForCase(executionContext) {
    var formContext = executionContext.getFormContext(); // Get form context
    var caseId = formContext.data.entity.getId(); // Get the GUID of the case

    // Ensure the Case ID is available
    if (caseId) {
        var actionName = "new_CreateFollowUpTask"; // Custom action name
        var serverUrl = Xrm.Utility.getGlobalContext().getClientUrl(); // Get the Dynamics 365 server URL
        var actionUrl = serverUrl + "/api/data/v9.0/" + actionName;

        // Prepare the payload with necessary parameters
        var payload = {
            "CaseId": caseId // Pass the Case ID to the custom action
        };

        // Execute the custom action using the Web API
        Xrm.WebApi.online.executeAction(actionUrl, payload).then(
            function(response) {
                console.log("Follow-up task created successfully for the case", response);
            },
            function(error) {
                console.error("Error creating follow-up task", error);
            }
        );
    } else {
        console.error("Case ID is not available.");
    }
}

Attach the JavaScript function to the button ‘Create Task’ event of the Case form, so that every time a case is created, the follow-up task is automatically generated.

Click on the ‘Create Task’ button.

And here, we have the follow up task created.

Happy Developing!

We hope you found this blog useful, and if you would like to discuss anything, you can reach out to us at transform@cloudfonts.com.


Share Story :

SEARCH BLOGS :

FOLLOW CLOUDFRONTS BLOG :


Secured By miniOrange