Developer forum

Forum » Templates » DW forms: ID of entry in email receipt

DW forms: ID of entry in email receipt

Martin Orvad
Reply
Hi

How do I get the ID of an entry in the forms module into the email receipt? Every entry is of course assigned a unique id, but as far as I can see it is not available in the template.

What I need is a tag like this: <!--@Form.Fields.myForm_ID.Value-->

It's the forms DW7 version.

- Martin


Replies

 
Marco Johannesen
Reply
Hmmm... If you have it added (The ID)  added in Forms(DW7) and you chose it as a hidden, dosent it send then?
It sends it to the confirmation page, so it should also send it in the email.

If you have a "ENTRY" database, and then have Entry_ID as primary key
Then in forms make sure Entry_ID is there, and is selected as hidden.
Then it should autogenerete the key in the database, and post it back on confirm page/email.

I might be misunderstanding something :)



 
Martin Orvad
Reply
The field is added automatically to all forms. It is called ID and it is probably the primary key for the table. It is populated with a sequential number (autonum): First entry gets ID = 1, second = 2, third = 3, etc.

Problem is this field is not avaliable in the email reciept template. I need this field in the template for a support form. Each email sent needs a unique id. Using the unique id from the database is the obvious choice.

AFAIK it is not possible to add autonumbering capabilities to a manually created field and I don't think it is possible to create a hidden field in a DW-form which overwrites the ID field. IMO that would be a very bad way to do it.

Any ideas?

-Martin

 
Jacob Storgaard Jensen
Reply

Okay, I see that this has never been answered - but now I need to use the key value on my confirmation template (not the e-mail, but would be great to have in the e-mail too) Is there any way to get this? Razor?

Urgent by the way!!

 
Jacob Storgaard Jensen
Reply

Anyone?

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

You need to query the information from the database in Razor:

If you have a form with the systemname "TestForm", you would do this:

System.Data.IDataReader dr = Dynamicweb.Database.CreateDataReader("SELECT top 1 * FROM TestForm order by TestFormID desc");
while (dr.Read()) {
 var fieldname1 = dr.Item["TestForm_FieldName1"];
 var fieldname2 = dr.Item["TestForm_FieldName2"];
}
Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply
 
Jacob Storgaard Jensen
Reply

Whops marked as answer too fast... Will this just read the latest created submission? My problem is that this is a submission form where 100 seats are available, last year 150 submission were made within a few minutes, which in the case taht your solution picks the latest created submission, could potentially be another submission...

 
Nicolai Høeg Pedersen
Reply

Yes, But I have no other brilliant ideas. But it would be VERY unlikely to happen. even if 10 persons submits within the same second.

But you can change the SQL to something like this:

SELECT top 1 *
  FROM TestForm
  where TestForm_Name = "Nicolai"
  order by testformid desc

And you have my name from one of the template fields in the confirmation template.

makes sense?

 
Jacob Storgaard Jensen
Reply

Makes sence!

Giving it a try :-)

Thanks!

P.S. Would it be stupid to suggest that the ID is added as a template tag in the confirmation page/template + e-mails?

 
Nicolai Høeg Pedersen
Reply

I have added the ID of the data with the tag Form.RowID in the confirmation template, TFS#16381 for 8.6

 
Jacob Storgaard Jensen
Reply

Okay, so now my problem is that I can't figure out how to get the name of the submitter as a variable in the SQL... I tried using some code that Mikkel Ricky has written in your Git Razor reposetory instead because your example throw som other errors that I didn't know how to fix. The below code gives me this error: System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@theNameOfTheSubmitter".

(P.S. Would like to check for the phone number too, so that I'm sure it's the correct row that's returned)

This is absolutely my lack of knowledge in SQL and Razor, but I just need help :-)

@{
    var theNameOfTheSubmitter = GetString("Form.Fields.Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn.Value");
    var sql = "SELECT top 1 * FROM Cykel_For_Sagen_Tilmeldte WHERE Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn = @theNameOfTheSubmitter order by Cykel_For_Sagen_TilmeldteID desc";

    using (var reader = Dynamicweb.Database.CreateDataReader(sql)) {
        while (reader.Read()) {
            <pre>@reader["Cykel_For_Sagen_TilmeldteID"]</pre>
        }
    }

}
 
