+ Start a Discussion
TheLearnerTheLearner 

List has no rows for assignment to SObject

Hi expert,

Im getting list has no rows for assignment to sobject error while creating triio project closure session records directly.


public with sharing class tRIIO_NewProjectClosureSession_Exten {
    public static tRIIO_Activity__c activity;
    public static tRIIO_Works_Order__c worksOrder;
    private static String actID;
    public tRIIO_Project_Closure_Session__c PCS {get;set;}
    static{
             String fullURL = ApexPages.currentPage().getUrl();
             String[] reqURL = fullURL.split('&');
             String[] reqId = reqURL[1].split('=');
             System.debug('@#@# URLs Full URL = '+ fullURL+'  Split URL = '+reqURL[1]+'  reqID = '+ reqId[1]);
             if([SELECT count() FROM tRIIO_Activity__c WHERE Id =:reqId[1]] == 1)
                activity = [SELECT id, Name,Works_Order__c from tRIIO_Activity__c Where id=:reqId[1]];
             else
                worksOrder = [SELECT id,Name from tRIIO_Works_Order__c where id=:reqId[1]];
    }
    public tRIIO_NewProjectClosureSession_Exten(ApexPages.StandardController controller) {
            PCS = (tRIIO_Project_Closure_Session__c)controller.getRecord();
          try{
            if(activity != null){
                PCS.tRIIO_Works_Order__c = activity.Works_Order__c;
                PCS.Activity_Ref__c = activity.id;
            }else if(worksOrder != null)
              PCS.tRIIO_Works_Order__c = worksOrder.id;
          }catch(Exception e){}
    }
}

Thanks in advance 
Best Answer chosen by TheLearner
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
public with sharing class tRIIO_NewProjectClosureSession_Exten 
{
    public static tRIIO_Activity__c activity;
	public static List<tRIIO_Activity__c> listActivity;
    public static tRIIO_Works_Order__c worksOrder;
	public static List<tRIIO_Works_Order__c> lstWorksOrder;
    private static String actID;
    public tRIIO_Project_Closure_Session__c PCS {get;set;}
    static
	{
             String fullURL = ApexPages.currentPage().getUrl();
             String[] reqURL = fullURL.split('&');
             String[] reqId = reqURL[1].split('=');
             System.debug('@#@# URLs Full URL = '+ fullURL+'  Split URL = '+reqURL[1]+'  reqID = '+ reqId[1]);
             if([SELECT count() FROM tRIIO_Activity__c WHERE Id =:reqId[1]] == 1)
                listActivity = [SELECT id, Name,Works_Order__c from tRIIO_Activity__c Where id=:reqId[1]];
             else
                lstWorksOrder = [SELECT id,Name from tRIIO_Works_Order__c where id=:reqId[1]];
    }
    public tRIIO_NewProjectClosureSession_Exten(ApexPages.StandardController controller) {
            PCS = (tRIIO_Project_Closure_Session__c)controller.getRecord();
          try{
            if(listActivity.size() > 0 )
			{
                PCS.tRIIO_Works_Order__c = listActivity[0].Works_Order__c;
                PCS.Activity_Ref__c = listActivity[0].id;
            }
			else if(lstWorksOrder.size() > 0 )
			{
              PCS.tRIIO_Works_Order__c = lstWorksOrder[0].id;
			}  
          }catch(Exception e){}
    }
}
NOTE:- Always try to use List for query so that this issue will not come.

Please let us know if this will help you.

Thanks
Amit Chaudhary

All Answers

NagaNaga (Salesforce Developers) 
Hi TheLearner,


Description
Issue:

Following query is not returning any number of records:

"[SELECT Id FROM Account WHERE Id = :Trigger.new[0].Account__c]"




Resolution
This error occurs when query doesn't return any rows.
 
While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned.
What’s not obvious is that it also assumes that exactly one row is returned!
 
While this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create,
especially when a WHERE statement is used that might return zero rows, such as:
 
Player__c player = [SELECT Id from Player__c where Name = :username];
if (player != null)
 p = player.Id;
 
The above code will fail if there is no Player__c record with the matching username. It doesn't actually return a null.
 
It would be safer to do the following:
 
Player__c[] players = [SELECT Id from Player__c where Name = :username];
if (players.size() > 0)
p = players[0].Id;
 
It’s one of those situations for which you would not normally think of creating a test, so it’s safer to just avoid the possibility.


Please let me know if this helps

Best Regards
Naga Kiran
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
public with sharing class tRIIO_NewProjectClosureSession_Exten 
{
    public static tRIIO_Activity__c activity;
	public static List<tRIIO_Activity__c> listActivity;
    public static tRIIO_Works_Order__c worksOrder;
	public static List<tRIIO_Works_Order__c> lstWorksOrder;
    private static String actID;
    public tRIIO_Project_Closure_Session__c PCS {get;set;}
    static
	{
             String fullURL = ApexPages.currentPage().getUrl();
             String[] reqURL = fullURL.split('&');
             String[] reqId = reqURL[1].split('=');
             System.debug('@#@# URLs Full URL = '+ fullURL+'  Split URL = '+reqURL[1]+'  reqID = '+ reqId[1]);
             if([SELECT count() FROM tRIIO_Activity__c WHERE Id =:reqId[1]] == 1)
                listActivity = [SELECT id, Name,Works_Order__c from tRIIO_Activity__c Where id=:reqId[1]];
             else
                lstWorksOrder = [SELECT id,Name from tRIIO_Works_Order__c where id=:reqId[1]];
    }
    public tRIIO_NewProjectClosureSession_Exten(ApexPages.StandardController controller) {
            PCS = (tRIIO_Project_Closure_Session__c)controller.getRecord();
          try{
            if(listActivity.size() > 0 )
			{
                PCS.tRIIO_Works_Order__c = listActivity[0].Works_Order__c;
                PCS.Activity_Ref__c = listActivity[0].id;
            }
			else if(lstWorksOrder.size() > 0 )
			{
              PCS.tRIIO_Works_Order__c = lstWorksOrder[0].id;
			}  
          }catch(Exception e){}
    }
}
NOTE:- Always try to use List for query so that this issue will not come.

Please let us know if this will help you.

Thanks
Amit Chaudhary
This was selected as the best answer
TheLearnerTheLearner
HI Amit,

Its really working fine thanks for the code.
TheLearnerTheLearner
HI Amit,

Could you write test class for this code please. this is the flow 
 trillo global params is the parent of all the remainging object what ever i wrote below.


tRIIO_Global_Params__c(parent)---tRIIO_Contract__c(child )---tRIIO_Project__c(child)--tRIIO_Works_Order__c(child)---tRIIO_Activity__c(child)--tRIIO_Project_Closure_Session__c