function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pradyprady 

sending email from trigger on a custom object

Hi,

 

I want to send email when a field is updated in a custom object. so i have a trigger written. i also created a email template.

I find that the targetobjectid is a required field while sending an email. Its erroring out where i am setting targeto

bject .Id. It says Save error: Variable does not exist: Lstuser.id

Any ideas what am i doing wrong

trigger change_action on Ticket__c (after update) {
	//List<user>Lstuser= new List<user>();
	List<id> lstid=new List<id>();
	List<string> lstemail= new List<string>();
	Map<id,string> Mapemail = new Map<id,string>();
	

	for(ticket__c t:trigger.new)
	{
		if(trigger.isUpdate)
		{
			if(t.Actions__c!=System.Trigger.oldMap.get(t.Id).Actions__c)
			{
				lstemail.clear();
				lstemail.add(t.Initiator_Email__c);
				lstemail.add(t.Admin_email__c);
				
				for(User Lstuser:[select id, email,username from user where email in :lstemail]);
				{
				List<EmailTemplate> emailtemplatelist = new List<EmailTemplate>();
				emailtemplatelist=[select id, name from EmailTemplate where name='Update on ticket'];
				if (emailtemplatelist.size()>0)
				{
					//emailtemplatelist[0]
					Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
					mail.toAddresses = new string[]{t.Initiator_Email__c,t.Admin_email__c};
					//mail.setToAddresses(t.Initiator_Email__c);
					mail.setTemplateId(emailtemplatelist[0].id);
/**************************Its THROWING ERROR ON BELOW LINE **********************************/ mail.TargetObjectId(Lstuser.id);//ERROR ON THIS LINE Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } } } } }

 

dmchengdmcheng

The correct method is Mail.setTargetObjectID()

UVUV

Why are you not using workflow????

pradyprady

Umesh,

 

Can i have a workflow which will fire when a field is updated? I mean any update, there is nothing that i can check it against. The field is a text field so users can enter anything. Is there a way to check if the field changed.

 

Thanks

UVUV

In the Rule criteria you see two values-

1-Criteria are met

2-Formula Evaluates to true.

 

use 2nd option along with function ISCHANGED(field) and then define your action.