<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic RE: C# in BP, struggling to get a query working in Product Forum</title>
    <link>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65082#M17743</link>
    <description>&lt;P&gt;Thank you so much for your help with this! Was my first time using code in BP and I just couldn't see where it was going wrong, your solution made perfect sense and worked exactly as needed.&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Neil Odwyer&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
    <pubDate>Wed, 31 Jan 2024 08:01:00 GMT</pubDate>
    <dc:creator>NeilO</dc:creator>
    <dc:date>2024-01-31T08:01:00Z</dc:date>
    <item>
      <title>C# in BP, struggling to get a query working</title>
      <link>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65079#M17740</link>
      <description>&lt;P&gt;I am struggling to get the following query running as a Code query. The full query is -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;PRE class="language-csharp"&gt;&lt;CODE&gt;using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Program
{
    static async Task Main()
    {
        DateTime currentDate = DateTime.Now;
        DateTime firstDayOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

        // Check if the current day is a working day (Monday to Friday),
        // is the first day of the month, and is not a bank holiday
        if (currentDate.DayOfWeek &amp;gt;= DayOfWeek.Monday &amp;amp;&amp;amp;
            currentDate.DayOfWeek &amp;lt;= DayOfWeek.Friday &amp;amp;&amp;amp;
            currentDate.Date == firstDayOfMonth &amp;amp;&amp;amp;
            !await IsBankHoliday(currentDate))
        {
            // It is the first working day of the month
            Console.WriteLine("It is the first working day of the month");
        }
        else
        {
            // It is not the first working day of the month
            Console.WriteLine("It is not the first working day of the month");
        }
    }

    static async Task&amp;lt;bool&amp;gt; IsBankHoliday(DateTime checkDate)
    {
        using (HttpClient client = new HttpClient())
        {
            try
            {
                // Make an HTTP request to get bank holidays JSON
                string json = await client.GetStringAsync("https://www.gov.uk/bank-holidays.json");

                // Parse JSON response
                JObject bankHolidayData = JObject.Parse(json);

                // Extract bank holidays for England and Wales
                JArray bankHolidays = (JArray)bankHolidayData["england-and-wales"]["events"];

                // Check if the checkDate is in the list of bank holidays
                foreach (JToken holiday in bankHolidays)
                {
                    if (DateTime.Parse(holiday["date"].ToString()).Date == checkDate.Date)
                    {
                        return true;
                    }
                }

                return false;
            }
            catch (HttpRequestException)
            {
                // Handle request exception, e.g., if the URL is unreachable
                Console.WriteLine("Error: Unable to retrieve bank holidays data.");
                return false;
            }
            catch (Exception ex)
            {
                // Handle other exceptions
                Console.WriteLine($"Error: {ex.Message}");
                return false;
            }
        }
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;It doesn't seem to matter if I add the code to Global or a Code Object, I get the error - Compiler Error at top section line 98: Type or namespace definition, or end-of-file expected.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have added in the External References for Newtonsoft.Json.dll, System.Net.Http.dll &amp;amp; System.Threading.Tasks.Extensions.dll.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have set namespaces up for System.Threading.Tasks, System.Net.Http.HttpClient &amp;amp; Newtonsoft.Json.Linq.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure I'm missing something simple but I can't for the life of me see what, any help would be appreciated.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Many Thanks&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Neil Odwyer&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Tue, 30 Jan 2024 14:47:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65079#M17740</guid>
      <dc:creator>NeilO</dc:creator>
      <dc:date>2024-01-30T14:47:00Z</dc:date>
    </item>
    <item>
      <title>RE: C# in BP, struggling to get a query working</title>
      <link>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65080#M17741</link>
      <description>&lt;P&gt;Hello Neil,&lt;/P&gt;
&lt;P&gt;Try doing this way. &amp;nbsp;No need for 'using' declarations in your code. Instead, consider configuring external references and namespace imports in the code options.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Let me know if that works for you.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="30885.png"&gt;&lt;img src="https://community.blueprism.com/t5/image/serverpage/image-id/31009iA2526DF470CB5FFC/image-size/large?v=v2&amp;amp;px=999" role="button" title="30885.png" alt="30885.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="30886.png"&gt;&lt;img src="https://community.blueprism.com/t5/image/serverpage/image-id/31016i9090F42B88A91603/image-size/large?v=v2&amp;amp;px=999" role="button" title="30886.png" alt="30886.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="30887.png"&gt;&lt;img src="https://community.blueprism.com/t5/image/serverpage/image-id/31011iED664FF1C235F6FA/image-size/large?v=v2&amp;amp;px=999" role="button" title="30887.png" alt="30887.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE class="language-csharp"&gt;&lt;CODE&gt;public class GlobalCode
{
    // Class Variables
    private static string output;
    private static string errorMessage;

    public static string Main()
    {
        DateTime currentDate = DateTime.Now;
        DateTime firstDayOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

        // Check if the current day is a working day (Monday to Friday),
        // is the first day of the month, and is not a bank holiday
        if (currentDate.DayOfWeek &amp;gt;= DayOfWeek.Monday &amp;amp;&amp;amp;
            currentDate.DayOfWeek &amp;lt;= DayOfWeek.Friday &amp;amp;&amp;amp;
            currentDate.Date == firstDayOfMonth &amp;amp;&amp;amp;
            !IsBankHolidayAsync(currentDate).Result) // Use .Result to synchronously wait for the task
        {
            // It is the first working day of the month
            output = "It is the first working day of the month";
        }
        else
        {
            // It is not the first working day of the month
            output = "It is not the first working day of the month";
        }

        return output; // Return the result
    }

    public static async Task&amp;lt;bool&amp;gt; IsBankHolidayAsync(DateTime checkDate)
    {
        return await Task.Run(async () =&amp;gt;
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    // Make an HTTP request to get bank holidays JSON
                    string json = await client.GetStringAsync("https://www.gov.uk/bank-holidays.json");

                    // Parse JSON response
                    JObject bankHolidayData = JObject.Parse(json);

                    // Extract bank holidays for England and Wales
                    JArray bankHolidays = (JArray)bankHolidayData["england-and-wales"]["events"];

                    // Check if the checkDate is in the list of bank holidays
                    foreach (JToken holiday in bankHolidays)
                    {
                        if (DateTime.Parse(holiday["date"].ToString()).Date == checkDate.Date)
                        {
                            return true;
                        }
                    }

                    return false;
                }
                catch (HttpRequestException)
                {
                    // Handle request exception, e.g., if the URL is unreachable
                    errorMessage = "Error: Unable to retrieve bank holidays data.";
                    return false;
                }
                catch (Exception ex)
                {
                    // Handle other exceptions
                    errorMessage = $"Error: {ex.Message}";
                    return false;
                }
            }
        });
    }
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Jerome Santiago - &lt;A href="https://www.linkedin.com/in/jerome-santiago-b9b272141/" target="test_blank"&gt;https://www.linkedin.com/in/jerome-santiago-b9b272141/&lt;/A&gt;&lt;BR /&gt;Automation Developer&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Tue, 30 Jan 2024 23:33:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65080#M17741</guid>
      <dc:creator>jsantiago08</dc:creator>
      <dc:date>2024-01-30T23:33:00Z</dc:date>
    </item>
    <item>
      <title>RE: C# in BP, struggling to get a query working</title>
      <link>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65081#M17742</link>
      <description>&lt;P&gt;In simple terms, code stages/global code in BP are not console apps, so Main method has no place in BP...&lt;/P&gt;
