3 weeks ago
Hi, everyone! I need help with something.
I need to know if there is a way to reference action specific parameters, declared in the parameters tab of an API request, in a custom code request response.
I made a code to extract some data from the response, but I can't make it work. The used code is shown below:
Dim dt As New DataTable()
' Define ALL columns
dt.Columns.Add("idConta", GetType(String))
dt.Columns.Add("situacaoProcessamento", GetType(String))
dt.Columns.Add("pagamentoEfetuado", GetType(Boolean))
dt.Columns.Add("dataVencimentoFatura", GetType(DateTime))
dt.Columns.Add("dataVencimentoReal", GetType(DateTime))
dt.Columns.Add("dataFechamento", GetType(DateTime))
dt.Columns.Add("valorTotal", GetType(Double))
dt.Columns.Add("valorPagamentoMinimo", GetType(Double))
dt.Columns.Add("valorDebitosFatura", GetType(Double))
dt.Columns.Add("valorPagamentoEfetuado", GetType(Double))
dt.Columns.Add("saldoAtualizado", GetType(Double))
dt.Columns.Add("atual", GetType(Boolean))
' Parse JSON
Dim json As JObject = JObject.Parse(Response_Content)
Dim content As JArray = json("content")
' Date filter
Dim dataInicio As DateTime = DateTime.Parse(Initial_Date)
Dim dataFim As DateTime = DateTime.Parse(Final_Date)
For Each item As JObject In content
If Not IsNothing(item("dataVencimentoReal")) Then
Dim dataVenc As DateTime = DateTime.Parse(item("dataVencimentoReal").ToString())
If dataVenc >= dataInicio AndAlso dataVenc <= dataFim Then
Dim row As DataRow = dt.NewRow()
' Strings
row("idConta") = item("idConta").ToString()
row("situacaoProcessamento") = item("situacaoProcessamento").ToString()
' Booleans
row("pagamentoEfetuado") = If(item("pagamentoEfetuado") IsNot Nothing, CBool(item("pagamentoEfetuado")), False)
row("atual") = If(item("atual") IsNot Nothing AndAlso Not IsDBNull(item("atual")), CBool(item("atual")), False)
' Dates
If item("dataVencimentoFatura") IsNot Nothing Then
row("dataVencimentoFatura") = DateTime.Parse(item("dataVencimentoFatura").ToString())
End If
row("dataVencimentoReal") = dataVenc
If item("dataFechamento") IsNot Nothing Then
row("dataFechamento") = DateTime.Parse(item("dataFechamento").ToString())
End If
' Numbers (safe conversion)
row("valorTotal") = If(item("valorTotal") IsNot Nothing, CDbl(item("valorTotal")), 0)
row("valorPagamentoMinimo") = If(item("valorPagamentoMinimo") IsNot Nothing AndAlso item("valorPagamentoMinimo").ToString() <> "", CDbl(item("valorPagamentoMinimo")), 0)
row("valorDebitosFatura") = If(item("valorDebitosFatura") IsNot Nothing, CDbl(item("valorDebitosFatura")), 0)
row("valorPagamentoEfetuado") = If(item("valorPagamentoEfetuado") IsNot Nothing AndAlso item("valorPagamentoEfetuado").ToString() <> "", CDbl(item("valorPagamentoEfetuado")), 0)
row("saldoAtualizado") = If(item("saldoAtualizado") IsNot Nothing, CDbl(item("saldoAtualizado")), 0)
dt.Rows.Add(row)
End If
End If
Next
Invoices = dtAs you can see, I have declared the parameters in the parameters tab for the API request, but they are not showed in the bottom corner, like the other parameters.
Did I do something wrong or it's just not possible to do it?
Answered! Go to Answer.
3 weeks ago
I see what you're saying now. I probably just didn't look closely enough. I don't think it's possible to do that. The problem is that the input params are designed only to be sent to the API endpoint, and all the Response area (such as the custom code) has access to is the response from the API. Now, that said, sometimes for APIs, you can include your own custom properties or a header or whatever, and the API will respond with those same values returned to you, and then you could use it like you're saying. I believe the right way for you to handle it is to do this in an object (VBO) like you described.
3 weeks ago
I may be reading this wrong, but I'll say some words and maybe it's what you're asking.
The "Parameters" are only input parameters I believe, especially given that you cannot select it as In vs Out. To add outputs, I think you'd have to add them to the Response list there underneath Invoices and then use either custom code or json path to extract each of the dates. Now I'll say this: I have perhaps never used custom code there. I've always either used Json Path or passed the whole response and parsed in a Blue Prism object (VBO) using either BP stages or C# there in the VBO. So, I don't know for sure that what I'm saying is correct, but I would assume you need separate custom code for extracting each output param.
3 weeks ago
Hi, david! Thanks for the response.
But what I want it's this: I want to grab the input parameters, 'Initial Date' and 'Final Date', and pass its values into the response, inside a custom code, so I can filter the 'Response_Content' based on this dates. They were not meant to be output parameters.
I imagined that I could use the input parameters as part of the custom code to extract the response.
Sorry if I didn't explain this the right way.
3 weeks ago
Just to test it out, I took the code and put it right into a code stage, inside a normal object. Then, I setted the 'Initial Date', 'Final Date' and 'Response Content' as input parameters; and the 'Invoices' collection as an output, just like I would have in the web API services section of Blue Prism. Then I setted the current value for the 'Response Request' with the actual API response I got from the request. The code ran beautifully.
Follow the images for the situation I described as well as the code I used for this test.
Dim dt As New DataTable()
' Define ALL columns
dt.Columns.Add("idConta", GetType(String))
dt.Columns.Add("situacaoProcessamento", GetType(String))
dt.Columns.Add("pagamentoEfetuado", GetType(Boolean))
dt.Columns.Add("dataVencimentoFatura", GetType(DateTime))
dt.Columns.Add("dataVencimentoReal", GetType(DateTime))
dt.Columns.Add("dataFechamento", GetType(DateTime))
dt.Columns.Add("valorTotal", GetType(Double))
dt.Columns.Add("valorPagamentoMinimo", GetType(Double))
dt.Columns.Add("valorDebitosFatura", GetType(Double))
dt.Columns.Add("valorPagamentoEfetuado", GetType(Double))
dt.Columns.Add("saldoAtualizado", GetType(Double))
dt.Columns.Add("atual", GetType(Boolean))
' Parse JSON safely
Dim json As JObject = JObject.Parse(Response_Content)
Dim content As JArray = If(json("content"), New JArray())
' --------------------------
' SAFE INPUT DATE PARSING
' --------------------------
Dim dataInicio As DateTime
Dim dataFim As DateTime
If Not DateTime.TryParse(Initial_Date, dataInicio) Then
Throw New Exception("Invalid Initial Date: " & Initial_Date)
End If
If Not DateTime.TryParse(Final_Date, dataFim) Then
Throw New Exception("Invalid Final Date: " & Final_Date)
End If
' --------------------------
' MAIN LOOP WITH DEBUG TRAP
' --------------------------
For Each item As JObject In content
Try
' --------------------------
' SAFE REQUIRED DATE
' --------------------------
If item("dataVencimentoReal") Is Nothing OrElse item("dataVencimentoReal").ToString().Trim() = "" Then
Continue For
End If
Dim dataVenc As DateTime
If Not DateTime.TryParse(item("dataVencimentoReal").ToString(), dataVenc) Then
Continue For
End If
' Filter by input dates
If dataVenc < dataInicio OrElse dataVenc > dataFim Then
Continue For
End If
Dim row As DataRow = dt.NewRow()
' --------------------------
' STRINGS
' --------------------------
row("idConta") = If(item("idConta") IsNot Nothing, item("idConta").ToString(), "")
row("situacaoProcessamento") = If(item("situacaoProcessamento") IsNot Nothing, item("situacaoProcessamento").ToString(), "")
' --------------------------
' BOOLEANS
' --------------------------
Dim boolTemp As Boolean
If Boolean.TryParse(If(item("pagamentoEfetuado"), "").ToString(), boolTemp) Then
row("pagamentoEfetuado") = boolTemp
Else
row("pagamentoEfetuado") = False
End If
If Boolean.TryParse(If(item("atual"), "").ToString(), boolTemp) Then
row("atual") = boolTemp
Else
row("atual") = False
End If
' --------------------------
' OPTIONAL DATES
' --------------------------
Dim tempDate As DateTime
If item("dataVencimentoFatura") IsNot Nothing AndAlso item("dataVencimentoFatura").ToString().Trim() <> "" Then
If DateTime.TryParse(item("dataVencimentoFatura").ToString(), tempDate) Then
row("dataVencimentoFatura") = tempDate
End If
End If
row("dataVencimentoReal") = dataVenc
If item("dataFechamento") IsNot Nothing AndAlso item("dataFechamento").ToString().Trim() <> "" Then
If DateTime.TryParse(item("dataFechamento").ToString(), tempDate) Then
row("dataFechamento") = tempDate
End If
End If
' --------------------------
' NUMBERS
' --------------------------
Dim dblTemp As Double
If Double.TryParse(If(item("valorTotal"), "0").ToString(), dblTemp) Then
row("valorTotal") = dblTemp
Else
row("valorTotal") = 0
End If
If Double.TryParse(If(item("valorPagamentoMinimo"), "0").ToString(), dblTemp) Then
row("valorPagamentoMinimo") = dblTemp
Else
row("valorPagamentoMinimo") = 0
End If
If Double.TryParse(If(item("valorDebitosFatura"), "0").ToString(), dblTemp) Then
row("valorDebitosFatura") = dblTemp
Else
row("valorDebitosFatura") = 0
End If
If Double.TryParse(If(item("valorPagamentoEfetuado"), "0").ToString(), dblTemp) Then
row("valorPagamentoEfetuado") = dblTemp
Else
row("valorPagamentoEfetuado") = 0
End If
If Double.TryParse(If(item("saldoAtualizado"), "0").ToString(), dblTemp) Then
row("saldoAtualizado") = dblTemp
Else
row("saldoAtualizado") = 0
End If
dt.Rows.Add(row)
Catch ex As Exception
Throw New Exception("Error processing item: " & item.ToString() & " | " & ex.Message)
End Try
Next
Invoices = dt
3 weeks ago
I see what you're saying now. I probably just didn't look closely enough. I don't think it's possible to do that. The problem is that the input params are designed only to be sent to the API endpoint, and all the Response area (such as the custom code) has access to is the response from the API. Now, that said, sometimes for APIs, you can include your own custom properties or a header or whatever, and the API will respond with those same values returned to you, and then you could use it like you're saying. I believe the right way for you to handle it is to do this in an object (VBO) like you described.
3 weeks ago
Thank you very much, Dave! I get it now. I think I'll proceed with an object.
By the way, your youtube videos helped me so much! Thank you for that as well.
3 weeks ago
I'm glad the videos helped! ❤️