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
Martin_DeloitteMartin_Deloitte 

Change owner in trigger - remove automatic email

Hello,

We have added a field "Assign to" on Case (a lookup on user) to let Salesforce.com users the ability to change the owner of a case from the case list. The Case owner is then updated via a trigger (before update on case) that is copying the "Assign to" field value into the Case.owner field.

The trigger works fine and the case owner is updated correctly.

The issue is that when the case owner is updated via that procedure, an automatic email is sent to the new Case owner to warn him that he s now assigned to the case. We do NOT want this email to be sent.

How can we prevent that email to be sent? Is there a piece of code available for that?


Here is the code I'm using currently:

trigger AssignToCaseOwner on Case (before update) {
	Integer i = 0;
	for(Case theCase : System.Trigger.new)
	{
		// only executed if the case owner is not changed as well 
		// and if the case owner is not already the "Assign to" user
		if(theCase.OwnerId == System.Trigger.old.get(i).OwnerId && 
			theCase.Assign_to__c != null && 
			theCase.Assign_to__c != System.Trigger.old.get(i).Assign_to__c && 
			theCase.OwnerId != theCase.Assign_to__c)
		{
			theCase.OwnerId = theCase.Assign_to__c;
			// set DML options to remove the auto email -> NOT WORKING
			Database.DMLOptions dlo = new Database.DMLOptions();
			dlo.EmailHeader.triggerUserEmail = false;
			theCase.setOptions(dlo);
		}
		
		// Make the "Assign to" field empty as we do not need the value 
		// to be stored in the record
		theCase.Assign_to__c = null;
	
		i++;
	}
}

 

 

Thanks on beforehand,
Martin HUBERT.


kiranmutturukiranmutturu

create a checkbox field in the backend defalut as false and dont expose that field in the lay out...and in the updation make that field as true and change the workflow logic as when the new field value is false and your previous condition.. then in the second scenario it will never send an email.. hope you got my  point... 

r1985r1985

I understand u dont want to send a mail to the new case owner when a case is assigned to him. For this u have to modify the support settings in case object.

 

Thanks,

Malar

 

Shashikant SharmaShashikant Sharma

 

try with this once

trigger AssignToCaseOwner on Case (before update) {
	Integer i = 0;
	for(Case theCase : System.Trigger.new)
	{
		// only executed if the case owner is not changed as well 
		// and if the case owner is not already the "Assign to" user
		if(theCase.OwnerId == System.Trigger.old.get(i).OwnerId && 
			theCase.Assign_to__c != null && 
			theCase.Assign_to__c != System.Trigger.old.get(i).Assign_to__c && 
			theCase.OwnerId != theCase.Assign_to__c)
		{
			theCase.OwnerId = theCase.Assign_to__c;
			// set DML options to remove the auto email -> NOT WORKING
			Database.DMLOptions dlo = new Database.DMLOptions();
			dlo.EmailHeader.triggerUserEmail = false;
                        dlo.EmailHeader.= false;
                        dlo.EmailHeader.triggerAutoResponseEmail = false;
			theCase.setOptions(dlo);
		}
		
		// Make the "Assign to" field empty as we do not need the value 
		// to be stored in the record
		theCase.Assign_to__c = null;
	
		i++;
	}
}

If it does not work please let me know this statement "case owner is updated via that procedure," what do you mean by procedure ant apex class method?

CLKCLK

No need to write to code for it. It's configuration change.

Go to setup->customize-> case ->support settings -> Notify Case Owners when Case Ownership Changes; and uncheck that checkbox.

 

 

Martin_DeloitteMartin_Deloitte

Unfortunately, this solution doesn't work... it is already unchecked...


CLK wrote:

No need to write to code for it. It's configuration change.

Go to setup->customize-> case ->support settings -> Notify Case Owners when Case Ownership Changes; and uncheck that checkbox.

 

 


Any other alternatives?

Martin_DeloitteMartin_Deloitte

Shashikant Sharma wrote:

 

try with this once

trigger AssignToCaseOwner on Case (before update) {
	Integer i = 0;
	for(Case theCase : System.Trigger.new)
	{
		// only executed if the case owner is not changed as well 
		// and if the case owner is not already the "Assign to" user
		if(theCase.OwnerId == System.Trigger.old.get(i).OwnerId && 
			theCase.Assign_to__c != null && 
			theCase.Assign_to__c != System.Trigger.old.get(i).Assign_to__c && 
			theCase.OwnerId != theCase.Assign_to__c)
		{
			theCase.OwnerId = theCase.Assign_to__c;
			// set DML options to remove the auto email -> NOT WORKING
			Database.DMLOptions dlo = new Database.DMLOptions();
			dlo.EmailHeader.triggerUserEmail = false;
                        dlo.EmailHeader.= false;
                        dlo.EmailHeader.triggerAutoResponseEmail = false;
			theCase.setOptions(dlo);
		}
		
		// Make the "Assign to" field empty as we do not need the value 
		// to be stored in the record
		theCase.Assign_to__c = null;
	
		i++;
	}
}

