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
Kiru535Kiru535 

List has no recors associtaed to SOBJECT

I am invoking VF page by clicking on new button. But I am getting error like 'List has no recors associtaed to SOBJECT'

 

VF page contains multiselect picklist. these values getting from another object.

My requirement is I have one lookup to some other object. Whenever the  lookup field is selected in VF page the picklist must be updated with the selected values on another object.

 

Apex class :

public class CreateEditIBXLines
{
    public IBX_Lines__c ibxLine {get;set;}
    ApexPages.StandardController controller {get;set;}
        
    public SelectOption[] selectedCompetitors { get; set; }
    public SelectOption[] allCompetitors { get; set; }
             
    public CreateEditIBXLines(ApexPages.Standardcontroller controller)
    {
        this.controller = controller;
        ibxLine=(IBX_Lines__c)controller.getRecord();
        this.selectedCompetitors = new List<SelectOption>();
        this.allCompetitors = new List<SelectOption>();
        
        // check if there are any selected competitors already
        if(controller.getId() != null)
        {
            this.ibxLine = [SELECT Id,IBX_Name__c,Competitors__c FROM IBX_Lines__c WHERE Id =: controller.getId()];
        }
        
        // adding the selected competitors to the selected list
        if(this.ibxLine != null && this.ibxLine.Competitors__c != null)
        {
            for(String s : this.ibxLine.Competitors__c.split(';'))
            {
                selectedCompetitors.add(new SelectOption(s,s));
            }           
        }
        
        // finding out all the competitors available for selected IBX on IBX lines
        IBX__c ibxInfo = [SELECT Id,Name,Competitors__c FROM IBX__c where Id =:ibxLine.IBX_Name__c];
        
        if(ibxInfo != null && ibxInfo.Competitors__c != null)
        {
            for(String s : ibxInfo.Competitors__c.split(';'))
            {
                // logic to not add the already selected competitors to available list
                if(ibxLine != null && ibxLine.Competitors__c != null && ibxLine.Competitors__c.contains(s))
                {
                    // do not add to the list                   
                }
                else
                {
                    allCompetitors.add(new SelectOption(s,s));
                }
            }   
        }
    }

    public PageReference save()
    {
        String toPopulate = null;
        for(SelectOption selected: selectedCompetitors)
        {
            if(toPopulate == null)
              toPopulate = selected.getValue();
            else
            {
                toPopulate = toPopulate + ';' + selected.getValue();
            }
        }
        this.ibxLine.Competitors__c = toPopulate;
        
        update ibxLine;
        
        return this.controller.view();
    }
}

 

VF page:

<apex:page standardController="IBX_Lines__c" extensions="CreateEditIBXLines">
    <apex:sectionHeader title="IBX Lines Edit" subtitle="{!IBX_Lines__c.name}"/>
    <apex:form >
        <apex:pageBlock mode="edit" id="pgBlockId">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Save & New" action="{!save}" />
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Information" columns="2">
                <apex:inputField value="{!IBX_Lines__c.Name}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.CurrencyIsoCode}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Opportunity__c}" required="true"/>
                <apex:inputField value="{!IBX_Lines__c.Primary_Campaign_Source__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.IBX_Name__c}" required="true">
                 <apex:inputField/>
                <apex:inputField value="{!IBX_Lines__c.Lead_Source__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Line_Type__c}" required="true"/>
                <apex:inputField value="{!IBX_Lines__c.Cross_Region_Lead_Source__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Adjustment_Type__c}" required="false"/>
            </apex:pageBlockSection>
             
            <apex:pageBlockSection title="Status" columns="2">
                <apex:inputField value="{!IBX_Lines__c.Forecast_Status__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Closed_Won_Lost_Notes__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Lost_Reason__c}" required="false"/>
            </apex:pageBlockSection>
             
            <apex:pageBlockSection title="MRR/NRR" columns="2">
                <apex:inputField value="{!IBX_Lines__c.Forecast_MRR__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Forecast_NRR__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Booked_Gross_MRR__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Booked_Gross_NRR__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Booked_Net_MRR__c}" required="false"/>
                <apex:inputField value="{!IBX_Lines__c.Booked_Net_NRR__c}" required="false"/>
            </apex:pageBlockSection>
             
            <apex:pageBlockSection title="Competitors" columns="2">
                <!--  
                <apex:inputField value="{!IBX_Lines__c.Competitors__c}" required="false"/>
                -->
                <c:MultiselectPicklist leftLabel="Available Competitors"
                                       leftOptionsName="{!allCompetitors}"
                                       rightLabel="Selected Competitors"
                                       rightOptionsName="{!selectedCompetitors}"
                                       size="14"
                                       width="150px"/>
            </apex:pageBlockSection>
             
            <apex:pageBlockSection title="Comments" columns="2">
                <apex:inputField value="{!IBX_Lines__c.Comments__c}" required="false"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Please help me ASAP

 

