+ Start a Discussion
Madhu007Madhu007 

Regarding an email notification when task is completed

Hi,

 

I wrote a trigger for sending email notification when task status is completed and also task status update to completed.but i am getting two email notification when trigger fired.Please help me...

 

Thanks.....

KaityKaity

Can you please paste the code?

Madhu007Madhu007

This is the code.Please help...

Thanks in advance..

 


t

rigger UpdateTypePicklist on Task(after Insert,before Update) {
// Insert Record Into OAR Member Added Custom Object
if(!TaskTriggerHelper.hasAlreadyfired()){

if(trigger.isInsert || trigger.isUpdate){
List<Member__c> objList = new List<Member__c>();
List<Attachment> a1=new List<Attachment>();
// System.Debug('Stypes ------>' + stypes);
System.Debug('NEw trigger------>' +Trigger.new);
map<id,opportunity> opps = new map<id,opportunity>();
map<id,account> acc = new map<id,account>();
Lead led=new Lead();
List<Task> lsttask=Trigger.new;
for(Task T: lsttask){
set<id> stypes = new set<id>();
for(recordtype objr : [Select Id From RecordType where sObjectType='Task' and developername = 'Renewals_Record_Type']){
stypes.add(objr.id);
}
if(stypes.contains(T.RecordTypeId)&&(T.Status == 'Completed')){
Member__c obj = new Member__c();
obj.Text_1__c = T.Type__c;
obj.Text_2__c = T.Type_Detail__c;
obj.Text_3__c = T.Subject;

// WR# 258057- Code starts here
obj.Text_4__c = 'Renewals Type Email';
//WR# 258057 Code ends above

obj.Task_Comments__c = T.Description;
obj.Text_7__c = T.Status;
if(t.whatid!=null&&t.whatid.getsobjecttype()==opportunity.sobjecttype) {
opps.put(t.whatid,null);
}
opps.putAll([select id,Opportunity_Number__c from opportunity where id in :opps.keyset()]);
if(opps.containskey(t.whatid)) {
String Oppnum = opps.get(t.whatid).Opportunity_Number__c;
obj.Text_5__c = Oppnum;
Opportunity Opp1 = [select account.name from opportunity where id=:T.Whatid];
obj.Text_6__c=Opp1.account.name;
}

if(t.whatid!=null&&t.whatid.getsobjecttype()==account.sobjecttype) {
acc.put(t.whatid,null);
}
acc.putAll([select id,Name from account where id in :acc.keyset()]);
if(acc.containskey(t.whatid)) {
String Accname = acc.get(t.whatid).Name;
obj.Text_6__c = Accname;
}
String TempObj = Userinfo.getUserId();
System.Debug('Task Owner........' +TempObj);
if( t.ownerid.getsobjecttype() == user.sobjecttype ) {
obj.user_1__c = t.ownerid;
}
if( t.CreatedById.getsobjecttype() == user.sobjecttype && t.CreatedById != null) {
obj.User2__c = t.CreatedById;
}
System.Debug('Trigger.oldMap.get(T.Id).Owner....' + T.Owner);
String urlForObj= URL.getSalesforceBaseUrl().toExternalForm() + '/'+T.WhatId;
obj.Task_Related_To_URL__c = urlForObj;
String RelUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/'+T.Id;
obj.Task_URL__c = RelUrl;
// for lead owner
If(t.whoid != null ){
String urlForObj1= URL.getSalesforceBaseUrl().toExternalForm() + '/'+T.WhoId;
obj.Task_Related_To_URL__c = urlForObj1;
led=[select ownerid,Related_Account__r.Name from lead where id=:t.whoid];
obj.Text_6__c = led.Related_Account__r.Name;
for(user usr:[select email from user where id=:led.ownerid]){
obj.Email_notification_1__c=usr.email; 
system.debug('email add'+usr.email); 
}
}
// for Attachment on email template
for(Attachment a:[Select name,body,Id,parentid From Attachment where parentid=:t.id limit 1]){
attachment at=new attachment();
at.name=a.name;
at.body=a.body;
at.parentid='00X70000001ST05';
a1.add(at);
insert a1[0];
}
// obj.Text_6__c = acc1.name;
objList.add(obj);
}
}
if(!objList.isEmpty())
insert objList;
delete a1;
TaskTriggerHelper.setAlreadyfired();
}
}

 

KaityKaity

This trigger is not firing any email notification, it works on attaching the attachment and putting retriving the emailId for led. Some other metadatas are responsicle for sending 2 mails.

 

You open up your debug log and perform the action. Then the  paste the debug log. 

 

 

for(user usr:[select email from user where id=:led.ownerid]){
obj.Email_notification_1__c=usr.email;

Madhu007Madhu007

ther is workflow on member__c object. 

 

obj.Text_4__c = 'Renewals Type Email';

 

on this field, i wrote a workflow.It will send the email notification when trigger fired.but i am getting two email notifications.that is the issue.please help.......

Thanks in advance..

KaityKaity

Hi-

 

In this triggere you have not written the below line any where:

 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

So, I'm sure. this trigger is not responsible for sending mails. You run the debug and see introspectly and let me know.

 

-Kaity

Madhu007Madhu007

Hi

 

Thers is workflow on member__c object .Through workflow i addde email alert.It sent the email alert.

Thats why i don,t need this Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Thanks..