You need to sign in to do that
Don't have an account?
amrit
How to display opportunity name field in Task using trigger?
Hi,
I have created a trigger in task where the creator should recieve an email alert when the task is completed.Here the trigger is working fine.Creator has received the emailalert .But in emailalert i need to display opportunity name which is related to that task. Here i used WhatId. How do i get opportunity name instead of opportunity id
trigger EmailAlertforTaskCompletion on Task (before update) { set<String> setownerIds = new set<String>(); if(Trigger.isUpdate && Trigger.isBefore){ for(Task objT : Trigger.new){ if(objT.Status == 'Completed'&& ('' + objT.WhatId).startsWith('006')){ setownerIds.add(objT.CreatedById); List<User> lstU = [select id,email from User where Id in : setownerIds]; // limit 1 List<String> lstEmails = new List<String>(); for(User objU : lstU){ lstEmails.add(objU.email); } Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); List<String> toAddresses = new List<String>(); toAddresses.addall(lstEmails); mail.setToAddresses(toAddresses); mail.setSubject('A task assigned by you has Completed'); // Set the subject String template = 'Hello, \nYour assigned task has Completed. Here are the details - \n\n'; template+= 'Subject - {0}\n'; template+= 'Opportunity - {1}\n'; template+= 'Due Date - {2}\n'; template+= 'Priority - {3}\n'; template+= 'Comments - {4}\n'; String duedate = ''; if (objT.ActivityDate==null) duedate = ''; else duedate = objT.ActivityDate.format(); List<String> args = new List<String>(); args.add(objT.Subject); args.add(objT.WhatId); args.add(duedate); args.add(objT.Priority); args.add(objT.Description); String formattedHtml = String.format(template, args); mail.setPlainTextBody(formattedHtml); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } } }
Once u get the opportunity id, u will need to query the opportunity for the opportunity name.
Select name from opportunity where id =: whatid
U will need to use map to store name and associated opportunity id.
Your Query for user is within for loop, this violates basic rule of not putting query within for loop.
Query for opportunity name outside for loop,store names in map<id,name> and then use that name in email messages.
Hope this helps u to move in right direction.
All Answers
Once u get the opportunity id, u will need to query the opportunity for the opportunity name.
Select name from opportunity where id =: whatid
U will need to use map to store name and associated opportunity id.
Your Query for user is within for loop, this violates basic rule of not putting query within for loop.
Query for opportunity name outside for loop,store names in map<id,name> and then use that name in email messages.
Hope this helps u to move in right direction.
Hi,
Thanks for you reply.its working now