+ Start a Discussion
John NeilanJohn Neilan 

VF Controller Error

Hello,

I have a controller below on a VF page off the Opportunity object.  All I am trying to do is pre-populate a custom field (RENEW_Date_Updated__c) with today's date.  When I try to save my VF page, I get the error below.  Does anyone know how I can fix this?  Thanks.

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!save}' in component <apex:commandButton> in page renewprogupdate: Class.RenewalOppProg.save: line 18, column 1
Class.RenewalOppProg.save: line 18, column 1

 
public class RenewalOppProg{

public List<Opportunity> opps {get; set;}
    private final Opportunity oppty;
    public RenewalOppProg(ApexPages.StandardController myController) {
        opps = new List<Opportunity>();
        oppty = (Opportunity)myController.getrecord();
       }

public Opportunity opp2 = new Opportunity();
    public void OppProg(){
        opp2.RENEW_Date_Updated__c = Date.Today();
        opps.add(opp2);
    }

    public PageReference save() {
    
        update opps;
        {
        PageReference RetPage = new PageReference('/apex/RenewProgView?id=' + opps[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
        }
        }

}


 
Best Answer chosen by John Neilan
John NeilanJohn Neilan
For anyone interested, below is my working controller:
 
public class RenewalOppProg{
    private final Opportunity oppty;

        public RenewalOppProg(ApexPages.StandardController myController) {
 
            oppty=(opportunity)myController.getRecord();
                oppty.RP_Date_Updated__c = Date.Today();
                oppty.RP_Edit__c = TRUE;
        }

    public PageReference save() {

    try{

        update oppty;
        return new PageReference('/apex/RenewProgView?id=' + oppty.id);
    }
    catch(Exception e){
    }
    return null;
    }
}

 

All Answers

lifewithryanlifewithryan
It looks like you are adding a new opportunity (opp2) yes? If its a brand new opportunity (and if it will be everytime) try calling insert instead of update. If its possible you'd be editing an existing opp (or possibly doing both if adding multiple ones) try calling upsert instead of update.  The error is telling you you are calling "update" on an opportunity that doesn't exist yet, so it doesn't have the ID and therefore doesn't know what opportunity its updating.  Using upsert will insert the opp if need be, or update if its already existing. Using insert will just insert the opportunity.

Hope that helps.
John NeilanJohn Neilan
Thanks.  This is only meant to update the Opportunity from which the page is generated.  Is my controller uncecessarily complicated to do this?
lifewithryanlifewithryan
If all you are doing is updating the existing opportunity I would just do something like this:
public class OppRenewController {
    public Opportunity opp {get; set;}

    public OppRenewController(ApexPages.StandardController myController) {
        this.opp = myController.getRecord();
        //if you are instead passing in an opp ID, lookup the opp
        //you can even check if opp is null and then look for the ID on the params as well
        //may want to wrap in try/catch in case nothing is found/passed in, etc.
       String oppId =  ApexPages.CurrentPage().getParameters().get('oppid');
        this.opp = [SELECT ID, RENEW_Date_Updated__c FROM Opportunity WHERE ID = :oppId];
    }

    public pageReference save() {
        //do custom save stuff
        this.opp.RENEW_Date_Updated__c = Date.Today();
        update this.opp;
   }
}

Naturally you'll want to wrap the necessary try/catch around the opportunity ID part to ensure you have an ID, etc. but this should get you the gist...
There may be errros in this code as this is off the top of my head and I don't have access to my editor at the moment :)
John NeilanJohn Neilan
Thanks Ryan!  I'm new to custom controllers.  There are errors, but I'll see what I can do.  Thanks! 
lifewithryanlifewithryan
The main issue you had was that you were creating a "new opportunity" on line 10 of your code instead of using the existing opportunity.  I would also investigate whether or not you need a custom page at all. Not knowing the business logic around how/when/why this field gets updated, along with other things, you may be able to accomplish this same task without any code and instead use a workflow rule with a field update, etc.  If perhaps the value of some field changing is what signals the user to update this date, you could get it done with a workflow rule.  But, again...I don't know the business rules surrounding this, so I'm assuming you know whether or not you need custom code for this portion. (Also, my save method I just noticed isn't returning a page reference...you'll want to add something like return new PageReference('/' + this.opp.Id);
John NeilanJohn Neilan
For anyone interested, below is my working controller:
 
public class RenewalOppProg{
    private final Opportunity oppty;

        public RenewalOppProg(ApexPages.StandardController myController) {
 
            oppty=(opportunity)myController.getRecord();
                oppty.RP_Date_Updated__c = Date.Today();
                oppty.RP_Edit__c = TRUE;
        }

    public PageReference save() {

    try{

        update oppty;
        return new PageReference('/apex/RenewProgView?id=' + oppty.id);
    }
    catch(Exception e){
    }
    return null;
    }
}

 
This was selected as the best answer