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

Re: Calling REST web api from PCF

if your using the fetch api be sure to include a polyfill for IE  Man Wink

PowerApps Staff HemantG
PowerApps Staff

Re: Calling REST web api from PCF


@ben-thompson wrote:

This is the code we use to retrieve data via the web api (it's 2 separate functions as it promises and so the first one is a try catch handler and the second one does the heavy lifting. 

 

public async retrieveCRMRecords (entityName: string, query: string 😞 Promise<Xrm.RetrieveMultipleResult>{
        try {
        let result=await    this.getResults(entityName,query);
        return result;
        }
        catch (error){
            // display error message within error section of tile
            
            return Promise.reject(error);
        }
        finally {}
        
    }

    public async getResults(entityName: string, query: string 😞 Promise<Xrm.RetrieveMultipleResult> {
        let a: Promise<Xrm.RetrieveMultipleResult>;
            return Xrm.WebApi.retrieveMultipleRecords(entityName,query).then(
            function success(result) {
            //  return result.entities;
             return result;
            },
            function (error) {
                return Promise.reject(error);                
            }
            )
    }
 
which we then call using
 
        let result= await this.retrieveCRMRecords("entityName",webapiQuery say"?$select=hdn_cssstyle,hdn_name&$filter=_hdn_template_value%20eq%20"+tId);
   
        result.entities.forEach(element => {
//do something with each record.
}
You should be able to repurpose the above to work without much difficulty but don't forget about CORS

Curious why didn't you used webAPI interface from PCF for this as opposed to direct XRM which is not supported PCF pattern ? Sample is published here  - https://docs.microsoft.com/en-us/powerapps/developer/component-framework/sample-controls/webapi-cont...

 

Please see -

private calculateAverageButtonOnClickHandler() : void

--------
// Invoke the Web API RetrieveMultipleRecords method to calculate the aggregate value
this._context.webAPI.retrieveMultipleRecords(TSWebAPI._entityName, "?fetchXml=" + fetchXML).then
(
	function (response: ComponentFramework.WebApi.RetrieveMultipleResponse) 
	{
		// Retrieve multiple completed successfully -- retrieve the averageValue 
		let averageVal:Number = response.entities[0].average_val;
		// Generate HTML to inject into the result div to showcase the result of the RetrieveMultiple Web API call
		let resultHTML: string = "Average value of " + TSWebAPI._currencyAttributeNameFriendlyName + " attribute for all " + TSWebAPI._entityName + " records: " + averageVal;
		thisRef.updateResultContainerText(resultHTML);
	},
	function (errorResponse: any) 
	{
		// Error handling code here
		thisRef.updateResultContainerTextWithErrorResponse(errorResponse);
	}
);

 

indikaa
Level: Powered On

Re: Calling REST web api from PCF

Hi @HemantG , 

Sorry for the late reply. Our requirement to call a 3rd party API via APIM. Yes if we had to query CRM we would definitely use the CRM web API.

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 (5,272)