0

I want update list of opportunities using visualforce page.In visualforce page I want to update Amount,close date and Stage.The visulaforce page should only show all open opportunities(STAGE NOT EQUALS TO CLOSED WON AND CLOSED LOST).

Anyone help me to fix this error and update only all open opportunities.

Here is my code:

Apex

public class UpdateListofOppty {
    public Opportunity opportunities {get;set;} 
    public Id recId{get;set;}
    public UpdateListofOpporunity(ApexPages.StandardSetController sc){
    recId = ApexPages.CurrentPage().getParameters().get('id');
    opportunities = [SELECT Name,Amount,StageName FROM opportunity WHERE Id = :recId AND StageName NOT IN ('Closed Lost', 'Closed Won')];
    }

    public Opportunity getOpptyDetail(){
        return opportunities;
    }

    public PageReference Save() {
        try{
        update(opportunities);
        }
        catch(System.DmlException e){
        ApexPages.addMessages(e);
        return null;
    }
        PageReference view = new ApexPages.StandardController(opportunities).view();
         return (view);
    }
}

VF Page

<apex:page standardController="Opportunity" extensions="UpdateListofOppty" recordSetVar="opportunities" tabStyle="Opportunity" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!opportunities}" var="opp">
                <apex:column value="{!opp.name}"/>
                <apex:column headerValue="Amount">
                    <apex:inputField value="{!opp.Amount}"/>
                </apex:column>
                <apex:column headerValue="Close Date">
                    <apex:inputField value="{!opp.CloseDate}"/>
                </apex:column>
                <apex:column headerValue="Stage">
                    <apex:inputField value="{!opp.stageName}"/>
                </apex:column>
            </apex:pageBlockTable>      
        </apex:pageBlock>
    </apex:form>
</apex:page>

ERROR:

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

Error shown in this line:

opportunities = [SELECT Name,Amount,StageName FROM opportunity WHERE Id = :recId AND StageName NOT IN ('Closed Lost', 'Closed Won')];
Caconde
  • 4,177
  • 7
  • 35
  • 32
Jane
  • 1
  • 1

1 Answers1

0

I suspect your id parameter is not passed through so it's a null. Querying Opportunities WHERE Id = null will always return zero results. Or maybe it's being passed but it's useless. If you have StandardSetController the Id value in the page URL might be used as filter id (listview), not an id of any particular record. Meaning query will still return 0 rows.

What were you trying to do? Maybe just removing that part of condition will help although this list can grow very fast. You might want to put a LIMIT statement in the query or read about StandardSetController and pagination.

eyescream
  • 18,088
  • 2
  • 34
  • 46