web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Custom API for numeric...
Power Apps
Answered

Custom API for numeric formulas

(1) ShareShare
ReportReport
Posted on by 873

Hello, following this great post and substituting the C# code with the below (no changes required to the JSON code) I have created a function that returns the factorial of any integer between 0 and 170 or returns -1 if the input is not an integer or out of the range.

The problem is that the number is returned as a string.  Could anyone suggest any changes to the C# and JSON code to make this happen?  I have tried to change the "string" to "number" and "double" in both the C# and JSON but it has not worked.

 

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
 log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

 // parse query parameter
 string name = req.GetQueryNameValuePairs()
 .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
 .Value;

 // Get request body
 dynamic data = await req.Content.ReadAsAsync<object>();

 // Set name to query string or body data
 name = name ?? data?.name;

 // Initialise the return variable
 double factorial = -1;
 
 // Convert the input to a short integer and calculate factorial. Result is -1 if input is not an integer between 0 and 170
 ushort n;
 if (ushort.TryParse(name, out n))
 {
 if (n <= 170)
 // If input is >170 then it will be out of range for double type variable 
 { 
 if (n == 0) 
 {
 factorial = 1;
 // Factorial of 0 is 1
 } 
 else 
 {
 int numberInt = n;
 factorial = n;
 for (int i = 1; i < n; i++)
 {
 factorial = factorial * i;
 }
 } 
 } 
 }
 return req.CreateResponse(HttpStatusCode.OK, factorial.ToString());
 // return the result as a string since I don't know how to return a number to PowerApps
}

 

Categories:
I have the same question (0)
  • dtsiniavskyi Profile Picture
    78 on at

    Just make your api action to return int instead of HttpResponseMessage. So it will be treated as int in your powerapp. Because content of your HttpResponseMessage will always be non generic object and you wont know the actual type of it's properties in the powerapp.

  • Steelman70 Profile Picture
    873 on at

    dtsiniavskyi, thank you.  Apologies but I am just a beginner in APIs.  Should I just replace HttpResponseMessage with int in the C# code like this?

     

    public static async Task<int> Run(HttpRequestMessage req, TraceWriter log)

     

    Any changes to the JSON?

     

    Also, because the factorial returns large numbers, I would ideally need to be able to return a double.

  • dtsiniavskyi Profile Picture
    78 on at

    Yes, just replace HttpResponseMessage with the type you need. I know for sure that int can be returned easily and it will be recognized as a number within your powerapp, not sure about double, but you could try. Maybe decimal will be the best option.

  • Steelman70 Profile Picture
    873 on at

    The Azure function compiler gives me this...

    2016-05-31T15:36:38.572 run.csx(53,12): error CS0029: Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'int'
    2016-05-31T15:36:38.572 Compilation failed.

     

    PS I can easily make the C# return a double by just removing the .ToString() in the third but last line, but then PowerApps will not show any result 

  • Verified answer
    dtsiniavskyi Profile Picture
    78 on at
    using System.Net;
    
    public static async Task<decimal> Run(HttpRequestMessage req, TraceWriter log)
    {
     log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
    
     // parse query parameter
     string name = req.GetQueryNameValuePairs()
     .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
     .Value;
    
     // Get request body
     dynamic data = await req.Content.ReadAsAsync<object>();
    
     // Set name to query string or body data
     name = name ?? data?.name;
    
     // Initialise the return variable
     double factorial = -1;
     
     // Convert the input to a short integer and calculate factorial. Result is -1 if input is not an integer between 0 and 170
     ushort n;
     if (ushort.TryParse(name, out n))
     {
     if (n <= 170)
     // If input is >170 then it will be out of range for double type variable 
     { 
     if (n == 0) 
     {
     factorial = 1;
     // Factorial of 0 is 1
     } 
     else 
     {
     int numberInt = n;
     factorial = n;
     for (int i = 1; i < n; i++)
     {
     factorial = factorial * i;
     }
     } 
     } 
     }
     return (decimal)factorial;
    }

     

    That's how I think your api action should look. 

  • Verified answer
    Steelman70 Profile Picture
    873 on at

     

    I played around a bit and the final few lines of the JSON file need to be edited as follows.  Not only does the "string" need to be substituted with "integer", but also "format": "int32" needs to be added.

     

     "responses": {
     "200": {
     "description": "Successful response",
     "schema": {
     "title": "The response of the api.",
     "type": "integer",
     "format": "int32"
     }
     }
     }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 483

#2
WarrenBelz Profile Picture

WarrenBelz 399 Most Valuable Professional

#3
11manish Profile Picture

11manish 327

Last 30 days Overall leaderboard