1

I'm having this problem. Every time I try to insert inputFields values to DB via Save button I either have this error

Attempted to upsert a null list Error is in expression '{!Save}' in component in page neworderpage: Class.NewOrderController.Save: line 23, column 1

OR

when I instantiate the List object in Save method, it is not inserted into DB.

VF Page (please ignore missing beginning and end):

<apex:form >
    <apex:dataList value="{!Opportunities}" var="o" style="list-style-type: none">
        <apex:pageBlock >
            <apex:pageBlockSection collapsible="false" title="Products (Assets)">
                <apex:inputField value="{!o.Order__r.Asset__r.Product2Id}" />
                <apex:inputField value="{!o.Order__r.Asset__r.Quantity}" /> 
                <apex:outputField value="{!o.Order__r.Asset__r.Price}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection collapsible="false" title="Order details">
                <apex:inputField value="{!o.Order__r.Payment__c}" />  
                <apex:inputField value="{!o.Order__r.Delivery__c}" />
                <apex:inputField value="{!o.Order__r.EffectiveDate}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection collapsible="false" title="Contact Info">
                <apex:inputField value="{!o.Order__r.Contact__r.OwnerId}" />     
                <apex:inputField value="{!o.Order__r.Contact__r.Email}" />
                <apex:inputField value="{!o.Order__r.Contact__r.MobilePhone}" />
                <apex:inputField value="{!o.Order__r.Contact__r.AssistantName}" />
                <apex:inputField value="{!o.Order__r.Contact__r.AssistantPhone}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection collapsible="false" title="Billing Address">
                <apex:inputText value="{!o.Order__r.BillingStreet}" />
                <apex:inputText value="{!o.Order__r.BillingCity}" />
                <apex:inputText value="{!o.Order__r.BillingPostalCode}" />
                <apex:inputText value="{!o.Order__r.BillingCountry}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection collapsible="false" title="Shipping Address">
                <apex:inputText value="{!o.Order__r.ShippingStreet}" />
                <apex:inputText value="{!o.Order__r.ShippingCity}" />
                <apex:inputText value="{!o.Order__r.ShippingPostalCode}" />
                <apex:inputText value="{!o.Order__r.ShippingCountry}" />
            </apex:pageBlockSection>
                <apex:commandButton action="{!Save}" title="Save" value="Save order" />
        </apex:pageBlock>
    </apex:dataList>
</apex:form>

And my controller:

    public class NewOrderController {

    public List<Opportunity> oppList;


    public List<Opportunity> getOpportunities() {
        List<Opportunity> oppList = new List<Opportunity>();

        oppList = [SELECT *some query entries*
                   FROM Opportunity
                   WHERE Order__c != null];
        return oppList;
    }

    public PageReference Save(){

        upsert oppList; 

        return new PageReference('/apex/MyOrders');
    } 
}

What am I missing? Dev console does not throw any error.

Thank you in advance for help. I am kinda new to apex/soql/visualforce.

1

You are facing a name shadow problem here.

You have oppList defined as a member variable as well as locally in the method getOpportunities(). So when you are accessing the list on the VF, you are not really populating the member variable. The member variable always remains null and thus you are getting the error in Save().

Just remove the declaration List<Opportunity> oppList = new List<Opportunity>(); from your getOpportunities() and use this instantiation at class level while declaring the variable and that should work.

| improve this answer | |
  • Thank you. It helped a bit but it threw another error: Insert failed. First exception on row 0 with id 0061t000004qNM4AAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error is in expression '{!Save}' in component in page neworderpage: Class.NewOrderController.Save: line 23, column 1 Any ideas? I have no clue. Partially. – user65177 Mar 4 '19 at 20:10
  • Seems you are only updating the records, if that's the case then use update instead of upsert. For how upsert works, take a look at this documentation for details. If you have follow up questions on upsert, then that will categorize as a new question based on the format of SFSE. – Jayant Das Mar 4 '19 at 20:21
  • Thank you, I will try to resolve it somehow. Thank you for your time. – user65177 Mar 4 '19 at 21:03
0

You've created a "shadow" variable, so the class-level variable remains null. You need to remove the data declaration from the method:

public List<Opportunity> getOpportunities() {
    // Uses the class-level variable
    oppList = [SELECT *some query entries*
               FROM Opportunity
               WHERE Order__c != null];
    return oppList;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy