Read Data
script task
7.12
studio

Let's say I read one object using Read data step and I choose to only read columns First name and Last name. How do I reference that data in Script task? I know I can copy those strings from step parameters to process parameters using Formula step but maybe there is a better way.

Like 0

Like

15 comments

Dear Carlos,

For the [Script-task] elements and methods that have the [For an interpreted process] checkbox selected, the wrapper class is generated that contains the initialization and declaration of methods. This wrapper enables you to access the process values. 

The Get method returns the value of an item or process.

Method signature:

Get<T>(string path)

where:

  • T — parameter value type.
  • path — a string that specifies the path to a parameter or property. The path is built according to these rules:
  1. “parameter name”
  2. “property name”
  3. “element name.parameter name”
  4. “element name.property name”

Oliver

Oliver,

I don't see a parameter First name though. The only related parameter that I see is Columns to read.

Carlos Zaldivar Batista,

You don't need any parameters and select any specific columns to read in the element Read Data, simply use “element name.property name” to get the field value in the Script task.

Oliver

Oleh Chudiiovych,

Ok, so I have a step with code ReadSomeData and I try to read the columns of returned object like this:

Get<Entity>("ReadSomeData.ResultEntity").GetTypedColumnValue<string>("UsrFirstName")

I get NullReferenceException though because the Get method returned null.

Try read data from all columns. Also possible for your query (filter) no records? Do it work if you remove the filter

Grigoriy,

I read data from all columns and the query returns records.

Dear Carlos,

As far as we can see from your method you are trying to read values from the "UsrFirstName" column and we don't know what data is stored there and what are you trying to read. Please re-check all your columns and also try Grigoriy's suggestion about filters.

Oscar

Ok, so I created a new trial environment with default test data and in that environment I created a process:

 

The Read data step looks like this:

I set up the step name too:

And that is my Script task code:

Name is a process parameter that is shown in the auto-generated screen. That's what the screen shows after running the process:

There are no filters in the read data step and the data is there. What should I change to make the script task work?

Hi by default read data have code "ReadDataUserTask1".

Try this:

var entity = Get<Entity>("ReadDataUserTask1.ResultEntity"); 

 

See read data advanced mode for get elenent code (

In the advanced mode, the element setup area contains additional parameters and connections with system records

To access the advanced mode, click the btn_advanced_mode.png button in the element setup area and select the [Advanced mode] menu command

)

Sory. For interpretable processes, you can only directly work with the methods and parameters of the elements through the formula.

In autogenerate page set page item via formula like:

