function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Snehal Gaware 15Snehal Gaware 15 

not able to create record through vf page on object tab. getting error as "List has no rows for assignment to SObject ".

Hi i have created vf page and controller to override new button of custom object (Problem). So whenever i am able to create problem through case and it is working as expected. but when i am creating problem though it's tab i am getting error as : "List has no rows for assignment to SObject " . Please suggest.
 controller :
public class Populate
{
public Case cs{get;set;}
public SC_Problem_Management__c prblm{get;set;}
public ApexPages.StandardController stdCntrlr {get; set;}
    
public Populate(ApexPages.StandardController controller)
{
    cs = new Case();
    prblm = new SC_Problem_Management__c();
    prblm = (SC_Problem_Management__c)controller.getRecord();
    //Get the ID of Currently present case on vf page
    String csId = [SELECT Id FROM Case WHERE Id =: prblm.Case__c].Id;
    System.debug('testing'+ csId);
    prblm.Case__c= csId;
    if(csId != null)
    {
    //calling autofilling method
     autoCal();
    }
}

//function is called from actionsupport event

public void autoCal()
{
Id caseid = prblm.Case__c;     // collecting case id from visualforce page
List<Case> caseLst = [select id,Products__c,Componet__c ,Feature__c,Status,Priority,Subject,Description__c,Assignment_Group__c from Case where id=:caseid];
system.debug('Product'+ caseLst[0].Products__c);
    if(caseLst.isEmpty())
    {
      return;
    }    
prblm.Products__c= caseLst[0].Products__c;      
prblm.Component__c = caseLst[0].Componet__c; 
prblm.Feature__c = caseLst[0].Feature__c;
prblm.Status__c = caseLst[0].Status; 
prblm.Priority__c = caseLst[0].Priority; 
prblm.Title__c=caseLst[0].Subject;
prblm.Issue_Escape__c=caseLst[0].Assignment_Group__c;
prblm.Description__c= caseLst[0].Description__c;
  
}

public pagereference Save()
{
insert prblm;
pagereference pr = new pagereference('/'+prblm.id);                           
        return pr;     
      
} 
}

 
Snehal Gaware 15Snehal Gaware 15
Addon to previous : if problem is not having case related to it, it should only display vf page so that user can add case later on.
GovindarajGovindaraj
Hi Snehal,

Better create a seperate button with this VF page then include that in the related list and keep the new button (in the Problecm object tab) as such.

Thanks,
Govindaraj.S
Snehal Gaware 15Snehal Gaware 15
Hi Govindaraj, I am able to achieve what i want, but now the issue is when i click on button of related list it is not capturing case ID. because of which again i am getting same error of "List has no rows for assignment to SObject".

Could you please suggest
Raj VakatiRaj Vakati
Try this code
 
public class Populate
{
public Case cs{get;set;}
public SC_Problem_Management__c prblm{get;set;}
public ApexPages.StandardController stdCntrlr {get; set;}
    
public Populate(ApexPages.StandardController controller)
{
    cs = new Case();
    prblm = new SC_Problem_Management__c();
    prblm = (SC_Problem_Management__c)controller.getRecord();
    //Get the ID of Currently present case on vf page
    String csId = [SELECT Id FROM Case WHERE Id =: prblm.Case__c].Id;
    System.debug('testing'+ csId);
    prblm.Case__c= csId;
    if(csId != null)
    {
    //calling autofilling method
     autoCal();
    }
}

//function is called from actionsupport event

public void autoCal()
{
Id caseid = prblm.Case__c;     // collecting case id from visualforce page
if(caseid!=null){
List<Case> caseLst = [select id,Products__c,Componet__c ,Feature__c,Status,Priority,Subject,Description__c,Assignment_Group__c from Case where id=:caseid];
system.debug('Product'+ caseLst[0].Products__c);
    if(!caseLst.isEmpty())
    {
        
prblm.Products__c= caseLst[0].Products__c;      
prblm.Component__c = caseLst[0].Componet__c; 
prblm.Feature__c = caseLst[0].Feature__c;
prblm.Status__c = caseLst[0].Status; 
prblm.Priority__c = caseLst[0].Priority; 
prblm.Title__c=caseLst[0].Subject;
prblm.Issue_Escape__c=caseLst[0].Assignment_Group__c;
prblm.Description__c= caseLst[0].Description__c;
	}
}
}

public pagereference Save()
{
insert prblm;
pagereference pr = new pagereference('/'+prblm.id);                           
        return pr;     
      
} 
}