Search code examples
javascripttypescriptdynamics-crmpowerapps

Calling a custom action from power apps component


I am trying to create a component that makes a post request to a custom action in my environment. This action has an input called leadid and an output called data, and when called triggers a custom plugin. However, I am having trouble trying to figure out how to call this action from my component.

I understand that you can make calls using this._context.webAPI.retrieveMultipleRecords(entity, query) but I am not trying to achieve this.

I have tried the following directly in the console (in the browser) and it works perfectly, but I get an error Xrm is not defined when using the same code in my TypeScript file:

fetch(Xrm.Page.context.getClientUrl() + "/api/data/v9.2/dev_GetData", {
    "method":"POST",
    "headers":{
        "Accept": "application/json",
            "Content-Type": "application/json; charset=utf-8",
            "OData-MaxVersion": "4.0",
            "OData-Version": "4.0"
    },
    "body": JSON.stringify({
        "leadid": "7ba18ae0-4d0e-ea11-a813-000d3a1bbd52"
    })
}).then((response) => response.json())
.then((data) => {
    console.log(data);
})

I find this odd as when I hover over the other parts (page, context, getClientUrl), it gives me the details of what It does.

What might I be doing wrong, or how may I get the client url without using Xrm.page...?


Solution

  • There is Managed solution/Additional Tool for Dataverse which will give you code snippet with actual values for webapi call. Tool is Dataverse Rest Builder

    https://www.xrmtoolbox.com/plugins/GuidoPreite.DRB/

    Example code snippet for Lead qualification Action

    var execute_QualifyLead_Request = {
            // Parameters
            entity: { entityType: "lead", id: "524a9e0b-f3e6-e711-80e9-005056936c69" }, // entity
            CreateAccount: true, // Edm.Boolean
            CreateContact: true, // Edm.Boolean
            CreateOpportunity: true, // Edm.Boolean
            Status: 1, // Edm.Int32
        
            getMetadata: function () {
                return {
                    boundParameter: "entity",
                    parameterTypes: {
                        entity: { typeName: "mscrm.lead", structuralProperty: 5 },
                        CreateAccount: { typeName: "Edm.Boolean", structuralProperty: 1 },
                        CreateContact: { typeName: "Edm.Boolean", structuralProperty: 1 },
                        CreateOpportunity: { typeName: "Edm.Boolean", structuralProperty: 1 },
                        Status: { typeName: "Edm.Int32", structuralProperty: 1 }
                    },
                    operationType: 0, operationName: "QualifyLead"
                };
            }
        };
        
        Xrm.WebApi.online.execute(execute_QualifyLead_Request).then(
            function success(response) {
                if (response.ok) { return response.json(); }
            }
        ).then(function (responseBody) {
            var result = responseBody;
            console.log(result);
            // Return Type: Collection(mscrm.crmbaseentity)
        }).catch(function (error) {
            console.log(error.message);
        });
    
    
      [1]: https://github.com/GuidoPreite/DRB