[#Read data 1.First item of resulting collection.First name#] 

Grigoriy,

Thank you. And is it possible to use this formula in a script tasks?

Yes

1. Create a process parameter of type String - TestParam.

2. In the parameter value specify [#Read data 1.First item of the resulting collectionFirst name#].

3. In the Task-script element, work with the parameter.

     var contactFullName = Get<string> ("TestParam");

Grigoriy,

Ok, thank you.

Does anyone know if this is still the case? i.e. that it's necessary to save each individual field to a parameter in order to access them inside a Script Task element of a Business Process? It seems like it really should be simpler to access data on any of the fields!

 

I am aware of being able to query the entity directly within the Script Task, but I'd like to keep as much of the business logic in easy-to-read BP steps as possible, including reading data.

Dear Harvey,

 

Yes, the logic remains the same. There is still to call for read data element through the script task without the intermediate process parameters or settings. This logic will be review by our developers, it is confirmed by our R&D but we do not know when it will be done.

 

Regards,

Dean

 

Show all comments
DataServices
Read Data
Filtering
7.12
sales

Hi Team,

I am fetching list of records from account which are modified yesterday or certain date. But it throws Internal Server Error. 

Below is my code snippet:

 



         var selectFilters = new Filters()

         {



                    FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.FilterGroup,



                Items = new Dictionary

                {

                    {

                        "FilterByCreatedOn",

                        // Value.

                        new Filter

                        {



                            FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,



                            ComparisonType = FilterComparisonType.GreaterOrEqual,



                            LeftExpression = new BaseExpression ()

                            {

                                // Expression type - SchemaColumn.

                                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                                // Path to the column relative to the root schema.

                                ColumnPath = "ModifiedOn",

                            },

                            // Filter range final expression.

                            RightExpression= new BaseExpression ()

                            {



                                ExpressionType = EntitySchemaQueryExpressionType.Parameter,



                                Parameter = new Parameter ()

                                {



                                    DataValueType = DataValueType.DateTime,

                                    Value = "5/11/2018" or new DateTime(2018,05,11)

                                }

                            }

                        }

                    }

                }

           };

 

Like 0

Like

11 comments

Hi

If you remember, bpmonline itself uses DataService for connecting to server. So the better way to apply a correct filter is to look at a browser console. Just apply an advanced filter you want in the interface and then watch Network section in the browser console.

In c# you should use a standart TimeStamp: 

const string startDate = @"""\""2000-12-31T09:41:59\"""""""""";

 

Peter Vdovukhin,

Thank you so much. It is really helpful for me. I have tried exactly like this but still getting Internal Server Error.

I do not understand, It was helpful or you still getting the error?

It is helpful that I get to know some of the things from this. But I have tried exactly like console filter and still getting error. Not able to resolve issue.

Please, post here a complete code example with validation. So I could reproduce it under my environment

 

 

 

 

 

 

Here is my complete code of my console application

 

 class ResponseStatus

    {

        public int Code { get; set; }

        public string Message { get; set; }

        public object Exception { get; set; }

        public object PasswordChangeUrl { get; set; }

        public object RedirectUrl { get; set; }

    }

    class Program

    {

        private const string baseUri = @"https://[example].bpmonline.com";

        // Query string to the Login method of the AuthService.svc service.

        private const string authServiceUri = baseUri + @"/ServiceModel/AuthService.svc/Login";

        // SelectQuery path string.

        private const string selectQueryUri = baseUri + @"/0/DataService/json/SyncReply/SelectQuery";

        // Bpm'online authentication cookie.

        private static CookieContainer AuthCookie = new CookieContainer();

        private static bool TryLogin(string userName, string userPassword)

        {

            // Creating an instance of the authentication service request.

            var authRequest = HttpWebRequest.Create(authServiceUri) as HttpWebRequest;

            // Defining the request's method.

            authRequest.Method = "POST";

            // Defining the request's content type.

            authRequest.ContentType = "application/json";

            // Enabling the use of cookie in the request.

            authRequest.CookieContainer = AuthCookie;

            // Placing user credentials to the request.

            using (var requestStream = authRequest.GetRequestStream())

            {

                using (var writer = new StreamWriter(requestStream))

                {

                    writer.Write(@"{

                ""UserName"":""" + userName + @""",

                ""UserPassword"":""" + userPassword + @"""

                }");

                }

            }

            // Auxiliary object where the HTTP reply data will be de-serialized.

            ResponseStatus status = null;

            // Getting a reply from the server. If the authentication is successful, cookie will be placed to the AuthCookie property.

            // These cookies can be used for subsequent requests.

            using (var response = (HttpWebResponse)authRequest.GetResponse())

            {

                using (var reader = new StreamReader(response.GetResponseStream()))

                {

                    // De-serialization of the HTTP reply to an auxiliary object.

                    string responseText = reader.ReadToEnd();

                    status = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ResponseStatus>(responseText);

                }

            }

            // Checking authentication status.

            if (status != null)

            {

                // Authentication is successful.

                if (status.Code == 0)

                {

                    return true;

                }

                // Authentication is unsuccessful.

                Console.WriteLine(status.Message);

            }

            return false;

        }

        static void AddAuthCookie(HttpWebRequest request)

        {

            request.CookieContainer = AuthCookie;

            // Adding a CSRF token to the request title.

            CookieCollection cookieCollection = AuthCookie.GetCookies(new Uri(authServiceUri));

            string csrfToken = cookieCollection["BPMCSRF"].Value;

            ((HttpWebRequest)request).Headers.Add("BPMCSRF", csrfToken);

        }

        static void Main(string[] args)

        {

            if (!TryLogin("Supervisor", "Supervisor1"))

            {

                return;

            }

            try

            {

                var selectQuery = new SelectQuery()

                {

                    RootSchemaName = "Account",

                    Columns = new SelectQueryColumns()

                };

                var columnExpressionName = new ColumnExpression()

                {

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    ColumnPath = "Name"

                };

                var selectQueryColumnName = new SelectQueryColumn()

                {

                    Caption = "Company Name",

                    OrderDirection = OrderDirection.Ascending,

                    // Sorting order position.

                    OrderPosition = 0,

                    // Expression that specifies column type.

                    Expression = columnExpressionName

                };

                var columnExpressionContactName = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "PrimaryContact",

                };

                var selectQueryColumnContactName = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Full Name",

                    // Expression, which specifies column type.

                    Expression = columnExpressionContactName

                };

                var columnExpressionJobTitle = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "JobTitle"

                };

                var selectQueryColumnJobTitle = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Job Title",

                    // Sorting direction — ascending.

                    OrderDirection = OrderDirection.Ascending,

                    // Sorting order position.

                    OrderPosition = 1,

                    // Expression, which specifies column type.

                    Expression = columnExpressionJobTitle

                };

                var columnExpressionREPCode = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "UsrREPCode"

                };

                var selectQueryColumnREPCode = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "REP Code",

                    // Sorting direction — ascending.

                    OrderDirection = OrderDirection.Ascending,

                    // Sorting order position.

                    OrderPosition = 2,

                    // Expression, which specifies column type.

                    Expression = columnExpressionREPCode

                };

                var columnExpressionDAC = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "UsrDAC"

                };

                var selectQueryColumnDAC = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "DAC",

                    // Expression, which specifies column type.

                    Expression = columnExpressionDAC

                };

                var columnExpressionOwner = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "Owner"

                };

                var selectQueryColumnOwner = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Owner",

                    // Sorting direction — ascending.

                    OrderDirection = OrderDirection.Ascending,

                    // Sorting order position.

                    OrderPosition = 4,

                    // Expression, which specifies column type.

                    Expression = columnExpressionOwner

                };

                var columnExpressionPhoneNumber = new ColumnExpression()

                {

                    // Expression type — SchemaColumn.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "Phone"

                };

                var selectQueryColumnPhoneNumber = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Contact Number",

                    // Expression, which specifies column type.

                    Expression = columnExpressionPhoneNumber

                };

                var columnExpressionEmail = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SubQuery,

                    // Path to column in relation to root schema.

                    ColumnPath = "[Account:Contact].Email"

                };

                var selectQueryColumnEmail = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Email",

                    // Sorting direction — ascending.

                    OrderDirection = OrderDirection.Ascending,

                    // Sorting order position.

                    OrderPosition = 1,

                    // Expression, which specifies column type.

                    Expression = columnExpressionEmail

                };

                var columnExpressionCity = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "City"

                };

                var selectQueryColumnCity = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "City",

                    // Expression, which specifies column type.

                    Expression = columnExpressionCity

                };

                var columnExpressionState = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "Region"

                };

                var selectQueryColumnState = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "State",

                    // Expression, which specifies column type.

                    Expression = columnExpressionState

                };

                var columnExpressionCountry = new ColumnExpression()

                {

                    // Expression type — subquery.

                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                    // Path to column in relation to root schema.

                    ColumnPath = "Country"

                };

                var selectQueryColumnCountry = new SelectQueryColumn()

                {

                    //Title.

                    Caption = "Country",

                    // Expression, which specifies column type.

                    Expression = columnExpressionCountry

                };



                // Adding columns to query.

                selectQuery.Columns.Items = new Dictionary<string, SelectQueryColumn>()

                {

                    {

                        "Company Name",

                        selectQueryColumnName

                    },

                    {

                        "Full Name",

                        selectQueryColumnContactName

                    },

                    {

                        "Job Title",

                        selectQueryColumnJobTitle

                    },

                    {

                        "REP Code",

                        selectQueryColumnREPCode

                    },

                    {

                        "DAC",

                        selectQueryColumnDAC

                    },

                    {

                        "Owner",

                        selectQueryColumnOwner

                    },

                    {

                        "Contact Number",

                        selectQueryColumnPhoneNumber

                    },

                    //{

                    //    "Email",

                    //    selectQueryColumnEmail

                    //},

                    {

                        "City",

                        selectQueryColumnCity

                    },

                    {

                        "State",

                        selectQueryColumnState

                    },

                    {

                        "Country",

                        selectQueryColumnCountry

                    }

                };

                // Query filters.

                var selectFilters = new Filters()

                {

                    FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.FilterGroup,

                    IsEnabled = true,

                    RootSchemaName = "Account",

                    Items = new Dictionary<string, Filter>

                    {

                        {

                            "FilterByCreatedOn",

                            new Filter

                            {

                                FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,

                                ComparisonType = FilterComparisonType.Greater,

                                IsEnabled = true,

                                LeftExpression = new BaseExpression ()

                                {

                                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                                    ColumnPath = "ModifiedOn",

                                },

                                RightExpression= new BaseExpression ()

                                {

                                    ExpressionType = EntitySchemaQueryExpressionType.Parameter,

                                    Parameter = new Parameter ()

                                    {

                                        DataValueType = DataValueType.DateTime,

                                        Value= "2018-05-11T11:17:04.000"

                                    }

                                },

                                TrimDateTimeParameterToDate = true,

                            }

                        },

                        {

                            "FilterByName",

                            new Filter

                            {

                                FilterType = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,

                                ComparisonType = FilterComparisonType.Contain,

                                IsEnabled = true,

                                LeftExpression = new BaseExpression ()

                                {

                                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,

                                    ColumnPath = "Name",

                                },

                                RightExpression= new BaseExpression ()

                                {

                                    ExpressionType = EntitySchemaQueryExpressionType.Parameter,

                                    Parameter = new Parameter ()

                                    {

                                        DataValueType = DataValueType.Text,

                                        Value= "Test"

                                    }

                                },

                                TrimDateTimeParameterToDate = true,

                            }

                        }

                    }

                };

                selectQuery.Filters = selectFilters;

                // Serialization of an instance of query class to add to JSON string.

                var json = new JavaScriptSerializer().Serialize(selectQuery);

                byte[] jsonArray = Encoding.UTF8.GetBytes(json);

                var selectRequest = HttpWebRequest.Create(selectQueryUri) as HttpWebRequest;

                selectRequest.Method = "POST";

                selectRequest.ContentType = "application/json";

                // Adding earlier received authentication cookies to a data fetch query.

                selectRequest.CookieContainer = AuthCookie;

                selectRequest.ContentLength = jsonArray.Length;

                AddAuthCookie(selectRequest);

                using (var requestStream = selectRequest.GetRequestStream())

                {

                    requestStream.Write(jsonArray, 0, jsonArray.Length);

                }

                using (var response = (HttpWebResponse)selectRequest.GetResponse())

                {

                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))

                    {

                        var result = reader.ReadToEnd();

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);                

            }

        }

    }

Pay attention to this code: private const string baseUri = @"https://[example].bpmonline.com";

it should be the address of your application

yes i know... I have just write down example in this code only... for comment

 

You can put there 'example' instead of 'example'. It will give you exact scenario. Thanks

You passed a wrong date format. Again, if look at browser console when applying a date filter you will see the next format: ""2018-06-05T23:59:59.999"". So you should escape one more quote mark to rich the goal in c#: Value = "\""+DateTime.Now+ "\""

Okay thanks.

Show all comments