cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ManishJain
Solution Sage
Solution Sage

How to call custom actions using PCF

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

 

Thanks

18 REPLIES 18

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

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

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.

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.

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

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.

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 

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
Memorable Member
Memorable Member

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
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Users online (2,582)