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
Amr MohsenAmr Mohsen 

Update Record type of all cases

Hello, 
I've a field in cases called type which is picklist field and I want to copy the value from this field to CaseRecordType my code is 
public class casePatcherController {
    public PageReference UpdateCases(){
        List<Case> cList = [SELECT Type, Sub_Type__c FROM Case];
        for(Case c:cList ){
            Recordtype rec = [SELECT id,Name FROM RecordType Where Name=:c.Type];
            If(rec!=null){
                c.RecordType = rec;
            }
        }
        update cList;
        return null;
    }
}

But I get this error :

List has no rows for assignment to SObject
Error is in expression '{!UpdateCases}' in component <apex:commandButton> in page casepatcher: Class.casePatcherController.UpdateCases: line 5, column 1

I need help please.
Best Answer chosen by Amr Mohsen
Pankaj_GanwaniPankaj_Ganwani
Hi,

It seems that you have not followed the best practices of coding. I made some updates in your code. Please find the same:
 
public class casePatcherController {
    public PageReference UpdateCases(){
	    Set<String> setType = new Set<String>();
		Map<String,Id> mapTypeToRecordTypeId = new Map<String,Id>();
        List<Case> cList = [SELECT Type, Sub_Type__c FROM Case where c.Type!=NULL];
		
		for(RecordType objRT : [select Id, Name from RecordType where SobjectType = 'Case'])
		{
			mapTypeToRecordTypeId.put(objRT.Name, objRT.Id);
		}
		
        for(Case c:cList ){
            if(mapTypeToRecordTypeId.containskey(c.Type))
				c.RecordTypeId = mapTypeToRecordTypeId.get(c.Type);
        }
		
        update cList;
        return null;
    }
}

 

All Answers

Temoc MunozTemoc Munoz
Hi Amr.

First of all, it's not a good practice to quey a record inside a for loop. Can you try changing this into a Map for example?

Going back to your error, rec is returning zero elements, so Salesforce will stop all execution at this point. To avoid this, always query in bulk (i.e. use a list or set) to grab the record type.
 
List<Recordtype> recs = [SELECT id,Name FROM RecordType Where Name=:c.Type];
 
if(recs.size() > 0)
{
   c.RecordType = recs.get(0);
}

The above code snippet may vary once you refactor your code to avoid using a SOQL query inside a for loop.

Thanks
Pankaj_GanwaniPankaj_Ganwani
Hi,

It seems that you have not followed the best practices of coding. I made some updates in your code. Please find the same:
 
public class casePatcherController {
    public PageReference UpdateCases(){
	    Set<String> setType = new Set<String>();
		Map<String,Id> mapTypeToRecordTypeId = new Map<String,Id>();
        List<Case> cList = [SELECT Type, Sub_Type__c FROM Case where c.Type!=NULL];
		
		for(RecordType objRT : [select Id, Name from RecordType where SobjectType = 'Case'])
		{
			mapTypeToRecordTypeId.put(objRT.Name, objRT.Id);
		}
		
        for(Case c:cList ){
            if(mapTypeToRecordTypeId.containskey(c.Type))
				c.RecordTypeId = mapTypeToRecordTypeId.get(c.Type);
        }
		
        update cList;
        return null;
    }
}

 
This was selected as the best answer
Amr MohsenAmr Mohsen
Thank you , But I got this error :
Didn't understand relationship 'c' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Sorry I'm still new to salesforce
Pankaj_GanwaniPankaj_Ganwani
I got the issue. Please update the line no 5 with the below mentioned line:

List<Case> cList = [SELECT Type, Sub_Type__c FROM Case where Type!=NULL];
Amr MohsenAmr Mohsen
Thank you Pankaj I got it too , I did similar solution.
List<Case> cList = [SELECT Type, Sub_Type__c FROM Case c where c.Type!=NULL];
It was the alias of the object I think.

Thank you again Pankaj.