cancel
Showing results for 
Search instead for 
Did you mean: 

Make c# codeblock more dynamic

JohanSörman
Level 5
Hello,

I have a case where I needed to calculate 6 month date from today so I use a c# snippet for it


But was thinking making it more dynamic where a developer can send in a number, int instead of having it hardcoded. But I can't get it to work. Maybe I'm doing something wrong?

Orginal code:
Date = DateTime.Now.AddMonths(-6);

My attempt:
Date = DateTime.Now.AddMonths(-(Number));

Input: Number (as Number type)
Output: Date (as DateTime)

So my guess is that I have to declare my input to int but can't get that to work. By default It seems like BP wants to declare data items in number type as decimal

Error:
Description: Compiler error at line 1: Argument 1: cannot convert from 'decimal' to 'int'


EDIT:

I also tried this:

Number As integer;
Date = DateTime.Now.AddMonths(-(Number));

But then it throws this compiler error:
Compiler error at line 1: ; expected




------------------------------
Johan Sörman
DevOps Engineer, Senior RPA Developer
Telia Company
Sweden
------------------------------
1 BEST ANSWER

Helpful Answers

TobiasArnold
Level 6
Hi Johan,

there is no integer data type in Blue Prism.
Date.AddMonths takes an integer argument, so you have to use a type conversion from decimal to int (e.g a type cast).
This should do the trick:

Date = DateTime.Now.AddMonths(-(int)Number);

If you get an compiler error message make ensure that C# is the chosen language. (VB.NET is default)

b.t.w to add month to a date there is no need to use a code stage. Simply use the DateAdd built in function in a calculation stage:
DateAdd(5;-6;[Date])

------------------------------
Tobias Arnold
RPA Developer
ITERGO GmbH
Europe/Duesseldorf
------------------------------

View answer in original post

4 REPLIES 4

TobiasArnold
Level 6
Hi Johan,

there is no integer data type in Blue Prism.
Date.AddMonths takes an integer argument, so you have to use a type conversion from decimal to int (e.g a type cast).
This should do the trick:

Date = DateTime.Now.AddMonths(-(int)Number);

If you get an compiler error message make ensure that C# is the chosen language. (VB.NET is default)

b.t.w to add month to a date there is no need to use a code stage. Simply use the DateAdd built in function in a calculation stage:
DateAdd(5;-6;[Date])

------------------------------
Tobias Arnold
RPA Developer
ITERGO GmbH
Europe/Duesseldorf
------------------------------

Thank you, @Tobias Arnold

This solution works. I'm aware that BP has it own way via calculation stage but generally prefer using code snippets or scripting via powershell :)​

------------------------------
Johan Sörman
DevOps Engineer, Senior RPA Developer
Telia Company
Sweden
------------------------------

Just a thought...

With several years of working in BP RPA, my experience in our shop is that RPA developer vacancies are filled by business analysists but rarely programmers, with myself being the only exception to this 'rule'. As such I avoid creating too much code stages containing my code in the various languages allowed by BP. Instead, I try to stick to the building blocks handed to me by the good folks at BluePrism and try and build my stuff with them.

Sometimes however, you come across a challenge that would either be quite unsolvable without a code stage, or where sticking to the BP building blocks would just be overkill. In such cases I find code stages a perfectly viable alternative.

As a programmer, I have this strange rule that whatever I make, should be maintainable by whoever gets to work on it next. So, I stick to the rules, write readable and code in a structured manner, and in general do my best for the guy/gal next working on it.
In this view, making a new code stage to provide for something trivial that BP has already provided for makes not so much sense to me, neither why time should be spent on it nor what it could contribute to.

Again, just a thought...

...I am aware that we already had this discussion earlier, search for 'to code or not to code'.

------------------------------
Happy coding!
Paul
Sweden
------------------------------
Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)

Valid points, Paul. 
That's partially why I wanted to make it more re-usable for others who may not be programmers on the team or future RPA developers and not something only a handful of us knowing some c# or pshell scripting.

Fortunately, a big number of our automation team is programmers on some level so it usually works out pretty good, but I understand if you come from a BA point of view or don't know code it would be confusing.

------------------------------
Johan Sörman
DevOps Engineer, Senior RPA Developer
Telia Company
Sweden
------------------------------