cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Post Prodigy
Post Prodigy

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
Highlighted

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

Highlighted
Post Prodigy
Post Prodigy

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

Highlighted

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.

Highlighted
Advocate II
Advocate II

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.

Highlighted
Post Prodigy
Post Prodigy

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

Highlighted
Advocate II
Advocate II

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
Post Prodigy
Post Prodigy

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 

Highlighted
Advocate II
Advocate II

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..

Impactful Individual
Impactful Individual

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));
		});	
	}
---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Users online (7,492)