Comming from BluePrism, cant get c# code to work in Invoke Code

I have this code that i cant get to run.

using Newtonsoft.Json.Linq;
using System.Collections.Generic;

// Deserialize the JSON string to a JObject
JObject jsonObject = JObject.Parse(Response_Users);

// Extract the array of users from the JSON object
JArray usersArray = (JArray)jsonObject[“users”];

// Create a list to hold the user data
var userList = new List();

for (int i = 0; i < usersArray.Count; i++)
{
JObject userObject = (JObject)usersArray[i];
userList.Add(new User
{
DisplayName = (string)userObject[“displayName”],
Mail = (string)userObject[“mail”],
Id = (string)userObject[“id”],
OnPremisesDistinguishedName = (string)userObject[“onPremisesDistinguishedName”]
});
}

// Define the User class
public class User
{
public string DisplayName { get; set; }
public string Mail { get; set; }
public string Id { get; set; }
public string OnPremisesDistinguishedName { get; set; }
}

Can anybody help.
I am coming from BluePrism where i would define classes in global code in the object editor, and the rest in a code Block.

Hi @lasr,

Welcome to UiPath Community!

Try coded workflow or coded source file.

Steps to create coded workflow,

  1. Open studio
  2. Go to Design tab > New > Coded workflow

Hi @lasr

Use the below C# code in Invoke Code activity:

var jsonObject = JObject.Parse(Response_Users);
var usersArray = (JArray)jsonObject["users"];

userList = usersArray
    .Select(u => {
        var user = (JObject)u;
        return new List<string>
        {
            (string)user["displayName"],
            (string)user["mail"],
            (string)user["id"],
            (string)user["onPremisesDistinguishedName"]
        };
    })
    .ToList();

Invoke Code arguments:

Run a For Each loop for userData and inside that you can print the below values in Write Line

For Each item in userList
    WriteLine -> item(0) → DisplayName
    WriteLine -> item(1) → Mail
    WriteLine -> item(2) → Id
    WriteLine -> item(3) → OnPremisesDistinguishedName
End For Each

Workflow:

Hope it helps!!

@lasr,

Make sure you are changing Invoke Code activity Language property to CSharp

Thanks a lot. That was what i needed.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.