cancel
Showing results for 
Search instead for 
Did you mean: 

Blueprism Code Stage Issue

salman_shaik
Level 6

Hi Team,

I have written some customized code in code stage it requires the following packages
System
System.Collections.Generic

System.Data

System.Linq

I have added the references but i am getting the error in Initialize page.
can you please help on this.

Page: Initialise
Stage: Stage1
Type: Error
Action: Validate
Description: Compiler error at top section line 586: Type or namespace definition, or end-of-file expected
Repairable: No

salman_shaik_0-1730917058025.png

salman_shaik_1-1730917109186.png

No error in Code stage

Sample code:

success = true;
error_message = string.Empty;
filteredTable = new DataTable();
summaryTable = new DataTable();
var allowedTypes = new HashSet<string>
{
"ABC", "ABC"
};

var filteredRows = inputTable.AsEnumerable()
.Where(row => allowedCreativeTypes.Contains(row.Field<string>("Type")));

filteredTable = inputTable.Clone();

 

If I was of assistance, please vote for it to be the "Best Answer". Thanks & Regards, Salman Shaik
3 REPLIES 3

faheemsd
MVP

@salman_shaik 

Could you please help me with your Inputs and outputs added in the code stage? what are the data types?
From the above c# code your trying to filter the data table but to filter data table Blue Prism already providing the Filter Collection action in Collection Manipulation VBO

faheemsd_0-1730921997436.png

This is Similar to your C# code and here Collection referred as Data Table .

Inputs:

Collection
Filter Expression - You can write your filter expression here

Outputs:

you can save the Output in same collection (which is nothing but same data table).

If you specifically trying with c# code to achieve this kindly help me with all your Inputs and Output parameters in Code stage.

 




faheemsd
MVP

@salman_shaik 

Please try the below approach 

faheemsd_0-1730924741613.png

faheemsd_1-1730924989395.png

Code:

// Initialize success flag and error message
success = true;
error_message = string.Empty;

// Create new DataTables for storing the results
filteredTable = inputTable.Clone();
summaryTable = inputTable.Clone();

// Define the allowed creative types (use a List<string> or string array)
List<string> allowedTypes = new List<string> { "ABC", "DEF" }; // Modify as needed

// Loop through each row in the input table to filter based on "Type" column
foreach (DataRow row in inputTable.Rows)
{
// Check if the "Type" column value is in the allowedTypes collection
if (allowedTypes.Contains(row["Type"].ToString())) // This uses List.Contains() method
{
// If it matches, add the row to the filteredTable
filteredTable.ImportRow(row);
}
}

Please let me know if the above works for your requirement.

Note: You can pass the inputTable as a Startup parameter from the process to object.

@faheemsd 

It's not working below is my complete code along with all details. I feel some issue with dependent packages when i am compiling the code stage no errors were found.
This is not related to only filtering the collection, doing some operations using group by according to my use case which is not available in normal collection manipulation.

Code:

try
        {
success = true;
error_message = string.Empty;
filteredTable = new DataTable();
summaryTable = new DataTable();
         
            var allowedTypes = new HashSet<string>
            {
                "ABC", "ABC"
            };
            var filteredRows = inputTable.AsEnumerable()
                .Where(row => allowedTypes.Contains(row.Field<string>("Type")));
            filteredTable = inputTable.Clone(); 
            foreach (var row in filteredRows)
            {
                filteredTable.ImportRow(row);
            }
            var groupedData = filteredRows
                .GroupBy(row => new
                {
                    Id = row.Field<string>("ID"),
                    Type = row.Field<string>("Type")
                })
                .Select(group => new
                {
                    Id = group.Key.Id,
                    Type = group.Key.Type,
                    FinalTargeting = string.Join(",", group.Select(row => row.Field<string>("Site Type")).Distinct().OrderBy(s => s))
                });
 
            // Create summary DataTable
            summaryTable = new DataTable();
            summaryTable.Columns.Add("ID", typeof(string));
            summaryTable.Columns.Add("Type", typeof(string));
            summaryTable.Columns.Add("Final Targeting", typeof(string));
 
             foreach (var item in groupedData)
            {
                summaryTable.Rows.Add(item.Id, item.Type, item.FinalTargeting);
            }
 
        }
        catch (ArgumentNullException ex)
        {
            success = false;
            error_message = "Error: A required column is missing in the data - " + ex.Message;
        }
        catch (InvalidOperationException ex)
        {
            
success = false;
            error_message = "Error: Data processing issue - " + ex.Message;
        }
        catch (Exception ex)
        {
            success = false;
            error_message = "An unexpected error occurred: " + ex.Message;
        }
    }

Inputs:

salman_shaik_0-1730949705328.png

Outputs:

salman_shaik_1-1730949744562.png

Packages:

salman_shaik_2-1730949813765.png

Let me know if you need anything.

Thanks



 

If I was of assistance, please vote for it to be the "Best Answer". Thanks & Regards, Salman Shaik