Yoganand GadekarYoganand Gadekar

Figure out the line number and check the size of the list, it may not have any records in it...

 

Thanks

Kiru535Kiru535

I dnt know why this error is coming... I not figure out any line also...

Please help me

Avidev9Avidev9

Well this error crops up when the query doesnt return any records and you are catch the records in a single sobject rather than a list of sobjects.

made some changes have a look

public class CreateEditIBXLines {
    public IBX_Lines__c ibxLine {
        get;
        set;
    }
    ApexPages.StandardController controller {
        get;
        set;
    }

    public SelectOption[] selectedCompetitors {
        get;
        set;
    }
    public SelectOption[] allCompetitors {
        get;
        set;
    }

    public CreateEditIBXLines(ApexPages.Standardcontroller controller) {
        this.controller = controller;
        ibxLine = (IBX_Lines__c) controller.getRecord();
        this.selectedCompetitors = new List < SelectOption > ();
        this.allCompetitors = new List < SelectOption > ();

        // check if there are any selected competitors already
        if (controller.getId() != null) {
            this.ibxLine = [SELECT Id, IBX_Name__c, Competitors__c FROM IBX_Lines__c WHERE Id = : controller.getId()];
        }

        // adding the selected competitors to the selected list
        if (this.ibxLine != null && this.ibxLine.Competitors__c != null) {
            for (String s: this.ibxLine.Competitors__c.split(';')) {
                selectedCompetitors.add(new SelectOption(s, s));
            }
        }

        // finding out all the competitors available for selected IBX on IBX lines
//Error was coming from this line I guess List<IBX__c> ibxInfos = [SELECT Id, Name, Competitors__c FROM IBX__c where Id = : ibxLine.IBX_Name__c]; if (!ibxInfos.isEmpty() && ibxInfos[0].Competitors__c != null) { for (String s: ibxInfos[0].Competitors__c.split(';')) { // logic to not add the already selected competitors to available list if (ibxLine != null && ibxLine.Competitors__c != null && ibxLine.Competitors__c.contains(s)) { // do not add to the list } else { allCompetitors.add(new SelectOption(s, s)); } } } } public PageReference save() { String toPopulate = null; for (SelectOption selected: selectedCompetitors) { if (toPopulate == null) toPopulate = selected.getValue(); else { toPopulate = toPopulate + ';' + selected.getValue(); } } this.ibxLine.Competitors__c = toPopulate; update ibxLine; return this.controller.view(); } }

 

asish1989asish1989

HI

I think error must be from this line...

IBX__c ibxInfo = [SELECT Id,Name,Competitors__c FROM IBX__c where Id =:ibxLine.IBX_Name__c];

 

make sure that IBX__c contains some valid records which matchs to where conditions.

 

Did this post answers your questions, if so please accept as solutions and give kudos

 

Thanks

Kiru535Kiru535

I getting error like List out of bound Index 0.

 

Please help me

Avidev9Avidev9
Can you post your current code ?
Seems like you missed something
Avidev9Avidev9

Have a look at the statement

if (!ibxInfos.isEmpty() && ibxInfos[0].Competitors__c != null)

The first one is not empty check

!ibxInfos.isEmpty()  //Make sure you added the exclamatory "!" sign

 

anded with

 

ibxInfos[0].Competitors__c != null

 

Kiru535Kiru535

 

Thanks for ur solution.Working fine .. I am bale to open the VF page by clicking on new. But the problem is  when we click on save, the page was not going going to read only mode. 

 

Please let me know what is the problem?

 

 

Avidev9Avidev9
Because you never coded that way.
You have to replace inputField with outField.

Or you can redirect the user to standard detail page.