1

Can anyone please look into this and help me out

Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

here is my code :

public Class SelectProductController{

public string Product2Id{get;set;}
Public String OppName{get;set;}
public Opportunity opp{get;set;}
public OpportunityLineItem oli{get;set;}
public Integer prodCount = 0;
public String OppId{get;set;}

public PricebookEntry pdata{get;set;}
public String oppgetname{get;set;}

public String unitprice{get;set;}

public productWrapper[] productList {
get{
    if(productList == null){
        productList = New ProductWrapper[]{};

        for(Product2 p : [select Id, Name, Manufacturer__c,Description from Product2 where IsActive= true order by Manufacturer__c asc ]){
            productList.add(New productWrapper(p,Product2Id));
        }
    }

    return productList;
}
set;
}

public SelectProductController(ApexPages.StandardController Controller){        
    Product2Id = ApexPages.currentPage().getParameters().get('Product2Id');

    opp = new Opportunity();
    OppId = Apexpages.currentPage().getParameters().get('Id');
    opp.LID__LinkedIn_Company_Id__c = Apexpages.currentPage().getParameters().get('oppLID');
    //opp.IsPrivate = Boolean.ValueOf(Apexpages.currentPage().getParameters().get('oppIsPrivate'));
    opp.Name = Apexpages.currentPage().getParameters().get('oppName');
    opp.Accountid = Apexpages.currentPage().getParameters().get('oppAccountid');
    opp.CloseDate = date.parse(Apexpages.currentPage().getParameters().get('oppCloseDate'));
    opp.PrimaryTechnologyArchitect__c = Apexpages.currentPage().getParameters().get('oppPrimaryTech');
    opp.StageName = Apexpages.currentPage().getParameters().get('oppStageName');

    opp.Probability = Decimal.valueOf(Apexpages.currentPage().getParameters().get('oppProbability'));
    opp.Type = Apexpages.currentPage().getParameters().get('oppType');
    opp.WinLoss__c = Apexpages.currentPage().getParameters().get('oppWinLoss__c');
    opp.LeadSource = Apexpages.currentPage().getParameters().get('oppLeadSource');
    opp.Assessment__c = Apexpages.currentPage().getParameters().get('oppAssessment__c');

    opp.Budget__c = Apexpages.currentPage().getParameters().get('oppBudget');
    opp.ContractNumber__c = Apexpages.currentPage().getParameters().get('oppContractNumber');
    opp.Competition__c = Apexpages.currentPage().getParameters().get('oppCompetition');
    opp.Competition2__c = Apexpages.currentPage().getParameters().get('oppCompetition2');
    opp.Competition3__c = Apexpages.currentPage().getParameters().get('oppCompetition3');
    opp.SOW_Nbr__c = Apexpages.currentPage().getParameters().get('oppSOW');
    opp.BIG_Sow_Number__c = Apexpages.currentPage().getParameters().get('oppSowNumber');


    oli = new OpportunityLineItem();
}

public Pagereference save(){

     unitprice = Apexpages.currentPage().getParameters().get('unitprice');

     Product2 tbu = New Product2();


    for(productWrapper p : productlist){

        if(p.isSelected == True){
        prodCount ++;
        }
    }
    if(OppId == Null){
        if(prodCount > 0){
            Insert opp;
        }else{
            apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please select atleast one product.'));
            return new Pagereference('/apex/OppvsProductnew');
        }
    }else{
        update opp;
    }

    for(productWrapper p : productlist){

        if(p.isSelected == True){



            pdata = [select Id from PricebookEntry where Product2Id =: p.prod.id];

                tbu = p.prod;
                oli = new OpportunityLineItem();
                oli.PriceBookEntryId = pdata.id;
                oli.OpportunityId = opp.Id;    
                oli.Quantity = 1;
                //oli.TotalPrice = 100;
                oli.UnitPrice = p.price;
                oli.Description = p.Description;
                insert oli;
        }
    }
    return new Pagereference('/'+Opp.Id);

}


public class productWrapper{
    public Product2 prod {get;set;}
    public boolean isSelected {get;set;}
    public integer price{get;set;}
    public String Description{get;set;}

    public productWrapper(Product2 prod, Id Product2Id){
        this.prod = prod;
        isSelected = prod.Id == Product2Id; 
    }
}
}
| improve this question | | | | |
  • 2
    From where you getting values to opp? – Reshma Mar 17 '17 at 11:43
  • from page side (Let me put my full code here so it would be better to understand) – WantToBeACoder Mar 17 '17 at 12:07
  • @Reshma please see my updated code – WantToBeACoder Mar 17 '17 at 12:10
1

You never actually set the Id on the record, you just store it in a separate property. Instead of:

oppId = Apexpages.currentPage().getParameters().get('Id');

Use:

opp.Id = Apexpages.currentPage().getParameters().get('Id');

Just remove the oppId variable from your page entirely and replace it everywhere with opp.Id.


You should also get in the habit of never putting DML in a for loop. Right now you have a loop where every iteration calls insert oli;. It is dead simple to move this logic out of the loop, and inexcusable to consume governor limits so wastefully. The basic idea is always:

List<OpportunityLineItem> lineItems = new List<OpportunityLineItem>();
for (...)
{
    lineItems.add(new OpportunityLineItem(...));
}
insert lineItems;

Also worth noting that SObject construction is much faster with key=value pairs:

opp = new Opportunity(
    Id = ApexPages.currentPage().getParameters.get('Id'),
    Name = Apexpages.currentPage().getParameters().get('oppName')
    // etc
);
| improve this answer | | | | |
  • thanks Adrian, i tried your way and you are absolutely correct but when i am updating the opp and inserting opportunityLine item, it always insert a new Opportunity also..i just want to update opportunity with new lineitem, could you please help me on this ? – WantToBeACoder Mar 17 '17 at 12:52
2

In your constructor method, you have the following line:

OppId = Apexpages.currentPage().getParameters().get('Id');

The remainder of your lines are used to define the fields in

opp = new Opportunity();

As a consequence, you're not defining opp.Id. That's the source of your issue. You need to assign the value of OppId to opp.Id.

| improve this answer | | | | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.