You are viewing limited content. For full access, please sign in.

Question

Question

Creating a Hex counter in Workflow

asked on April 1, 2018

So I am pulling a Hex value from SQL and I need to run a counter for it.  Is it possible to do this in workflow?

Ex:

Values pulled from SQL

464A294D

474A294D

 

Next Value in sequence to be created:

484A294D

 

0 0

Replies

replied on April 2, 2018 Show version history

Hi Chase,

The small workflow below will populate a multi-value token with the next 10 numbers in your sequence, based on a starting value and increment value.    I am not sure how you will be using these values but this might give you a place to start.   The values returned are strings, not actual hex numbers .....

Workflow: 

Token values:
 hex_start is the hex number that your sequence will start at
 inc_value is the value that your sequence will increment ( in hexadecimal )
 sequence is a multi-value token with the sequence


The script (C#) converts the hex_start and inc_Value to hexadecimal,  runs a loop that generates the next 10 values saving them into a list and returns that.

namespace WorkflowActivity.Scripting.Script
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class Script1 : ScriptClass90
    {
        /// <summary>
        /// This method is run when the activity is performed.
        /// </summary>
        protected override void Execute()
        {
            int hex = Convert.ToInt32((string)this.GetTokenValue("hex_start"),16);
            int inc = Convert.ToInt32((string)this.GetTokenValue("inc_Value"),16);

            List<string> result = new List<string>();
            for(int x= 0; x<10;x++) {
                hex += inc;
                result.Add(hex.ToString("X"));
            }
            SetTokenValue("sequence",result);
        }
    }
}

Results:


Hope that helps.
~ Andrew
 

0 0
replied on April 2, 2018

These are used as a unique identifier in a database table:

You can see that it's counting up as your counter has been built.  I initially wanted to have the database write the next row as the identifier is set to NOT NULL, but the database warned me during an insert that a value needs to exist there because the identifier is NOT NULL.  When I wrote the next value in the sequence manually it took the insert just fine.

If you know a better way to satisfy this through the DB instead of workflow I would be happy to learn that as well.  Otherwise I will test what you gave me.

Thank you!

0 0
replied on April 2, 2018

Your table looks like it's using guids for that column, and there are two ways you can handle that.  You can have the database server generate them, by assigning a default value to the column of NEWID() (if this is MSSQL, other systems will have different methods).  Alternately, you can generate them client-side with Guid.NewGuid().

0 0
replied on April 2, 2018

Since the GUID column does not contain an Identity increments and Is Computed set to FALSE, the NEWID() will generate a new GUID,  but completely out of order as the system seems to count from.

See below:

 

0 0
replied on April 2, 2018

Guids are designed for uniqueness but you only get that guarantee if you generate them correctly.  Generating them in an auto-incrementing fashion negates the only benefit that they give you, so for whatever problem you are trying to solve there is probably a better approach.  If you want to easily order by insertion time, you could use an auto-incrementing int column or an explicit datetime column, and you could still have the guid column for external uniqueness or references.

0 0
replied on April 2, 2018

This isn't by my design but by the SAGE application I am integrating to.  Otherwise I would just write with the NEWID()

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.