Convert my Calculated Field from BAQ to C#

Can someone help convert my beautiful calculated field into a c# expression? I’m furiously googling how to do this at the moment and trying to find a way.

My idea is to find where the 2nd “-” exists then do a substring to that # of characters and dump everything aftwards.

Example Data:
AB4000-01-433593-1-1

Trying to convert it to
AB4000-01

CASE
WHEN JobHead.PartNum LIKE ‘%-%-%’ THEN
SUBSTRING(JobHead.PartNum, 0, PATINDEX(‘%-%’, JobHead.PartNum)) + ‘-’ + SUBSTRING(
JobHead.PartNum, /First argument/
CHARINDEX(‘-’, JobHead.PartNum) + LEN(‘-’), /Second argument/
CHARINDEX(‘-’, JobHead.PartNum, CHARINDEX(‘-’, JobHead.PartNum) + LEN(‘-’)) -
LEN(‘-’) - CHARINDEX(‘-’, JobHead.PartNum) /Third argument/
)
ELSE ‘NA’
END

Maybe something like this.

string str = tModelNum;
int index1 = str.IndexOf(’-’);
tModelNum.Substring(0, (str.Length - index1));

You could try using RegEx for a bit more flexibility

1 Like

If your string is always parsing on the dash, you can use the string split command which creates an array of all the pieces. Then you can reassemble the pieces as desired. This solution really only takes two simple lines of code (not counting the original partnumber assignment).

string partnumber = "AB4000-01-433593-1-1";   //this is for testing
string[] partpieces = partnumber.split('-');    //creates an array splitting at the dash
//next line does the magic... if the number of pieces is <3 then use it... otherwise, reassemble just the first two portions of the part number.
string newpartnumber = (partpieces.Count()<3) ? partnumber : partpieces[0]+"-"+partpieces[1]; 







Another way that might be more efficient would only create the array IF there are enough dashes. This uses the “CountStringOccurance” feature that will count the number of dashes to determine if we need to split out the array… but then I also didn’t create a separate array first… doing everything in one line.

string partnumber = "AB4000-01-433593-1-1";   //this is for testing
string newpartnumber = (TextTool.CountStringOccurrences(partnumber , "-")>1) ?
  partnumber : newpartnumber = partnumber.split('-')[0]+"-"+partnumber.split('-')[1]; 

OK… that was fun… back to work. :wink:

1 Like

I experimented with the other code but regardless I still got stuck where i could not produce it on my sendmail widget.

I’m having trouble creating a variable to use in my send mail widget. It does not seem to listen and gives me a blank output. This simple code gives me my result but something is wrong with my “Execute Custom Code” widget in Data Directive.

image

Sendmail widget
image

Do i have to write this to a variable inside a dataset or temp table?

1 Like

Try it using the CallContext fields… see if that will work for you… That is what I am using in my BPM’s and works well. (I never thought of using scalar variables…)

In code you set it as: callContextBpmData.Character01 = your info

Pierre

1 Like

You Nailed it!!! Thanks a bunch! I will probably use CallContext fields more now =)