+ Start a Discussion

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


   Below is my code which is working properly in Dev org but not in Production. From production am geeting exception.


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


Class.JobSuite.RetrieveTaskNewTesting.<init>: line 731, column 1



public List<Job_Task__c> lstJT{get;set;}


public RetrieveTaskNewTesting(ApexPages.StandardController st)
jobId = ApexPages.currentPage().getParameters().get('id');
Job__c objJob=[Select Id,Campaign__c from Job__c where Id=: jobId LIMIT 1];
lstJT=[Select Name, Start_Date__c,Days__c,Due_Date__c,Task_Order__c,Assigned_Users__c,Marked_Done__c,Completion_Date__c,Revised_Due_Date__c from Job_Task__c where Job__c =: jobId Order By Task_Order__c];
lstJobTeam=[Select Id, Role__c,Staff__c, Job__r.Name from Job_Team__c where Job__c =: jobId order by createddate];



  Thanks in advance







Darshan FarswanDarshan Farswan

This exception usually occurs when you try to assign the value to a single Object variable and the query does not return any row. But in this case you are storing the value in the list lstJT . So the error should not be there in the first place. Are you sure if this is the line that is thorowing the error. 


Just for some safety reasons, try to initialize the variable inside the constructor.


lstJT = new List<Job_Task__c>();

 This is just precautionary. I am not sure if this will work.


But it is working fine in dev org ..

I have tried in developer console with the query that have no records

Darshan FarswanDarshan Farswan

I tried all the possible methods, but could not create the issue. The Exception will only occur while assigning the query record to a Single sObject. Even the developer console in Production does not show any exception for the query that does not return any record.




         The line numbers displaying in mail  from salesforce for exception are correct  or we can expect some other place also?

Darshan FarswanDarshan Farswan

The line where this exception can arise is this one :


Job__c objJob=[Select Id,Campaign__c from Job__c where Id=: jobId LIMIT 1];

 Can you please see the code at line 731. I suppose it is same as the mentioned above.


We are getting jobId from url using 


jobId = ApexPages.currentPage().getParameters().get('id'); 


So  there must be 1 record to asign to object odjJob and campaignId is  a string : public String campaignId{get;set;}

Darshan FarswanDarshan Farswan

With the code that you have written, the only place where Exception can be thrown is the line I have mentioned above. What you are saying is also logically correct that the id must have some value. So there is no reason why the code should throw exception.


Also this class seems to be a Controller extension for Standard Controller 'Job__c', so to get the ID value use the getRecord function available with Standard Controller.


public RetrieveTaskNewTesting(ApexPages.StandardController st){
   Job__c objJob = (Job__c) st.getRecord();
   Id jobId = objJob.id;




Hi Darshan,


         the problem is, am not getting id  properly from url. The proble is not with id, it is with url

ex: We have overriden 'view' standard button in  Job__c, on click of the record we are displaying page with url: apex/JobListView?id=a0Z90000007U9Ty

I think we are not getting the url properly, because if i change the url then only am getting exception, if i change the id the page just displaying invalid id.

Darshan FarswanDarshan Farswan

Ok. So did u try the code that I mentioned above. The page will work only whey you provide the valid Job__c ID to the page. And the Constructor use the code that I have mentioned above. Use the getRecord() function on st (standardController). And once you have the record, you can fetch the ID.

actually the problem is not with Id , it's with url problem i think.
Because when i remove the id from url , it is display as 'invalid id', when i change the url then am getting exception

Darshan FarswanDarshan Farswan

In both the cases, you will get error. Please see the code that I have mentioned and also try to use a valid ID. Also you are using paramU parameter. Make sure to pass that parameter as well. As for ID parameter, you will need a valid Job__c ID.