function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yuki TanYuki Tan 

Some error with update record in apex class.

Hi guys. I have some error with update record in apex class.
I write a datatable and a button in cmp. I wanna select some rows in datatable, and change the field of selected records when i click the button.
My datatable is show a custom object called Case2__c. I wanna change a checkbox field called approvalStatus__c from false to true.
Now, when i click the button, the page haven't response. I write an alert in callback when SUCCESS, page is no alert. When i delete "update cs", page have alert.
There is my code.

Cmp:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="shipClass">
	<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
	<aura:attribute name="columns" type="List"/>
	<aura:attribute name="data" type="Object"/>
	<aura:attribute name="errors" type="Object" default="[]"/>
	<aura:attribute name="selectedCase" type="List"/>

	<lightning:card title="Ship">
	<lightning:layout  multipleRows="true">
		<lightning:layoutItem padding="around-small">
			<lightning:button name="shipButton" label="ship" onclick="{!c.ship}"/>
		</lightning:layoutItem>
	</lightning:layout>
		
		<lightning:datatable
			columns="{! v.columns }"
			data="{! v.data }"
			keyField="Id"
			errors="{! v.errors}"
			onrowselection="{!c.handleSelect}"
			hideCheckboxColumn="false"/>
	</lightning:card>
</aura:component>

 Controller:
({
    doInit : function(component, event, helper) {
    	component.set('v.columns', [
            { label: 'Case No.', fieldName: 'Name', type: 'text',editable:false},
            { label: 'Product', fieldName: 'ProductName', type: 'text',editable:false},
            { label: 'Type', fieldName: 'Customer_Type__c', type: 'text',editable:false},
            { label: 'Account', fieldName: 'AccountName', type: 'text',editable:false},
            { label: 'Amount', fieldName: 'Amount__c', type: 'number',editable:false}]);
    	helper.getShipRecord(component,event,helper); 
    },
    ship : function(component, event, helper){
      var records = component.get("v.selectedCase");
      var action = component.get("c.pass");
      action.setParam("shipList", records);
      action.setCallback(this, function(response){
        var state = response.getState();
        // call back success
        if (state == "SUCCESS"){
          alert('2');

          var a = component.get('c.doInit');
          $A.enqueueAction(a);
        }
        // call back failed
        else {
          var a = component.get('c.doInit');
          $A.enqueueAction(a);
        }
      });
      $A.enqueueAction(action);
    },
    handleSelect : function(component, event, helper){
      var selectedRows = event.getParam('selectedRows');
      var setRows = [];
      for ( var i = 0; i < selectedRows.length; i++ ) {
        setRows.push(selectedRows[i]);
      }
      component.set("v.selectedCase", setRows);
    }
})
Helper:
({
	getShipRecord : function(component, event, helper) {
		var action = component.get("c.query");
        // call back
        action.setCallback(this, function(response){
            var state = response.getState(); 
            // call back success
            if (state == "SUCCESS"){
                var rows = response.getReturnValue();
                for(var i = 0; i < rows.length; i++){
                    var row = rows[i];
                    if(row.Product__r) row.ProductName = row.Product__r.Name;
                    if(row.Account__r) row.AccountName = row.Account__r.Name;
                }
                component.set('v.data', rows);
            }
            // call back failed
            else {
                var toastEvent=$A.get("e.force:showToast");
                toastEvent.setParams({
                    "title":'Error!',
                    "message":'Get Record Failed!',
                    "duration":5000,
                    "type":ERROR
                });
                toastEvent.fire();
            }
        });
        $A.enqueueAction(action);
	}
})

Apex class:
public class shipClass {
    @AuraEnabled
    public static List<Case2__c> query() {
        List<Case2__c> ship = new List<Case2__c>();
        ship = [SELECT Name,Product__r.Name,Account__r.Name,Customer_Type__c,Amount__c FROM Case2__c 
        		WHERE Type__c = 'ship' AND ApprovalStatus__c = false 
        		ORDER BY Account__r.Name ASC];
        return ship;
    }
    @AuraEnabled
    public static void pass(List<Case2__c> shipList) {
    	for(Case2__c a : shipList){
    		List<Case2__c> csList = [SELECT Id,Name,ApprovalStatus__c FROM Case2__c WHERE Name = :a.Name];
    		if(csList.size() > 0){
    			Case2__c cs = csList[0];
    			cs.ApprovalStatus__c = true;
    			update cs;
    		}
    		
    	}
    }
}

How can i fix that. Pls help me. Tanks very much.
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Yuki,

Have you tried checking if the values are updating in case if the update statement is present?

Regards,
Anutej

 
Yuki TanYuki Tan
Hi ANUTEJ,
yes i check that, but actually i click button and wait minute, web still not response and i refresh the record still here. if upadate success, it will be not here because i only select approval status = false.