• V20: 3CX Re-engineered. Get V20 for increased security, better call management, a new admin console and Windows softphone. Learn More.

How to return string values to a Callflow Variable from external c# Script

Status
Not open for further replies.

Voipist

Bronze Partner
Basic Certified
Joined
Feb 19, 2018
Messages
20
Reaction score
1
I call a rest service that gives me customer information as json (status, language, etc.). I deserialize the json. That works everything. Now would like to return the parameters to a callflow variable. How does it work?
Thank you very much
 
Hello @Voipist,

You're using a C# script to deserialize the JSON, correct? In that case, you're using a Launch External Script component to invoke that C# code. The C# method you use should return a string, for example:

public string GetSomeJsonPart(string json)
{
// Do some work...
return "my json part";
}

Then if for example your Launch External Script component name is "jsonParser", then you will have a variable "jsonParser.ReturnValue". You can assign that value to a callflow variable using an "Assign a Variable" component.

Kind regards.
 
  • Like
Reactions: Mekaeil Andisheh
Helo @edossantos,
Thanks for the answer. As described, I have already implemented that and it works fine, but there are many values to return. Is it possible to return an array or assign a value directly to callflow variables
Thx
 
Hello @Voipist,

You can't assign callflow variables from your script. You will need to return N strings and then use N "Assign a variable" components to assign each variable.

For example, if you return an array of strings:
Code:
public string[] GetSomeJsonPart(string json)
{
  // your code...
}

Then you can use the following expression for your "Assign a variable" component, to get the first string:
Code:
((string[])jsonParser.ReturnValue)[0]

Then for the second string you can use:
Code:
((string[])jsonParser.ReturnValue)[1]

and so on...

But this will lead to errors if you change the order of the returned values, or add a new value to be returned. Maybe it's better to return a "struct", and then use the fields by name. For example:
Code:
public struct MyReturnData
{
  public string MyData1;
  public string MyData2;
  public string MyDataN;
}

Then your method should be:
Code:
public MyReturnData GetSomeJsonPart(string json)
{
  // your code...
}

And the expressions to use with your "Assign a variable" components:
Code:
((MyReturnData)jsonParser.ReturnValue).MyData1

Kind regards.
 
  • Like
Reactions: Mekaeil Andisheh
Hello @ Edossantos,
thank you for the helpful responses. I tried it and it works!
Kind regards
 
Status
Not open for further replies.
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.