+ Start a Discussion
Jaynandan Prasad 8Jaynandan Prasad 8 

trigger to change the record type

Wrote a trigger to change the record type after a field getting populated in the same object. Its getting saved, but not working.

trigger RecordTypeMap on Case (before insert) {

Map<Id, String> typeMap = New Map<Id, String>();
for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) {
     typeMap.put(rt.Id,rt.DeveloperName);
   }

 for (Case cc : trigger.new)  {

      
      if (cc.Master_Record_Type__c != null) { 
            
               id recid = typeMap.get(cc.Master_Record_Type__c);
               recordtype rectype = [select id, developername from recordtype where id=:recid];
               cc.RecordTypeid = rectype.id; 
              
            }
       }
   }
Best Answer chosen by Jaynandan Prasad 8
ManojjenaManojjena
Try with below code also it may help .
 
trigger RecordTypeMap on Case (before insert) {
	for (Case cc : trigger.new)  {
       if (cc.Master_Record_Type__c != null) { 
	       cc.RecordTypeid = Case.sObjectType.getDescribe().getRecordTypeInfosByName().get(cc.Master_Record_Type__c)getRecordTypeId(); 
       }
   }
}

Thanks 
Manoj

All Answers

ManojjenaManojjena
Hi Janardan,

What is Master_Record_Type__c in case what is value you are storing in that field ?

 
Jaynandan Prasad 8Jaynandan Prasad 8
so here's the use case-
 I am doing a salesforce  to saleforce connection on Case object. Now as Record type cannot be synched in S2S connection, I created a custom text field - Master_Record_Type__c in the target org which will hold the record type name from the source Org.  Once this field is populated with the String value i.e record type name from the source Org..it should change the current record type in the Target org  accordingly..

 
ManojjenaManojjena
Hi Janardan ,

Try with below it may help but still you have query in for loop it wil be issue .
 
trigger RecordTypeMap on Case (before insert) {
	Map< String,Id> typeMap = New Map< String,Id>();
	for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) {
		 typeMap.put(rt.DeveloperName,rt.Id);
	}
    for (Case cc : trigger.new)  {
       if (cc.Master_Record_Type__c != null) { 
             Id recid = typeMap.get(cc.Master_Record_Type__c);
               recordtype rectype = [select id, DeveloperName from RecordType where id=:recid];
               cc.RecordTypeid = rectype.id; 
              
       }
   }
}

Let me know if it helps 
ManojjenaManojjena
Try with below code also it may help .
 
trigger RecordTypeMap on Case (before insert) {
	for (Case cc : trigger.new)  {
       if (cc.Master_Record_Type__c != null) { 
	       cc.RecordTypeid = Case.sObjectType.getDescribe().getRecordTypeInfosByName().get(cc.Master_Record_Type__c)getRecordTypeId(); 
       }
   }
}

Thanks 
Manoj
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Janardan ,

Please try below code :-
trigger RecordTypeMap on Case (before insert) 
{
	Map< String,RecordType> typeMap = New Map< String,RecordType>();
	for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Case']) 
	{
		 typeMap.put( rt.DeveloperName , rt );
	}
	
    for (Case cc : trigger.new)  
	{
        if (cc.Master_Record_Type__c != null) 
	    { 
			if(typeMap.containsKey(cc.Master_Record_Type__c) )
			{
               RecordType rectype = typeMap.get( cc.Master_Record_Type__c );
               cc.RecordTypeid = rectype.id; 
            }  
        }
   }
}

If you are record Type Name in "Master_Record_Type__c" field then please add below code in above code
 
Map< String,RecordType> typeMap = New Map< String,RecordType>();
	for(RecordType rt: [Select Name, Id From RecordType Where sObjectType = 'Case']) 
	{
		 typeMap.put( rt.Name, rt );
	}




NOTE :- Never use SOQL inside FOR loop like below :-
for (Case cc : trigger.new)  {
       if (cc.Master_Record_Type__c != null) { 
             Id recid = typeMap.get(cc.Master_Record_Type__c);
               recordtype rectype = [select id, DeveloperName from RecordType where id=:recid];
               cc.RecordTypeid = rectype.id; 
              
       }

Please let me know if this will help you.


 
Mohit Bansal6Mohit Bansal6
Hi Janardan

Normally, We can get the Recordtype names for any sObject by using SOQL

[SELECT ID,Name, SobjectType from Recordtype WHERE SobjectType='MyObject__c' AND Name = 'MyRecordTypeName']

But it will count against your SOQL limit:

Below method will bypass the need of SOQL Query Governing LIMIT.
Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
Schema.SObjectType s = sObjectMap.get('MyObject__c') ; // getting Sobject Type
Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName();   //getting all Recordtype for the Sobject
Id rtId = recordTypeInfo.get('Record Type Name').getRecordTypeId();//particular RecordId


So you can use below complete code for trigger:
trigger RecordTypeMap on Case (before insert) 
{
	Map< String,RecordType> typeMap = New Map< String,RecordType>();

        Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
        Schema.SObjectType s = sObjectMap.get('Case') ; // getting Sobject Type
        Schema.DescribeSObjectResult resSchema = s.getDescribe() ; 
        Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName();	
        
        for(String s: recordTypeInfo.keyset()) 
	{
		 typeMap.put(s, s.getRecordTypeId() );
	}
 	
        for (Case cc : trigger.new)  
	{
        if (cc.Master_Record_Type__c != null) 
	    { 
			if(typeMap.containsKey(cc.Master_Record_Type__c) )
			{
               RecordType rectype = typeMap.get( cc.Master_Record_Type__c );
               cc.RecordTypeid = rectype.id; 
            }  
        }
   }
}

Regards
Mohit Bansal


In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.