Developer forum

Forum » Development » Get the unique ID of a form saved with the Forms for DataLists

Get the unique ID of a form saved with the Forms for DataLists

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

Similar to what I am doing here: http://developer.dynamicweb.com/forum.aspx?ThreadID=42947 I am trying to retrieve the unique ID of a form saved with the Forms for DataLists module so I can append it to the email subject in a FormSaveProvider. Is this possible? There's no ID in any of the base class properties, nor does there seem to be one in the keysAndValues collection.

Any ideas? I would like to create a unique identifier for the subject that is both unique and fairly simple to read out loud over the phone. I can use the number of seconds since last year or a GUID but neither seem to be very friendly.

Thanks.

Imar


Replies

 
Nicolai Høeg Pedersen
Reply

In your FormSaveProvider you could do a SELECT @@IDENTITY or just the newest record from the table... I know that is hacky.

Problem is that it is a provider implementation that adds the record to the table, and it is not in contact with the template.'

BR Nicolai

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Imar,

Forms for Data lists does not use a unique id for FormSaves internally and there's no reliable way to get a unique id based on the FormSave itself. Nicolai's solution falls into this unreliable category.

You can get a couple of ids that could make up a unique key, but it's probably more work that simply generating a unique key manually.

You could use the FormSetting.ID and then query the database and find the identity of the row that was inserted/updated based on the FormSave. Unless you have a unique id in the KeysAndValues collection, this is unreliable. You could send the number of milliseconds since 1970 or something as that unique key and use that to query for the row identity. Basically, using one unique key to get a shorter and simpler unique key.

As you can see, this is a long way to go to get something that is a little bit easier. The simplest solution is probably to add your own serial number to the FormSaves so they all have a continuous number based on the order they were submitted. You could even use the EcomNumbers API with your own key if you don't want to code it from scratch.

I hope this brings you a bit closer to a solution.

- Jeppe

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks guys, that all makes a lot of sense. I decided to go with the number of seconds since today as that's a small enough number for the next few years to read out loud over a phone. In addition, the chance of two people submitting on the same time are relatively small, and it wouldn't be a big deal if once in a while two people would end up with the same ID anyway.

Is there a sample of the Number API somewhere? I tried this:

return Dynamicweb.eCommerce.Common.NumberGenerator.GetNumber("UniqueFormId");

but that always gives me the same number: 0 and doesn't save the result in the database. I tried working with the Number class but couldn't figure out how to do it. XML docs like "Froms the type" on a method called FromType doesn't make it any easier ;-) I assume the very first time I need to create a new Number, set some properties and save it. And then subsequently call the static GetNumber method? I tried something like this:

internal static List<string> ExistingNumbers = new List<string>();
internal string ProvideUniqueNumber(string type, string prefix)
{
  if (!ExistingNumbers.Contains(type))
  {
    var number = Number.getNumbers(true).FirstOrDefault(x => x.ID == type + prefix);
    if (number == null)
    {
      number = new Number
      {
        ID = type + prefix,
        Add = 1,
        Counter = 1,
        Description = string.Format("Gets unique numbers for {0}", type),
        Editable = true,
        Prefix = prefix,
        Type = type
      };
      number.Save(number.ID); // also tried number.Save();
      ExistingNumbers.Add(type);
    }
  }
  return NumberGenerator.GetNumber(type + prefix);
}

However, the number is never saved in EcomNumbers and GetNumber keeps returning 0.

Thanks,

Imar

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Well, there are at least two different ways to create a new number. You can create it manually in the database or you can create it programmatically.

You can create a new row in the EcomNumbers table where you enter the values for each column that you want. The columns also map directly to the properties of the Number class. You'll want to do the following as a minimum.

  • Set an ID that is unlikely to be chosen by Dynamicweb. We use a continuous number series, but the column is an NVARCHAR(50), so choose something like Custom1 or FormID1.
  • Set a Type that you want. This is the key you use to request a new number, so choose something like UNIQUEFORMID.
  • Set the Counter to 0 unless you want a series that begins at a higher number. The value you set is considered the current value, so when you request a new number, the current value is incremented and then returned. This means that an initial value of 0 gives you the value 1 upon the first request if increment is 1.
  • Set Add to the interval you wish to increment the value by for each request -- usually 1.
  • Set Editable to indicate whether you want this number to editable in the Management Center -> Ecommerce -> Advanced configuration -> Auto-numbering. Usually false.

As an optional step, you can specify a description, prefix and postfix to the number as well. The description is pretty self-explanatory. The pre- and postfixes are used to prepend or append some static value to the number. This is just like order ids in Dynamicweb where ORDER1234 is produced by a number that has ORDER as its prefix.

Once you've either created the row, or the object and saved it, you can start requesting new numbers in that series simply by calling NumberGenerator.GetNumber(<Type>).

- Jeppe

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Jeppe,

Thanks for that. But isn't my code example doing exactly what you'e describing? When I call Save, I get nothing in the EcomNumbers table.

Imar

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Sorry, yes. You need to create the row manually directly in the database then you can manipulate it using code. The Save method requires an existing row to update.

- Jeppe

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Ah, I see. That explains it.

Thanks,

Imar

 
António Ramos
Reply

Hi guys, 

I tried Imar code and added the row manually but it always return 0. Have i missed something?

Best regards,

António Ramos

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

If you add the row manually, you don't need much of my code. All you need is this:

return NumberGenerator.GetNumber(type);

Does that work for you?

Imar

 
António Ramos
Reply

Oh.It's working now.. Thank you!

BR,

António 

 

You must be logged in to post in the forum