This topic is locked

Send Email on Condition

6/26/2007 5:26:10 AM
ASPRunnerPro General questions
D
dlangham author

I need some help to send a mail out when a condition is set when editing a record, but I only want to send it once.

I am using vesion 5.0 build 275 and SQL Server 2005.
I have the following fields:

Managers Email

Warning 1 (Dropdown Box)

Warning 2 (Dropdown Box)

Warning 3 (Dropdown Box)
When the record is created the Warning fields default to 'No'

When the record is edited and Warning 1 field is set to 'Yes' I would like to send a mail to the address that is in the Managers Email field and vice versa for the other 2 warning fields as and when they are edited, I would like to only send 1 mail out per edit/per field.

Basicly just send a mail out only when one of the Warning fields is set to 'Yes' and not to repeat if just editing other fields in the record i.e Managers Email field.
Any help would be appreciated on this please.

Sergey Kornilov admin 6/26/2007

Not clear if you like to send the email one time per edit or per the whole record existence.
If later create an additional EmailSent field with default value of 0.

After email is sent update this field value to 1 and check this field value prior to sending an email.

D
dlangham author 6/26/2007

Hi Sergey,
Thats the way I was thinking, I have already created a 'MailSent' field for each warning field, the idea is that a mail will be sent if a warning field changes from No to Yes, but only if the 'MailSent' field is zero, then update it to 1 after mail is sent so it wont send again.

Its the code I am stuck with, I am assuming that I will need 3 code sections for the 3 warning fields to do this...am I right?
If you could post me something here to get me started then that would be great.

Sergey Kornilov admin 6/26/2007

Here is the sample code:

set rstmp = Server.CreateObject("ADODB.Recordset")

rstmp.Open "select * from " & strTableName & " where " & where, dbConnection
MailSent1 = CLng(rstmp("MailSent1"))

MailSent2 = CLng(rstmp("MailSent2"))

MailSent3 = CLng(rstmp("MailSent3"))
rstmp.close : set rstmp = nothing
if dict("Warning 1")="Yes" and MailSent1=0 then



sendmail ...

dbConnection.Execute "update " & strTableName & " set MailSent1=1 where " & where
end if
if dict("Warning 2")="Yes" and MailSent2=0 then



sendmail ...

dbConnection.Execute "update " & strTableName & " set MailSent2=1 where " & where
end if
if dict("Warning 3")="Yes" and MailSent3=0 then



sendmail ...

dbConnection.Execute "update " & strTableName & " set MailSent3=1 where " & where
end if
BeforeEdit = True
D
dlangham author 6/27/2007

Hi Sergey,
I have implemented the code below in the before edit section but after I do this it will not even let me log-in, can you check it over for me and see what is wrong?

set rstmp = Server.CreateObject("ADODB.Recordset")

rstmp.Open "select * from " & strTableName & " where " & where, dbConnection
Mail Sent 1 = CLng(rstmp("Mail Sent 1"))

Mail Sent 2 = CLng(rstmp("Mail Sent 2"))

Mail Sent 3 = CLng(rstmp("Mail Sent 3"))
rstmp.close : set rstmp = nothing
if dict("Warning 1")="Yes" and Mail Sent 1=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 1"

subject="Warning 1"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set Mail Sent 1=1 where " & where
end if
if dict("Warning 2")="Yes" and Mail Sent 2=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 2"

subject="Warning 2"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set Mail Sent 2=1 where " & where
end if
if dict("Warning 3")="Yes" and Mail Sent 3=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 3"

subject="Warning 3"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set Mail Sent 3=1 where " & where
end if
BeforeEdit = True
Sergey Kornilov admin 6/27/2007

Spaces are not allowed in variable names.
See my code one more time.

D
dlangham author 6/27/2007

Thanks for that, below is the code I have at the minute and has fixed the error I was getting, but I am getting this now and cannot work it out what is wrong after an edit.
Error number -2147217865

Error description [Microsoft][ODBC SQL Server Driver]

