1

i have a business requirement to validate opportunitylintem while inserting by a user. if it does not meet criteria,it should not add to opportunity,and go to approval process. once it approved i need to link it back to opportunity. This is my requirement. So i designed a shadow object for opportunity line item and my trigger insert all unsatisfied records to shadow object and later delete those records from opportunityline item. once a record in shadow is approved, i need insert back to exact opportunity from which i deleted.

My issue here is while i am inserting opportunityline item ,i need to provide productname. if i am moving productid of shadow object, i am receiving an error 'product' is not writable.Then i started solve this issue with Pricebookentryid. But i unable to find where this filed exists,because my trigger was already deleted actual opportunityline item as soon as it get insert.Please can anybody provide some solution to insert lineitem to opportunity? It would be really appreciated.

         if (Trigger.isAfter){
       //   inserting record in shadow object

                linelist.add(op);

                  List<PriceBookEntry> Pe = new list<PriceBookEntry>();

          List<lockproduct__c> loc = new list<lockproduct__c>();
          lockproduct__c lp = new lockproduct__c();
          lp.Name = pt.name;
          lp.codebases__c = op.code_bases__c;
          lp.listprice__c = op.ListPrice; 
          lp.Opportunity__c = op.opportunityid;
          lp.Product__c    = pt.id; 
          lp.salesprice__c = op.UnitPrice;
          lp.TrainingSKU__c = op.Training_SKU__c;
          lp.unit_quantity__c =  op.Unit_Quantity__c;
          lp.approval_process__c = true;
          lp.lockrecord__c  = true;

          loc.add(lp);
                 try{
                insert loc;
                //op.Next_Step__c ='delete record'; 
            } catch(system.dmlexception e) {
                       system.debug(e);
           }

   /*
    // deleting opportunity product record
    List<OpportunityLineItem> delprod = new List<OpportunityLineItem>();
       for(opportunitylineitem oli:[select id from OpportunityLineItem where id in : trigger.new])
      for(opportunitylineitem oli:[select id from OpportunityLineItem where id in : linelist])
     {

         delprod.add(oli);

     }
     */
       List<OpportunityLineItem> delprod = new List<OpportunityLineItem>();
       for(opportunitylineitem oli:[select id from OpportunityLineItem where id in : linelist ])
       {
       delprod.add(oli);
       }
        system.debug('size of delete list:' +delprod.size());
      if(delprod.size()!=0)
      {
        database.DeleteResult[] delprod_del = database.delete(delprod);
          for(database.DeleteResult del : delprod_del)
            { if(del.isSuccess())
                {
                    system.debug('record deleted success:' +del.getid());

                }
                else
                {
                    system.debug('record deleted failure:' +del.geterrors());
                } 
            }
      }
        }                                                                                               pulling record from lockproduct(shadow). Inserting in to OLI

Trigger lockproduct on lockproduct__c (after update) {

          List<lockproduct__c> lplist = new list<lockproduct__c>();
          List<opportunitylineitem> opl = new list<opportunitylineitem>();
          for(lockproduct__c lp :Trigger.new){
          system.debug('productid in lock record:' +lp.Product__c);

         List<opportunity> opp = [select o.id, o.Pricebook2Id from opportunity o  where o.id =: lp.Opportunity__c];


          if(Trigger.isafter && lp.lockrecord__c == false){
          opportunitylineitem oli = new opportunitylineitem();
              for(opportunity op:opp){

                  if(op.id==lp.Opportunity__c){
                   oli.Opportunityid = op.id;
                   oli.code_bases__c = lp.codebases__c;
                   //oli.Pricebookentryid=pe[0].id; issue is here
                   system.debug('pricebookid:' +oli.Pricebookentryid);
                    oli.UnitPrice = lp.salesprice__c;
                    oli.Quantity =  lp.Unit_Quantity__c;
                    opl.add(oli);
                } 

          }

          insert opl;
          system.debug('opputunityname:' +oli.Opportunityid);

          system.debug('pricebookid in saverecord:' +oli.Pricebookentryid);

           if(opl.size()!=0)
           {
          Database.SaveResult[] olilis = database.insert(opl);
           for(Database.SaveResult olir :olilis){
            if(olir.issuccess())
            {
                system.debug('record is inserted:' +olir.getid());

            }
            else
            {
                system.debug('record is failed:' + olir.getid());
            }
           }
           }
         }
    }

}

Please see my code below. my agenda is opportunity line items which and all go to approval process, they are not added to opportunity untill they get approved. This is the reason that i am deleting record as soon as it get inserted. Is there any way that i can store pricebookentryid before it get deleted. So that i can move it to opportunitylitem before it get inserted from shadow object.

  • (1) You are correct that inserting OLI requires a PBEid. (2) You'll need to post your code (essential bits only). Personally, I might have designed this so that the "on approval" OLIs were kept as OLIs but with a shadow quantity and total price (where OLI.quantity = 0). Then once approved, you simply move the shadow quantity to the real quantity and the totals properly rollup – cropredy Jun 17 '16 at 5:06

Your Answer

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

Browse other questions tagged or ask your own question.