Click here to Skip to main content
15,898,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear,

i created a trigger on CustomerCall when call inserted & Assign to particular user i want to send email.

Below is the working trigger only how to get the Assign user email address from User Table
User-Table
----------
us_No
Us_Name
Us_Password
Us_Email

I want when the customer call assign to particular user take that email address and put it in below trigger.
SQL
alter TRIGGER CallInserted
ON [dbo].[CustomerCall]
FOR INSERT
AS
BEGIN
    SET NOCOUNT ON;

    IF EXISTS (SELECT * FROM inserted)
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail
	// here how to get receipent email from User table
          @recipients = 'recipients', 
          @profile_name = 'SendEmailCM',
          @subject = 'Notification', 
          @body_format = 'html',
          @body = 'body'
    END
END
GO
Posted
Updated 15-Aug-15 23:26pm
v3

1 solution

Taken that the CustomerCall table has a reference column us_No pointing to User table then you can fetch the email address by joining the inserted and the user.

In other words, something like:
SQL
alter TRIGGER CallInserted
ON [dbo].[CustomerCall]
FOR INSERT
AS
BEGIN
   DECLARE @us_email varchar(100);
   SET NOCOUNT ON;

   IF EXISTS (SELECT * FROM inserted)
   BEGIN
      SELECT @us_email = us_email
      FROM inserted i,
           user u
      WHERE u.us_no = i.us_no;

      EXEC msdb.dbo.sp_send_dbmail
           @recipients = @us_email,
           @profile_name = 'SendEmailCM',
           @subject = 'Notification',
           @body_format = 'html',
           @body = 'body'
   END
END
GO

However, notice that this fetches only one email address. If multiple rows are inserted in a single batch this trigger would fail because inserted would contain several rows.

Because of this I would suggest using a cursor to loop through the inserted records if separate emails should be sent based on each insertion.

For more information about cursors, see DECLARE CURSOR (Transact-SQL)[^]
 
Share this answer
 
Comments
basitsar 17-Aug-15 1:21am    
Thanks A lot dear, other thing what i want in Customercall if Assignto column is null then not execute email code how to do this. and if another column status='Closed' then send email to another persone from User table. Email Column is lineMangerEmail

Below is the example.

If (AssignTo=null) then

exit
end if

if (Status='Closed') then
send email to LineManager column from User table
and execute email
end if



thanks
Basit.
Wendelius 17-Aug-15 1:50am    
If these are columns in the CustomerCall table, just fetch the new values from the inserted and add the necessary logic to the trigger. The query could be something like

SELECT @us_email = u.us_email,
@status = i.status,
@assignto = i.assignto
FROM inserted i,
user u
WHERE u.us_no = i.us_no;

Just define the variables needed as their corresponding data types in the table.

Also note that the same comment still applies, if several rows are inserted in the same batch, this will not work correctly.
basitsar 17-Aug-15 3:10am    
Dear Thanks A lot. I have edited trigger.

Below is the Trigger


ALTER TRIGGER [dbo].[CallInserted]
ON [CM].[dbo].[CustomersCalls]
FOR update
AS
BEGIN

DECLARE @us_email varchar(500);
DECLARE @CallNo varchar(500);
DECLARE @CallNo2 varchar(500);
DECLARE @CallComplaint varchar(1000);

DECLARE @LineMgrEmail varchar(500);
DECLARE @Status varchar(500);
SET NOCOUNT ON;

IF EXISTS (SELECT * FROM inserted)
BEGIN
SELECT @us_email = EmailAddress
FROM inserted i,
Users u
WHERE u.UserNo = i.AssignedBy;

SELECT @LineMgrEmail = EmailLineManager
FROM inserted i,
Users u
WHERE u.UserNo = i.AssignedBy;

SELECT @CallNo ='Notification :Call Number is :' + SPACE(1)+ CallNumber
FROM inserted
SELECT @CallNo2 ='Notification :Call Closed, Call Number is :' + SPACE(1)+ CallNumber
FROM inserted

SELECT @Status =[Status]
FROM inserted


SELECT @CallComplaint = Complaint
FROM inserted

EXEC msdb.dbo.sp_send_dbmail
@recipients = @us_email,
@profile_name = 'SendEmailCM2',
@subject = @CallNo,
@body_format = 'html',
@body = @CallComplaint



END
END

What i want is if @Status=1 then
EXEC msdb.dbo.sp_send_dbmail
@recipients = @us_email,
@profile_name = 'SendEmailCM2',
@subject = @CallNo,
@body_format = 'html',
@body = @CallComplaint
and if @Status=2

EXEC msdb.dbo.sp_send_dbmail
@recipients = @EmailLineManager,
@profile_name = 'SendEmailCM2',
@subject = @CallNo2,
@body_format = 'html',
@body = @CallComplaint

How to use CASE or If in between.
Please help on this

Thanks
Basit.
Wendelius 17-Aug-15 3:27am    
Not sure if I understand the question correctly, but you can use simply

IF @Status = 1 BEGIN
EXEC msdb.dbo.sp_send_dbmail
...
END
IF @Status = 2 BEGIN
EXEC msdb.dbo.sp_send_dbmail
...
END
basitsar 17-Aug-15 3:55am    
Many Many Thanks. The problem has been solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900