Invalid object name 'dbo.Parking'.

**SQL query** select * from [dbo].[Parking Details] where [dbo].[Parking Details].[Reference Number]='UTL-2007-62616944447'
[code]set rstmp = Server.CreateObject("ADODB.Recordset")

rstmp.Open "select * from " & strTableName & " where " & where, dbConnection
MailSent1 = CLng(rstmp("Mail Sent 1"))

MailSent2 = CLng(rstmp("Mail Sent 2"))

MailSent3 = CLng(rstmp("Mail Sent 3"))
rstmp.close : set rstmp = nothing
if dict("Yellow Card 1")="Yes" and MailSent1=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 1"

subject="Warning 1"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set MailSent1=1 where " & where
end if
if dict("Yellow Card 2")="Yes" and MailSent2=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 2"

subject="Warning 2"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set MailSent2=1 where " & where
end if
if dict("Red Card Warning")="Yes" and MailSent3=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 3"

subject="Warning 3"

sendmail email, subject, message
dbConnection.Execute "update " & strTableName & " set MailSent3=1 where " & where
end if
BeforeEdit = True


Any ideas would be welcome Sergey.

Sergey Kornilov admin 6/27/2007

Dale,
the same issue with spaces.
Instead of strTableName use the real table name and make sure to wrap it by square brackets.

Before you use SQL query in this event test in in SQL query Analyzer to make sure it works.

D
dlangham author 6/28/2007

Sergey,
Sorry to be a pain, but I am getting this error now when I try to save after an edit!!!
**[Microsoft][ODBC SQL Server Driver]

Incorrect syntax near the keyword 'where'.**
My code now is as follows:

[code]set rstmp = Server.CreateObject("ADODB.Recordset")

rstmp.Open "select * from " & [Parking Details] & " where " & where, dbConnection
MailSent1 = CLng(rstmp("MailSent1"))

MailSent2 = CLng(rstmp("MailSent2"))

MailSent3 = CLng(rstmp("MailSent3"))
rstmp.close : set rstmp = nothing
if dict("Yellow Card 1")="Yes" and MailSent1=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 1"

subject="Warning 1"

sendmail email, subject, message
dbConnection.Execute "update " & [Parking Details] & " set MailSent1=1 where " & where
end if
if dict("Yellow Card 2")="Yes" and MailSent2=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 2"

subject="Warning 2"

sendmail email, subject, message
dbConnection.Execute "update " & [Parking Details] & " set MailSent2=1 where " & where
end if
if dict("Red Card Warning")="Yes" and MailSent3=0 then
email="me@me.com"

message="Parking" & vbcrlf & "Warning 3"

subject="Warning 3"

sendmail email, subject, message
dbConnection.Execute "update " & [Parking Details] & " set MailSent3=1 where " & where
end if
BeforeEdit = True


I really cannot see where I am going wrong, maybe there is a typo somewhere, but I cannot see it if there is!!!
Best Regards

Dale

Sergey Kornilov admin 6/28/2007

Which query gives you this error?
What event did you put this to?

D
dlangham author 6/29/2007

Sergey,
I am assuming it is something to do with this line:

dbConnection.Execute "update " & [Parking Details] & " set MailSent1=1 where " & where



There is no special WHERE statement in it really.
I am putting this in the BeforeEdit Event section.
Rgds

Dale

Sergey Kornilov admin 6/29/2007

Dale,,
instead of executing SQL statements print them on the page to see which one gives you the error.

D
dlangham author 7/2/2007

Sergey,
Everything runs OK, its just this event that errors out, I am lost on this one!!!
Rgds

Dale

Sergey Kornilov admin 7/2/2007

Dale,
before executing SQL statements in your event print them on the page to see which one gives you the error.
Example:

dbConnection.Execute "update " & [Parking Details]& " set MailSent1=1 where " & where


use

Response.Write "update " & [Parking Details] & " set MailSent1=1 where " & where

dbConnection.Execute "update " & [Parking Details] & " set MailSent1=1 where " & where