+ Start a Discussion
divya1234divya1234 

store record type id and name fin the custom setting and use custom setting values in trigger

i wrote a trigger which is use to create a record on case satus update, but now i have to store all record types in custom setting (so that it is easy to add or remove) based on requirment ,can someone please help me to create custom settings ,i am not sure ow to call custom setting under if condition.

Thanks in advance
Best Answer chosen by divya1234
edanna kedanna k
Dear divya1234,

1. Create list custom setting with fields RecordTypeID__c and RecordTypeName__c. (Ex: ist custom setting - CaseCSRecTypes__c)
Please use this custom setting as in below code!
trigger CustomSettingTrigger on Case(before insert, before update) {
	for(Case c1: Trigger.new){
		for (CaseCSRecTypes__c caseItem : CaseCSRecTypes__c.getAll().values()) {
			if(caseItem.RecordTypeName__c != c1.RecordType__c){
				c1.RecordType__c = caseItem.RecordTypeName__c;
			}
		 }
	}
}

NOTE: If you're thinking of using list custom settings, consider using custom metadata types instead. 
Unlike list custom settings, you can migrate the records of custom metadata types using packages or Metadata API tools. 

Please let me know if it helps OR need more information!

All Answers

edanna kedanna k
Dear divya1234,

1. Create list custom setting with fields RecordTypeID__c and RecordTypeName__c. (Ex: ist custom setting - CaseCSRecTypes__c)
Please use this custom setting as in below code!
trigger CustomSettingTrigger on Case(before insert, before update) {
	for(Case c1: Trigger.new){
		for (CaseCSRecTypes__c caseItem : CaseCSRecTypes__c.getAll().values()) {
			if(caseItem.RecordTypeName__c != c1.RecordType__c){
				c1.RecordType__c = caseItem.RecordTypeName__c;
			}
		 }
	}
}

NOTE: If you're thinking of using list custom settings, consider using custom metadata types instead. 
Unlike list custom settings, you can migrate the records of custom metadata types using packages or Metadata API tools. 

Please let me know if it helps OR need more information!
This was selected as the best answer
divya1234divya1234
Thanks Edanna.
I think using custom metadat would be a good idea. what custom field i should create while using custom metadata? and how i can call in trigger

Here is my requirment...

I am creating caseemail record  for cetain case status and record type ,i have create record for satus and record type combitions. Also after creating a record i need to set on filed case notificatios = some value.

Currently i created  recordtype name, record type id ,status and notification field,but not sure how i can use tis fileds in my code , can you please give me some idea on that.

Thanks in Advance 
edanna kedanna k
Dear Divya,

Please have a look on the below code!
 
trigger CustomMetaDataTypeTrigger on Case(before insert, before update) {

TestCMT__mdt[] cMTypeRecords = [SELECT RecordTypeName__c, Status__c FROM TestCMT__mdt];//Querying custom metadata type records. 

	for(Case c1: Trigger.new){
		for (TestCMT__mdt caseItem : cMTypeRecords) {
			if(caseItem.RecordTypeName__c != c1.RecordType__c){
				c1.RecordType__c = caseItem.RecordTypeName__c;
			}
		 }
	}
}