&lt;P&gt;In summary:&lt;/P&gt;
&lt;P&gt;1) The global code of a BP Object is the body of a class&lt;/P&gt;
&lt;P&gt;2) A code stage gets transformed to the body of a method of that class (spaces in the code stage name are auto-replaced by "_")&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Use the global code or code stages as methods to return values to a calling process page&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Only Object pages can be seen by a process, but Object pages may call code from Global code as per preferred design.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I'd set up an action (object page with code stage) that returns a Flag (boolean)&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Anton Hosang&lt;BR /&gt;Director&lt;BR /&gt;Omega RPA Ltd&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Tue, 30 Jan 2024 23:41:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65081#M17742</guid>
      <dc:creator>Anton__Hosang</dc:creator>
      <dc:date>2024-01-30T23:41:00Z</dc:date>
    </item>
    <item>
      <title>RE: C# in BP, struggling to get a query working</title>
      <link>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65082#M17743</link>
      <description>&lt;P&gt;Thank you so much for your help with this! Was my first time using code in BP and I just couldn't see where it was going wrong, your solution made perfect sense and worked exactly as needed.&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Neil Odwyer&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Wed, 31 Jan 2024 08:01:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Product-Forum/C-in-BP-struggling-to-get-a-query-working/m-p/65082#M17743</guid>
      <dc:creator>NeilO</dc:creator>
      <dc:date>2024-01-31T08:01:00Z</dc:date>
    </item>
  </channel>
</rss>