If it does not work please let me know this statement "case owner is updated via that procedure," what do you mean by procedure ant apex class method?


This is not working. The email is still sent out (I've removed the line that is not compiling before testing).

 

By procedure, I meant the piece of code in the trigger does update the case owner (meaning the piece of code works fine) but an email I'm not expecting is sent automatically.

 

A colleague told me it is a bug in SFDC... does someone knows more about it?

 

Thanks anyway.

 

b_kiltoffb_kiltoff

I've been working on this for hours, same issue. I've ruled out pretty much every documented control that has anything to do with email notification, and I do not think this is something that can be controlled through Setup menus or through Apex.

 

To recap: the problem is that in all other instances where case ownership is changed, the email notification to the new case owner is not sent. But when the trigger runs that reassigns a case to a new owner, the email notification is sent. I don't want this to happen.

 

Here's the code I've written for the trigger. You can see I've tried the Database.DMLOptions methods to prevent the auto email. Based on the fact that these methods fail, I think they do not have any bearing on case email notifications, (which is probably why emails notifications on case updates are not listed in the documented events that DMLOptions are meant to impact).

trigger caseAction2 on Case (before update) {
	Set<Id> setOfCaseIDs = new Set<Id>();
	Database.DMLOptions dlo = new Database.DMLOptions();
						dlo.EmailHeader.triggerUserEmail = false;
						dlo.EmailHeader.triggerOtherEmail = false;
						dlo.EmailHeader.triggerAutoResponseEmail = false;
	for (Case d : trigger.new){
		//test that the case.OwnerId in the new set is not the same as the case.OwnerId in the old set. If it is, <exit> (break or return?) 
		if(d.ownerId == Userinfo.getUserID() || d.ownerId != Trigger.oldMap.get(d.Id).OwnerId){			
			break;
		}
		else{		
			for (Case c : Trigger.new){
				setOfCaseIds.add(c.Id);
			}
			Map<Id, Case> mapOfCaseIDs = new Map<Id,Case>([SELECT Id, OwnerId from Case Where Id IN :setOfCaseIds]);
			for (Case c : trigger.new){
				//if the map set contains the case C
				if(mapOfCaseIDs.containsKey(c.Id)){
					//and the case owner ID is not the active user
					if(mapOfCaseIds.get(c.Id).OwnerId != Userinfo.getUserId()){
						c.setOptions(dlo);
						c.OwnerId = Userinfo.getUserID();	
					}
				}
			}
		}
	}
}

 This thread is several months old and if anyone has found a solution to it I'd love to hear about it.

jaslozjasloz

This is interesting, we are having a similar issue with apex changing the owner of a Lead.  We haven't tried hanging the DML settings to prevent the email notification, when we do we will bear in mind that it might NOT work and then report a case to salesforce.

CaukajunCaukajun

Has there been any resolution to this issue? I'm encountering the exact same thing in my own Case trigger. I'm programmaticly changing the Case Owner to a Queue in my trigger and when this happens it emails every user assigned to the queue. I've written my own custom email alerts so I really don't want to spam everyone whenever this trigger fires. I've already disabled the Automatic Case Notification option under Setup > Customize > Case > Support Settings and the email is still being sent. Any suggestions?

 

Thanks in advance. 

harryr3636harryr3636

Has anyone figured out a solution to this one? Running into the same problem and hoping there is a work-around.

CaukajunCaukajun

I was able to get things working in my org by doing a combination of things. First of all, as suggested previously in this thread, you need to disable automatic email alerts via the Setup menu (Case > Support Settings > Notify Case Owners when Case Ownership Changes) which will disable emails when the owner changes from one User to another.

 

My situation was complicated because I was often changing the Case Owner to one of the Queue's I had created. In order to prevent these emails from firing, I had to adjust the Queue settings under the Setup menu. These settings can be found under Administration Setup > Manage Users > Queues and selecting the Queue of interest. To disable all emails, I had to set the Queue email to a non-existant address and uncheck the Send Email to Queue Members checkbox.

 

These two solutions allowed me to suppress all Salesforce generated Case notifications with one exception: when a User manually changed the owner to another user using the "Change" link next to the Case Owner field and manually checked the Send Notification Email checkbox. 

harryr3636harryr3636

Hi, Thanks for the quick reply. Unfortunately, we have all of the config turned off like you do, but it's still sending the email when the trigger makes the adjustment. When a user does it normally, we're all set, but the trigger seems to be ignoring the config settings. Harry

kingapkingap
I am running into a similar issue: an Apex Trigger is changing lead owner for us.  (we let our users associate leads with account via a custom lookup field, and when they do that, the Apex trigger sets lead owner equal to account owner. ).  I can't find any way to have the trigger not send the automatic "lead has been assigned to you" email.  Is this a know salesforce issue we need to live with?
Michael HendersonMichael Henderson
Seems this is answered by the below Knowledge Article.  Since the update is implicit, and not actually a DML operation, it is ignoring the settings.  Solution is to use an @future method.

https://help.salesforce.com/apex/HTViewSolution?id=000176854&language=en_US

HTH