+ Start a Discussion
Naren9Naren9 

SOQL query in VF Page returning error

Hi 
I have created a VF page and it call a custom extension controller.
In the custom extension conroller, if I use the below Select statement, it is throwing the error: 
List has no rows for assignment to SObject

String ProdName = csvRecordData[1];//From CSV file I am reading the data into a string Variable.
String name2 = [SELECT Id, Name FROM Product2 WHERE Name = :ProdName LIMIT 1].Name;

I am not sure what went wrong with the above query.

If I used as below, then it is working. 
String name2 = [SELECT Id, Name FROM Product2 WHERE Name = 'Test123' LIMIT 1].Name;

Thanks,
Naren
Sunil RathoreSunil Rathore
Hi Naren,

Greetings to you!!!

The error is throwing because there is no record found in the query where name =: ProdName.

Use the Debug log to check the ProdName value and also make sure that the product should be present in org.

Let me know if it helps you. If yes, then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore  
Naren9Naren9
Thank you Sunil for the reply.
Product is existed in the System and when I do a Direct search I am getting the result, but when I do a Search based on variable, i am getting the error.
Below is the latest code with debug.
               system.debug(csvFileLines.size());
               String ProdName = csvRecordData[1];
               system.debug('Product Name'+ ProdName);
               String name1 = [SELECT Id, Name FROM Product2 WHERE Product2.Name = 'RCHF212A' LIMIT 1].Name;
               system.debug('ProductNameFromSOQLwithHardcodedProductName'+name1);
               String name2 = [SELECT Id, Name FROM Product2 WHERE Product2.Name = :ProdName LIMIT 1].Name;
               system.debug('ProductNameFromSOQLVariableSubstitution'+name2);


User-added image
Thanks,
Naren