You need to sign in to do that
Don't have an account?
Amr 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
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.
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.
It seems that you have not followed the best practices of coding. I made some updates in your code. Please find the same:
All Answers
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.
The above code snippet may vary once you refactor your code to avoid using a SOQL query inside a for loop.
Thanks
It seems that you have not followed the best practices of coding. I made some updates in your code. Please find the same:
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
List<Case> cList = [SELECT Type, Sub_Type__c FROM Case where Type!=NULL];
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.