+ Start a Discussion
shravani milshravani mil 

Error:List has no rows for assignment to SObject

Hi
Can any one help me out of this error

System.QueryException: List has no rows for assignment to SObject .

error line recordtype = [SELECT Id FROM RecordType WHERE SobjectType = 'EXM__c' AND DeveloperName = : developerName LIMIT 1].Id;

Thanks.
Best Answer chosen by shravani mil
Akhil AnilAkhil Anil
Put the snippet in a try catch block as below.
 
try {
  
  recordtype = [SELECT Id FROM RecordType WHERE SobjectType = 'EXM__c' AND DeveloperName = : developerName LIMIT 1].Id;

}

catch(Exception e) {
 
recordtype = null;

}

Kindly mark it as a solution if that works.

All Answers

Akhil AnilAkhil Anil
Put the snippet in a try catch block as below.
 
try {
  
  recordtype = [SELECT Id FROM RecordType WHERE SobjectType = 'EXM__c' AND DeveloperName = : developerName LIMIT 1].Id;

}

catch(Exception e) {
 
recordtype = null;

}

Kindly mark it as a solution if that works.
This was selected as the best answer
KaranrajKaranraj
The SOQL query returns null value for your conditions so you are getting the error message, you must have null pointer check in your code.
Vishal Negandhi 16Vishal Negandhi 16
For such SOQL, you can better have a list variable and then check if list has some records. 

List<RecordType> lstErrorRecordType =  [SELECT Id FROM RecordType WHERE SobjectType = 'EXM__c' AND DeveloperName = : developerName LIMIT 1];

if(!lstErrorRecordType.isEmpty()){
// your code when you have such a record type
}
else{
// some message or email or or a default recordtype to be set or any action stating such record type //doesn't exist
}
David Holland 6David Holland 6
Agreed you should always have a list of values, then check if it is empty,
shravani milshravani mil
Hi All ,

Thanks for all yours quick reply.

this test is passing in uat and its getting failed in prod?

Thanks
KaranrajKaranraj
The reason you are getting error message in production is that particular recordtpye is not available in production instance.Instead of querying recordtype id from system, create new record type in your test class and keep "seeAllData = false" in your test class.
Add the following utility method in your test class
 
​public static Id getRecordTypeId(String ObjectName,String recordTypeName){

        Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
        Schema.SObjectType s = sObjectMap.get(ObjectName) ; 
        Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
        Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName(); 
        Id rtId = recordTypeInfo.get(recordTypeName).getRecordTypeId();
        return rtId;

    }

Then get the recordtype for the object by passing the objectName and the recordtypename
getRecordTypeId(Account,myRecordTypeName);

 
Carlos Tellez 13Carlos Tellez 13
So is it the usual mistakes of not announcing variables? What is the initial expression to assign to the variable? I apologize for stupid questions as I am new to SOQL. I used to ask essay writers (http://pimion.com) to write my codes but it was poor university level not an advanced as I require for my job.