1

I am trying to update quantity field in customer_product__c, but no luck - its updating in client level but not in server

Code:

/*method to update quantity in customer product */
     public void save(){
     List<Customer_Product__c> cList = new List<Customer_Product__c>();
     for(proWrpr csp: cusProWrprList){
        if(csp.cu.Quantity__c !=NULL ){
        if(csp.cu.Quantity__c != cusMap.get(csp.cu.Product_Code__c).Quantity__c){
         cusMap.get(csp.cu.Product_Code__c).Quantity__c= csp.cu.Quantity__c;
          system.debug('cusMap.get(csp.cu.Product_Code__c).Quantity__c#####' + cusMap.get(csp.cu.Product_Code__c).Quantity__c);
           cList.add(cusMap.get(csp.cu.Product_Code__c));
           system.debug('cList#####' + cList.size());
        }
        }
     }
     try{
       update cList;
     }catch(Exception e){
     } 
    }

VF Page:

<apex:commandButton value="Save" action="{!save}"  id="saveId" style="display:none"/>

enter image description here

  • 1
    Never use an empty catch block. You are just hiding the error from yourself. – Adrian Larson Jun 6 '17 at 17:59
3

To update a record you need to have Id.

cList doesn't have Id.

So, use the following logic in your loop and update the records.

List<Customer_Product__c> cList = new List<Customer_Product__c>();

//put this inside for loop
Customer_Product__c obj = new Customer_Product__c(Id=...., Quantity__c = ...);
cList.add(obj)

//make an update outside for loop
update cList;
  • Are you sure there's no ID? We don't have enough code to verify this either way. It could be some random DML error. We can't tell because there's a try-catch block swallowing up any errors. – sfdcfox Jun 6 '17 at 18:04
  • Inside save method cList is declared and I dont see any code other than cList.add(cusMap.get(csp.cu.Product_Code__c)); and update cList – Santanu Boral Jun 6 '17 at 19:30

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.