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
LaurenP6777LaurenP6777 

Simple Trigger to Change Custom Object Record Type

Hi guys, 

I have a custom object called User_Support__c in SFDC. I have an "Intake" record type that is the default for all users. Depending on there answer to a single field, i would like a trigger to change the record type. I have a formula field called "Record_Type_Conversion_Controller__c" that will determine the name of the record type i would like the record to be changed to. 

I get the following error when I try it : ConvertRecordType: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ConvertRecordType: line 8, column 1 


Trigger ConvertRecordType on User_Support__c (after insert) {
User_Support__c u= Trigger.new[0];{
   

         //hold current User Support Fields
         u = [select Id, Record_Type_Conversion_Controller__c,Issue_Type__c, Name FROM User_Support__c WHERE Id=:u.id];
         
        RecordType RecType = [Select Id From RecordType Where SobjectType = 'Opportunity' and DeveloperName=:u.Record_Type_Conversion_Controller__c];
         

         If(u.Issue_Type__c =='License Request')
         u.name='License Request';
         If(u.Issue_Type__c !='License Request')
         u.name='';
         
        
         u.RecordTypeId=RecType.Id;
         
         update u;
         
         }
         }


If anyone could help it woudl be greatly appreciated!!!!
Best Answer chosen by LaurenP6777
AMIT KAMBOJAMIT KAMBOJ
Hi Lauren,
Please try to use below updated code. There may be some syntax errors. Let me know if you face any.
If this answer resolves the issue then please mark this answer as solution
Trigger ConvertRecordType on User_Support__c (after insert) 
{

         //hold current User Support Fields
         List<User_Support__c> ulist = [select Id, Record_Type_Conversion_Controller__c,Issue_Type__c, Name FROM User_Support__c WHERE Id in :Trigger.newMap.keySet()];
		 List<User_Support__c> ufinallist = List<User_Support__c>();
         if(ulist.size() >0)
		 {
			for(User_Support__c urecord : ulist )
				{
					RecordType RecType = [Select Id From RecordType Where SobjectType = 'Opportunity' and DeveloperName=:urecord.Record_Type_Conversion_Controller__c];
					if(RecType !=null )
						{
							If(urecord.Issue_Type__c =='License Request')
								{
									urecord.name='License Request';
								}
							 else
								{
									urecord.name='';
								}	
							 
							 urecord.RecordTypeId=RecType.Id;
						}
						ufinallist.add(urecord);
				}
			update ufinallist;
		 }	 
}

 

All Answers

AMIT KAMBOJAMIT KAMBOJ
Hi Lauren,
Please try to use below updated code. There may be some syntax errors. Let me know if you face any.
If this answer resolves the issue then please mark this answer as solution
Trigger ConvertRecordType on User_Support__c (after insert) 
{

         //hold current User Support Fields
         List<User_Support__c> ulist = [select Id, Record_Type_Conversion_Controller__c,Issue_Type__c, Name FROM User_Support__c WHERE Id in :Trigger.newMap.keySet()];
		 List<User_Support__c> ufinallist = List<User_Support__c>();
         if(ulist.size() >0)
		 {
			for(User_Support__c urecord : ulist )
				{
					RecordType RecType = [Select Id From RecordType Where SobjectType = 'Opportunity' and DeveloperName=:urecord.Record_Type_Conversion_Controller__c];
					if(RecType !=null )
						{
							If(urecord.Issue_Type__c =='License Request')
								{
									urecord.name='License Request';
								}
							 else
								{
									urecord.name='';
								}	
							 
							 urecord.RecordTypeId=RecType.Id;
						}
						ufinallist.add(urecord);
				}
			update ufinallist;
		 }	 
}

 
This was selected as the best answer
Rahul BorgaonkarRahul Borgaonkar
Writing a SOQL in for loop is not recommended because of governor limits. Should run SOQL outside loop get results in list variable then process that list variable in for loop.

Regards
 
LaurenP6777LaurenP6777
Thank you, Amit for your help. Your code did error but when I put a "new" in front of  'List<User_Support__c>();', it let me save. However, it still didn't work but I figured it out. Instead of saying 'DeveloperName=:urecord.Record_Type_Conversion_Controller__c];', I put 'Name=:urecord.Record_Type_Conversion_Controller__c];'. And it works now!!!

here is the final code...
 
Trigger ConvertRecordType on User_Support__c (after insert) 
{

         //hold current User Support Fields
         List<User_Support__c> ulist = [select Id, Record_Type_Conversion_Controller__c,Issue_Type__c, Name FROM User_Support__c WHERE Id in :Trigger.newMap.keySet()];
		 List<User_Support__c> ufinallist = new List<User_Support__c>();
         if(ulist.size() >0)
		 {
			for(User_Support__c urecord : ulist )
				{
					RecordType RecType = [Select Id From RecordType Where SobjectType = 'User_Support__c' and Name=:urecord.Record_Type_Conversion_Controller__c];
					if(RecType !=null )
						{
							If(urecord.Issue_Type__c =='License Request')
								{
									urecord.name='License Request';
								}
							 else
								{
									urecord.name='';
								}	
							 
							 urecord.RecordTypeId=RecType.Id;
						}
						ufinallist.add(urecord);
				}
			update ufinallist;
		 }	 
}