cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ManishJain
Level: Powered On

How to call custom actions using PCF

How do I make calls to custom actions from PCF ? Any suggestions , please help.

 

Thanks

9 REPLIES 9
ScottDurow
Level 8

Re: How to call custom actions using PCF

You will need to use the WebApi.execute method to call the action.

 

Here is a sample I wrote for the Xrm.WebApi - but it's the same for the context.WebApi in PCF - https://github.com/scottdurow/PowerApps-Samples/blob/model-driven-app-webapi/cds/webapi/ts-model-dri...

 

Hope this helps,

Scott

ManishJain
Level: Powered On

Re: How to call custom actions using PCF

Thanks Scott . Initially I thought of the same. 

 

As per latest documentation it looks like only 5 methods are only supported and in VS code those 5 methods are only showing. 

 

https://docs.microsoft.com/en-us/powerapps/developer/component-framework/reference/webapi

 

Please suggest if that still holds true.

 

Thanks

ScottDurow
Level 8

Re: How to call custom actions using PCF

execute and executeMultiple is still there on the webApi object - it is just not exposed by the Typescript defintions in PCF. 

I suspect this is the case because it doesn't necessarily match up with the direction that PCF is going when embeded in CanvasApps.

 

@HemantGCould you comment on this? Is Xrm.WebApi.execute specifically not supported in PCF even if it is part of the Xrm.WebApi in Model driven apps? If this is the case then it'll be a problem for converting Webresources over to PCF.

NickDewitt
Level 8

Re: How to call custom actions using PCF

You can revert to just calling the Web API via fetch if it's not implemented yet. While not as 'nice' as using the webAPI wrapper, it's supported and does the job.

 

This example calls the "WinOpportunity" action as documented in https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/use-web-api-actions

const query = "/api/data/v9.0/WinOpportunity";

const headers = {
    "Accept": "application/json",
    "Content-Type": "application/json; charset=utf-8",
    "OData-MaxVersion": "4.0",
    "OData-Version": "4.0"
}

const opportunityId = "(b3828ac8-917a-e511-80d2-00155d2a68d2)"

const body = JSON.stringify(
    {
        "Status": 3,
        "OpportunityClose": {
            "subject": "Won Opportunity",
            "opportunityid@odata.bind": "/api/data/v9.0/opportunities" + opportunityId
        }
    }
)

await fetch(query, {
    method: 'post',
    headers: headers,
    body: body
});

You may need to polyfill fetch for internet explorer though as using XmlHttpRequest is just ugly.

ManishJain
Level: Powered On

Re: How to call custom actions using PCF

Hi Nick, 

 

Thanks for replying on this. before proceeding wanted to know if this is working for you ? As per latest MSDN and earlier post suggested by Scott it should work but to confirm if this is working for you ?

 

Thanks

NickDewitt
Level 8

Re: How to call custom actions using PCF

Hey ManishJain,

 

The .execute and .executeMultiple do work for me, they are there on the webAPI object in the browser, but not in the typescript definitions or the documentation at the moment, so they are technically not supported.

 

The webAPI is just a wrapper around the actual Web Api though, which you can call via XmlHttpRequest or Fetch. So if you want to do this in a supported manner, you could just call the Web Api yourself instead of using the not yet documented or supported execute method on the webAPI object.

Highlighted
ManishJain
Level: Powered On

Re: How to call custom actions using PCF

Please share the link/code


@NickDewitt wrote:

Hey ManishJain,

 

The .execute and .executeMultiple do work for me, they are there on the webAPI object in the browser, but not in the typescript definitions or the documentation at the moment, so they are technically not supported.

 

The webAPI is just a wrapper around the actual Web Api though, which you can call via XmlHttpRequest or Fetch. So if you want to do this in a supported manner, you could just call the Web Api yourself instead of using the not yet documented or supported execute method on the webAPI object.



to your working solution 

NickDewitt
Level 8

Re: How to call custom actions using PCF

I shared it in the first post, that's an example of calling the WinOpportunity action using fetch, and I linked to the documentation supporting it..

ben-thompson
Level 8

Re: How to call custom actions using PCF

If we are talking about webapi calls to actions this is the code we use using XMLHttpRequest.

 

 

	private async getLicence(product:string): Promise<any>{
		var id=	Xrm.Utility.getGlobalContext();
		var parameters = {
			ProductName : product
		};
		var req = new XMLHttpRequest();
		
		return new Promise(function (resolve, reject) {
			req.open("POST", id.getClientUrl() + "/api/data/v9.0/hdn_ValidateLicence", true);			
			req.onreadystatechange = function () {
				if (req.readyState !== 4) return;
				if (req.status >= 200 && req.status < 300) {				
					// If successful
					try {
						var result=JSON.parse(req.response);
						if (parseInt(result.StatusCode)<0){
							reject({
								status: result.StatusCode,
								statusText: result.StatusMessage
							});
						}						
						resolve(req.response);
					}
					catch (error){
						throw error;
					}
					
				} else {
					// If failed
					reject({
						status: req.status,
						statusText: req.statusText
					});
				}
	
			};
			req.setRequestHeader("OData-MaxVersion", "4.0");
			req.setRequestHeader("OData-Version", "4.0");
			req.setRequestHeader("Accept", "application/json");
			req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
			req.send(JSON.stringify(parameters));
		});	
	}

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (4,746)