+ Start a Discussion
Mandakini SaranuMandakini Saranu 

How to check on when a trigger to fire using custom setting?

Best Answer chosen by Mandakini Saranu
GauravTrivediGauravTrivedi
Mandakini, for this you need to create one custom setting something like migration and create one checkbox field MigrationFlag__c. Create a record with name = Migration and checkbox should be checked when you deploy it to different sandbox and uncheck it after deployment.
Your code will look like this :
trigger UserTrigger on User (after insert,after update) {
	CustomSetting__c custom =  CustomSetting__c.getValues('Migration');
	Boolean byPassFlag = custom.MigrationFlag__c;
	if(byPassFlag){
		List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
		User u = [select firstname,username,LastModifiedBy.name,email from user where id=:userinfo.getuserid()];
		if(Trigger.isInsert){
			for(User myUser:Trigger.new){
				Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
				List<String> sendTo=new List<String>();
				sendTo.add(myUser.Email);
				m.setToAddresses(sendTo);
				m.setSubject('User Created ');
				string emailBody='User Details:';
				emailBody+='<br><br>User Name:' +myUser.firstname;
				emailBody+='<br><br>User Email:' +myUser.Email;
				emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
				emailBody+='<br><br>Username: ' +myUser.Username;
				emailBody+='<br><br>User createdby:' +u.LastModifiedBy.name;
				m.setHtmlBody(emailBody);
				mails.add(m);
				Messaging.sendEmail(mails);
			}
		}
		else if(Trigger.isUpdate){
			for(User myUser:Trigger.new){
				User oldUser = Trigger.OldMap.get(myUser.id);
				if (myUser.isActive != oldUser.isActive) {
					Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
					List<String> sendTo=new List<String>();
					sendTo.add(myUser.Email);
					m.setToAddresses(sendTo);
					m.setSubject('User Deactivated');
					string emailBody='User Details:';
					emailBody+='<br><br>User Name:' +myUser.firstname;
					emailBody+='<br><br>User Email:' +myUser.Email;
					emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
					emailBody+='<br><br>Username:' +myUser.Username;
					emailBody+='<br><br>User Deactivatedby:' +u.LastModifiedBy.name;
					m.setHtmlBody(emailBody);
					mails.add(m);
					Messaging.sendEmail(mails);
				}
			}
		}
	}
}

Kodus and your feedback will be highly appreciated.

All Answers

GauravTrivediGauravTrivedi
What exactly you want to do, could you please elaborate.
Mandakini SaranuMandakini Saranu
Hi Gaurav,

I want to use a custom setting to control on the trigger on user object when to fire and not.

The trigger usually fires when a new user is created or existing user is deactivated to send an email alert.
trigger UserTrigger on User (after insert,after update) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
User u = [select firstname,username,LastModifiedBy.name,email from user where id=:userinfo.getuserid()];
if(Trigger.isInsert){
for(User myUser:Trigger.new){
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
List<String> sendTo=new List<String>();
sendTo.add(myUser.Email);
m.setToAddresses(sendTo);
m.setSubject('User Created ');
string emailBody='User Details:';
emailBody+='<br><br>User Name:' +myUser.firstname;
emailBody+='<br><br>User Email:' +myUser.Email;
emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
emailBody+='<br><br>Username: ' +myUser.Username;
emailBody+='<br><br>User createdby:' +u.LastModifiedBy.name;
m.setHtmlBody(emailBody);
mails.add(m);
Messaging.sendEmail(mails);
}
}
else
if(Trigger.isUpdate){
for(User myUser:Trigger.new)
{
User oldUser = Trigger.OldMap.get(myUser.id);
if (myUser.isActive != oldUser.isActive) {
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
List<String> sendTo=new List<String>();
sendTo.add(myUser.Email);
m.setToAddresses(sendTo);
m.setSubject('User Deactivated');
string emailBody='User Details:';
emailBody+='<br><br>User Name:' +myUser.firstname;
emailBody+='<br><br>User Email:' +myUser.Email;
emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
emailBody+='<br><br>Username:' +myUser.Username;
emailBody+='<br><br>User Deactivatedby:' +u.LastModifiedBy.name;
m.setHtmlBody(emailBody);
mails.add(m);
Messaging.sendEmail(mails);
}
}
}
}
 
GauravTrivediGauravTrivedi
If you want to store something in Custom setting then you can use before(insert, update) trigger event to perform this action and use something like this 
if(Trigger.isBefore){
	insert timestamp or something you want in to be stored in custom setting
}

Hope it will work for you. let me know if you have got your objective.
Mandakini SaranuMandakini Saranu
Gaurav, actually I want to use custom setting to activate/deactivate the trigger during data migration so it is not crossing the limitations. Thanks in advance.
 
GauravTrivediGauravTrivedi
Mandakini, for this you need to create one custom setting something like migration and create one checkbox field MigrationFlag__c. Create a record with name = Migration and checkbox should be checked when you deploy it to different sandbox and uncheck it after deployment.
Your code will look like this :
trigger UserTrigger on User (after insert,after update) {
	CustomSetting__c custom =  CustomSetting__c.getValues('Migration');
	Boolean byPassFlag = custom.MigrationFlag__c;
	if(byPassFlag){
		List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
		User u = [select firstname,username,LastModifiedBy.name,email from user where id=:userinfo.getuserid()];
		if(Trigger.isInsert){
			for(User myUser:Trigger.new){
				Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
				List<String> sendTo=new List<String>();
				sendTo.add(myUser.Email);
				m.setToAddresses(sendTo);
				m.setSubject('User Created ');
				string emailBody='User Details:';
				emailBody+='<br><br>User Name:' +myUser.firstname;
				emailBody+='<br><br>User Email:' +myUser.Email;
				emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
				emailBody+='<br><br>Username: ' +myUser.Username;
				emailBody+='<br><br>User createdby:' +u.LastModifiedBy.name;
				m.setHtmlBody(emailBody);
				mails.add(m);
				Messaging.sendEmail(mails);
			}
		}
		else if(Trigger.isUpdate){
			for(User myUser:Trigger.new){
				User oldUser = Trigger.OldMap.get(myUser.id);
				if (myUser.isActive != oldUser.isActive) {
					Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
					List<String> sendTo=new List<String>();
					sendTo.add(myUser.Email);
					m.setToAddresses(sendTo);
					m.setSubject('User Deactivated');
					string emailBody='User Details:';
					emailBody+='<br><br>User Name:' +myUser.firstname;
					emailBody+='<br><br>User Email:' +myUser.Email;
					emailBody+='<br><br>SFDC License Type:' +myUser.SFDC_License_Type__c;
					emailBody+='<br><br>Username:' +myUser.Username;
					emailBody+='<br><br>User Deactivatedby:' +u.LastModifiedBy.name;
					m.setHtmlBody(emailBody);
					mails.add(m);
					Messaging.sendEmail(mails);
				}
			}
		}
	}
}

Kodus and your feedback will be highly appreciated.
This was selected as the best answer
Mandakini SaranuMandakini Saranu
Thank you so much Gaurav. That did work perfectly.
Mandakini SaranuMandakini Saranu
As I am completely new to test classes can somebody help me with a test class for this trigger.