Forrest Kribs
Forrest Kribs

Reputation: 67

How do I extract a string value from my PowerApps custom connector response?

I need to be able to retrieve values from an API custom connector and store them in a variable (using UpdateContext). For example, if my API response is {"result": 100}, I'd want to put the value 100 into the text property of a Label.

I am trying to replace a Microsoft Flow that already works (see the first formula below). I would like to format my API response so that it populates variables in the same way that the Flow did.

I've tried using the Set() function, setting a global variable to the results of my custom connector request. I have also tried using ClearCollect(). (see below)

This is the formula that works as intended. This is the existing Microsoft Flow that I'm trying to replace.

Set(varDefaults,GetGasDefaultValues.Run());UpdateContext({v6:varDefaults.specificgravity});UpdateContext({v7:varDefaults.co2});UpdateContext({v8:varDefaults.n2})

I have tried using the 'Set' function. This formula gives the following error: "Name isn't valid..." and "Invalid use of '.' "

Set(varDefaults,GasVolumeCalculatorConnector.CalcVol();UpdateContext({v6:varDefaults.specificgravity});UpdateContext({v7:varDefaults.co2});UpdateContext({v8:varDefaults.n2})

I have also tried using the 'ClearCollect' function. This formula gives the following error: "Incompatible type. We can't evaluate your formula because the context variable types are incompatible with the types of values in other places in your app."

ClearCollect(defaultCollection, GasVolumeCalculatorConnector.GetDefaults());UpdateContext({v6:First(defaultCollection).specific_gravity});UpdateContext({v7:First(defaultCollection).co2});UpdateContext({v8:First(defaultCollection).n2})

I do not wish to change the types of my existing variables. How can I format my API response so that it matches the previous Flows response? (see first formula)

Upvotes: 0

Views: 2065

Answers (1)

Forrest Kribs
Forrest Kribs

Reputation: 67

I found the solution to this problem. The issue was not that the API connector was returning a different type than the Flow, which is what I originally thought.

When I replaced the Flow connector with my new API connector, I did not update all locations where that Flow was being referenced. While most variable definitions were of type 'Text', one was of type 'Error' because it was still using the old Flow connector.

By going to the 'View' tab and selecting the 'Variables' option, I was able to select a particular variable and see all current definitions. This helped me diagnose the issue that I mentioned.

Here is the code that successfully extracted text values from the API response:

ClearCollect(collectionName, Connector.Operation({param1: "val1", param2: "val2"}));
UpdateContext({variableName: Text(First(collectionName).attributeName)})

Upvotes: 1

Related Questions