0
List has no rows for assignment to SObject Error is in expression '{!urlIICRedirectEdit}' in component <apex:page> in page agciic_edit: Class.AgriFiIICPageExtension.urlIICRedirectEdit: line 45, column 1

Below is the VF Page;

<apex:page sidebar="false" showHeader="true" standardController="AgriFI_IIC_Evaluation__c" extensions="AgriFiIICPageExtension" action="{!urlIICRedirectEdit}">
<apex:form >      
<html>
<body>
<p align="center"><strong>Redirect Page</strong><br/></p>
</body>
</html>
</apex:form>  
</apex:page>

Below is the Apex Class PageExtension

    public with sharing class AgriFiIICPageExtension {
    private ApexPages.StandardController standardController;
    private final AgriFI_IIC_Evaluation__c application;
    public AgriFiIICPageExtension(ApexPages.StandardController stdcontroller) {
        this.application = (AgriFI_IIC_Evaluation__c)stdController.getRecord();
        this.standardController = stdController;
     }
 public PageReference urlIICRedirect() {
    string selectedId = ApexPages.currentPage().getParameters().get('Id');
    string selectedRecordType ;
    selectedRecordType = [SELECT recordtypeid from AgriFI_IIC_Evaluation__c where Id = :selectedId LIMIT 1].recordtypeid;

    if (selectedRecordType == '0121v0000007JXYAA2'){ // 0121v0000007JXYAA2    Call1
    String currentApplication = '/apex/AgriFI_IIC_Call1_View?id=' + ApexPages.currentPage().getParameters().get('Id') + '&sfdc.override=1/'  ;
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;

    }else if (selectedRecordType == '0121v0000007JXdAAM'){ // 0121v0000007JXdAAM    Call2
    String currentApplication = '/apex/AgriFI_IIC_Call2_View?id=' + ApexPages.currentPage().getParameters().get('Id') + '&sfdc.override=1/'  ;
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;

    }else{
       String currentApplication = '/' + ApexPages.currentPage().getParameters().get('Id') + '?RT=' + selectedRecordType ;
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;
    }

}
public PageReference urlIICRedirectEdit() {
    string selectedId = ApexPages.currentPage().getParameters().get('Id');
    string selectedRecordType ;
    selectedRecordType = [SELECT recordtypeid from AgriFI_IIC_Evaluation__c where Id = :selectedId LIMIT 1].recordtypeid;

    if(selectedRecordType == '0121v0000007JXYAA2'){ // 0121v0000007JXYAA2   Call1
     String currentApplication = '/apex/AgriFI_IIC_Call1_Edit?id=' + ApexPages.currentPage().getParameters().get('Id') + '&sfdc.override=1/'  ;
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;
     }else if(selectedRecordType == '0121v0000007JXdAAM'){ // 0121v0000007JXdAAM    Call2
     String currentApplication = '/apex/AgriFI_IIC_Call2_Edit?id=' + ApexPages.currentPage().getParameters().get('Id') + '&sfdc.override=1/'  ;
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;

    }else{
        String currentApplication = '/' + ApexPages.currentPage().getParameters().get('Id') + '/e';
        PageReference currentPage = new PageReference(currentApplication);
        currentPage.getParameters().put('nooverride','1');
        return currentPage;
    }

}


}
1
  • See below VF Page; ' ' ' <apex:page sidebar="false" showHeader="true" standardController="AgriFI_IIC_Evaluation__c" extensions="AgriFiIICPageExtension" action="{!urlIICRedirectEdit}"> <apex:form > <html> <!-- extensions="AgriFiPageExtension" action="{!urlRedirectEdit}" --> <body> <p align="center"><strong>Redirect Page</strong><br/></p> </body> </html> </apex:form> </apex:page> ' ' '
    – Robert
    Apr 19, 2020 at 15:52

2 Answers 2

1

enter image description here

selectedRecordType = [SELECT recordtypeid from AgriFI_IIC_Evaluation__c where Id = :selectedId LIMIT 1].recordtypeid;

Are you sure you are getting data when you execute this SOQL in Developer Console?

I think the highlishted SOQL in above snapshot is getting Zero-Rows, and since you are trying to access [].recordtypeid , this is throwing exception.

To mitigate this, and handle exception properly, collect this result and do a null check and then access recordtypeid value

Also, refer to this link to get an idea on how to get record type Id dynamically instead of hard coding RecorsType IDs in code. https://www.davehelgerson.com/get-record-type-id-name-salesforce-object/

0

Edit your button to make its content source as "URL" and in the box below write down the URL in the following manner "/apex/AgriFiIICPage?Id={! AgriFI_IIC_Evaluation__c.Id}". This will populate variable selectedId with record id. Hence query will return a record.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .