2

On button click on opportunity... I am showing opportunityline items records on a page with line number using repeat.

I have used wrapper class for showing line number.

My Problem

Opportunityline items fields are editable on page..so user can update them...i am having problem updating it...How to specify id for updating...

in Constructor

List<OpportunityLineItem> oli;
oli=[Select Id,opportunityId From OpportunityLineItem where opportunityId=:id];
showsavedoppLineitems();

wrapper class

public list<linenumberclass> wrapperopllist;//List used in repeat on page
public Class linenumberclass{
public integer lineNumber{get;set;}
public OpportunityLineItem oppLineNo{get;set;}



 public linenumberclass(OpportunityLineItem oppLineNo,Integer lineNumber){
    this.oppLineNo=oppLineNo;
    this.lineNumber=lineNumber;
  }

}

method called from constructor

public void showsavedoppLineitems(){   

List<OpportunityLineItem> lOpp=[select   id,Business_Type__c,pricebookentry.pricebook2id,pricebookentryid from OpportunityLineItem where id in:opplineIttems]; 

  for(OpportunityLineItem l:lOpp){    
     opline = new OpportunityLineItem ();
     opline.OpportunityId=id;
     opline.pricebookentry=pre.get(l.pricebookentryid);
     opline.pricebookentryid=l.pricebookentryid;
     opline.Territory1__c=l.Territory1__c;
     wrapperopllist.add(new linenumberclass(opline,m));
      m++;
  }
}

i am calling this method on button click...so i have the updated record value in oplist...

public list<OpportunityLineItem> opllist {get;set;}

public void add {

    for(linenumberclass ln:wrapperopllist) {
        opline = new OpportunityLineItem ();
        opline.opportunityid=id;
        opline.PricebookEntry=ln.oppLineNo.pricebookentry;
        opline.PricebookEntryid=ln.oppLineNo.pricebookentryid;
        opline.Line__c=ln.linenumber;
        opllist.add(opline);
    }

}

i dont know how to update oplist....

As i am showing the records in editable mode...if any user edits them and click on them records should be updated .

i dont know how to update existing records.i dont how to pass id.

  • Please format the code properly while presenting long codes .Also could you please see once again that you have not missed anything while pasting your code .Also please avoid any DML inside constructor . – Mohith Shrivastava Nov 11 '12 at 5:31
  • Please reformat your question, either by breaking it to several general questions, or be more specific. The way it is shown now, it's too localized, and I feel you will not get the answer you are looking for. – Saariko Nov 11 '12 at 6:33
2

Your issue is that you are not retaining the queried OpportunityLineItem records, which contain the Id needed to update them later.

Instead you appear to be creating for each item in your wrapped list a brand new OpportunityLineItem record and passing that into your wrapper. These do not have an Id (nor can you 'easily' be assign one) by default. Typically you only create new objects if you intend to perform inserts.

So my suggestion is to simply pass 'l' into your 'linenumberclass' constructor. This also saves you having to copy across field values.

for(OpportunityLineItem l:lOpp){    
    wrapperopllist.add(new linenumberclass(l,m));
    m++;
}

Also when updating make sure once again to use the instance of the record you originally queried to populate your list of OpportunityLineItem's to update.

for(linenumberclass ln:wrapperopllist){
    opllist.add(ln.oppLineNo);
}

Then...

update opllist;

UPDATE: In your original code you are utilising the 'pre' variable when creating OpportunityLineItem. Which as you say in the comments is a map of PricebookEntries. You can often obtain related information by using the relationships between the objects in your queries and save yourself code and SOQL requests. For example to retrieve the Product name, add 'pricebookentry.product2.name' like so...

List<OpportunityLineItem> lOpp=
    [select id,
       Business_Type__c, 
       pricebookentry.product2.name,
       pricebookentry.pricebook2id,
       pricebookentryid 
     from OpportunityLineItem 
     where id in:opplineIttems]; 

Should work, hope this helps.

| improve this answer | |
  • thanks andrew for replying....but i have a problem...in my showsavedoppLineitems() method...i am using Map<id,Pricebookentry> pre=new Map<id,Pricebookentry>();.. opline.pricebookentry=pre.get(l.pricebookentryid); i am using this to show on page...i dont know how to show this..if i use your way....onpage oppLineNo.PricebookEntry.Product2.name – Mik Nov 11 '12 at 15:25
  • I've updated my answer hope this clears it up for you! – Andrew Fawcett Nov 11 '12 at 15:39
  • how can i vote any Q up...like i want to vote up your answer.... also can can you help me this Question ... i am new to salesforce...salesforce.stackexchange.com/questions/4267/… – Mik Nov 11 '12 at 17:20
  • You just press the up arrow on the question (if its not yours) and also importantly any answers you receive! ;-) Also be sure to tick answers you've received if they have obviously answered your question. I'll take a look at your other question now... – Andrew Fawcett Nov 11 '12 at 17:21
  • Also take a look at this to know how to best contribute and ask questions on this site. meta.salesforce.stackexchange.com/faq – Andrew Fawcett Nov 11 '12 at 17:23
0

If i understand correctly you are trying to update the record after user interaction (after edit) and your problem is you have a wrapper list.

for(linenumberclass ln:wrapperopllist){
   opline = new OpportunityLineItem ();
   **opline.opportunityid=ln.oppLineNo.opportunityid**;//added the proper code here
   opline.PricebookEntry=ln.oppLineNo.pricebookentry;
   opline.PricebookEntryid=ln.oppLineNo.pricebookentryid;
   opline.Line__c=ln.linenumber;
   opllist.add(opline);
}
   update opllist;

Also please dont make any DML calls inside the constructor .

Note please query the opportunityId field too else you will run into exception saying sobject row was retrieved without querying the field .

| improve this answer | |
  • yes my problem is with wrapper class... i dont know how to add id to wrapper class...showsavedoppLineitems()..see this method.. – Mik Nov 11 '12 at 7:11
  • Did you observed i changed a little bit of your code .Please see the answer . – Mohith Shrivastava Nov 11 '12 at 10:15

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.