Mikkel Ricky
Reply
This post has been marked as an answer

You have to add/define the parameter in your database query:

    using (var cmd = Dynamicweb.Database.CreateConnection().CreateCommand())
    {
        cmd.CommandText = sql;
        Dynamicweb.Database.AddStringParam(cmd, "theNameOfTheSubmitter", theNameOfTheSubmitter);

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                ...
            }
        }
    }

Best regards,
Mikkel

Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

THANKS Nicolai and Mikkel!

For those of you who like to have the full template and context, heres is my final code (including the check for mobile number to ensure the correct id is ):

@{
    var theNameOfTheSubmitter = GetString("Form.Fields.Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn.Value");
    var theMobileOfTheSubmitter = GetString("Form.Fields.Cykel_For_Sagen_Tilmeldte_Mobil.Value");
    var sql = "SELECT top 1 * FROM Cykel_For_Sagen_Tilmeldte WHERE Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn = @theNameOfTheSubmitter AND Cykel_For_Sagen_Tilmeldte_Mobil = @theMobileOfTheSubmitter order by Cykel_For_Sagen_TilmeldteID desc";
    
    using (var cmd = Dynamicweb.Database.CreateConnection().CreateCommand())
    {
        cmd.CommandText = sql;
        Dynamicweb.Database.AddStringParam(cmd, "theNameOfTheSubmitter", theNameOfTheSubmitter);
        Dynamicweb.Database.AddStringParam(cmd, "theMobileOfTheSubmitter", theMobileOfTheSubmitter);
    
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                var currentRowID = @reader["Cykel_For_Sagen_TilmeldteID"];
                
                <h2>@currentRowID</h2>
This is where i'm going to check if the RowID is larger than 100 and render confirmation messages accordingly based on that check.

            }
        }
    }
}

 

 
Jacob Storgaard Jensen
Reply

Okay, now we're at it... If the RowID is larger than 100, can I then update the column Cykel_For_Sagen_Tilmeldte_Venteliste ?

 

 
Mikkel Ricky
Reply
This post has been marked as an answer

You can do anything in Razor:

@{
    var theNameOfTheSubmitter = GetString("Form.Fields.Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn.Value");
    var theMobileOfTheSubmitter = GetString("Form.Fields.Cykel_For_Sagen_Tilmeldte_Mobil.Value");

    var sql = "SELECT top 1 * FROM Cykel_For_Sagen_Tilmeldte WHERE Cykel_For_Sagen_Tilmeldte_Fornavn_og_efternavn = @theNameOfTheSubmitter AND Cykel_For_Sagen_Tilmeldte_Mobil = @theMobileOfTheSubmitter order by Cykel_For_Sagen_TilmeldteID desc";

    var currentRowID = 0;

    using (var cmd = Dynamicweb.Database.CreateConnection().CreateCommand())
    {
        cmd.CommandText = sql;
        Dynamicweb.Database.AddStringParam(cmd, "theNameOfTheSubmitter", theNameOfTheSubmitter);
        Dynamicweb.Database.AddStringParam(cmd, "theMobileOfTheSubmitter", theMobileOfTheSubmitter);

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                currentRowID = (int)reader["Cykel_For_Sagen_TilmeldteID"];
            }
        }
    }

    if (currentRowID > 100)
    {
        sql = "update Cykel_For_Sagen_Tilmeldte set Cykel_For_Sagen_Tilmeldte_Venteliste = @value where Cykel_For_Sagen_TilmeldteID = @id";
        using (var cmd = Dynamicweb.Database.CreateConnection().CreateCommand())
        {
            cmd.CommandText = sql;
            Dynamicweb.Database.AddDateTimeParam(cmd, "value", DateTime.Now);
            Dynamicweb.Database.AddInt32Param(cmd, "id", currentRowID);

            if (cmd.ExecuteNonQuery() == 1)
            {
                // Row updated
            }
        }
    }
}
Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

I LOVE RAZOR - Wish I was better at it!

Thanks a million!

 

BTW changed

Dynamicweb.Database.AddDateTimeParam(cmd, "value", DateTime.Now);

to

Dynamicweb.Database.AddStringParam(cmd, "value", "True");

 

 

You must be logged in to post in the forum