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

Discussion in 'Call Flow Designer' started by Voipist, Mar 10, 2018.

Thread Status:
Not open for further replies.
  1. Voipist

    Joined:
    Feb 19, 2018
    Messages:
    13
    Likes Received:
    0
    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
     
  2. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,367
    Likes Received:
    110
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    Mekaeil Andisheh likes this.
  3. Voipist

    Joined:
    Feb 19, 2018
    Messages:
    13
    Likes Received:
    0
    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
     
  4. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,367
    Likes Received:
    110
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
    Mekaeil Andisheh likes this.
  5. Voipist

    Joined:
    Feb 19, 2018
    Messages:
    13
    Likes Received:
    0
    Hello @ Edossantos,
    thank you for the helpful responses. I tried it and it works!
    Kind regards
     
Thread Status:
Not open for further replies.