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
imrohitimrohit 

hello developers

I am having  a problem  , i am showing record to the lightning component with checkboxes using the following apex code
public with sharing class bOQrenewClass {
    @AuraEnabled
    public static List<WrapperClass> getproducts() {
        List<WrapperClass> wrapperList = new List<WrapperClass>();
		Map<String, List<SelectedRecordsModel>> stageMap = new Map<String, List<SelectedRecordsModel>>();
       
        
        for(productRenew__c obj: [SELECT Id, Name, Quantity__c, Price__c, Stage__c FROM productRenew__c]){
            stageMap.put(obj.Stage__c, null);           
        }
        
        for(productRenew__c obj: [SELECT Id, Name, Quantity__c, Price__c, Stage__c FROM productRenew__c ORDER BY Stage__c ASC, Name ASC]) {                      
            List<SelectedRecordsModel> prodList = new List<SelectedRecordsModel>();
            
            if (stageMap.get(obj.Stage__c) != null) {
                prodList = stageMap.get(obj.Stage__c);
            }
            
            prodList.add(new SelectedRecordsModel(obj));      
            stageMap.put(obj.Stage__c, prodList);
        }
                    
        for(String obj: stageMap.keyset()){
            WrapperClass wrapperObj = new WrapperClass();
            wrapperObj.Stage = obj;
            wrapperObj.productRenewList = stageMap.get(obj);
            wrapperList.add(wrapperObj);
        }
        
        return wrapperList;
    }
    
   @AuraEnabled
    public static void performAction(List<WrapperClass> positionRecords) {       
       for(WrapperClass obj:positionRecords)
            {
                for(SelectedRecordsModel obj1: obj.productRenewList)
                if(obj1.isSelected)
                {
                    //Play with selected Records here                   
                    system.debug('Selected Record :'+obj1);
                }
            }      
    }
    public class WrapperClass {
        @AuraEnabled String Stage{get; set;}
        @AuraEnabled List<SelectedRecordsModel> productRenewList {get; set;}
        
    }
    public class SelectedRecordsModel {
        @AuraEnabled Boolean isSelected {get;set;}
        @AuraEnabled productRenew__c prd {get;set;}
        
        public SelectedRecordsModel(ProductRenew__c prd){
            this.prd = prd;
            isSelected = false;
        }
    }
}

and here is the component code
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="bOQrenewClass">
    
    <aura:attribute name="wrapperList" type="object[]"/>
    
    
    <aura:handler name="init" value="{! this }" action="{! c.doInit }"/>
    
    <lightning:card  title="xyz">
        <table class="slds-table slds-table_bordered slds-max-medium-table_stacked-horizontal ">
            <thead>
                <tr class="slds-text-title--caps">
                    <th scope="col">
                        <div class="slds-truncate " title="Account Name">SELECT</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate  " title="FIRsT Name">PRODUCT</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate " title="Account Name">PRICE</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate " title="Close Date">QUANTITY</div>
                    </th>
                    <th scope="col">
                        <div class="slds-truncate " title="Stage">TOTAL</div>
                    </th>      
                </tr>
            </thead>
            
            <aura:iteration items="{!v.wrapperList}" var="obj">
                
                <tr class="slds-box slds-theme_shade ">
                    <td colspan="5">{!obj.Stage}</td>
                   
                </tr>
                <tbody>
                <aura:iteration items="{!obj.productRenewList}" var="iobj">
                    <tr class="slds_line_height-small" >
                       <!-- <td><lightning:input type="checkbox"/></td>-->
                        <td><ui:inputCheckbox value="{!iobj.isSelected}"/></td>
                        <td> <ui:outputText value="{!iobj.prd.Name}" /> </td>
                       <!-- <td>{!iobj.prd.Name}</td>
                        <td>{!iobj.prd.Price__c}</td>-->
                         <td> <ui:outputText value="{!iobj.prd.Price__c}" /> </td>
                        <td><lightning:input type="Number" value="{!iobj.Quantity__c}" /></td>
                        <td><ui:outputText value="{!iobj.prd.Price__c * (iobj.prd.Quantity__c !=null ? iobj.prd.Quantity__c : 0)}" /> </td>
                    </tr>
                </aura:iteration>
                </tbody>
            </aura:iteration>
        </table>
         
            
            <lightning:button class="slds-theme_inverse slds-p-horizontal_xx-large"
                              aura:id="expenseform2" label="  SAVE  "
                              name="Mobile"
                              onclick="{!c.searchitems1}"/>    
                  	
    </lightning:card>
    
</aura:component>

here is the jscontroller
({
	
    searchitems1 : function(component, event, helper) {
        
       var selectedRecords = component.get("v.productRenewList");
        console.log(JSON.stringify(selectedRecords));
            var action = component.get("c.performAction");           
            action.setParams({
                positionRecords : selectedRecords
            });
            
           
            action.setCallback(this,function(a){                
                var state = a.getState();                
                if(state == "SUCCESS"){                                       
                    alert('Success in calling server side action');                    
                } else if(state == "ERROR"){
                    alert('Error in calling server side action');
                }
            });
            $A.enqueueAction(action);            
        },
  
    doInit : function(cmp,event,helper){
        //alert('hello');
        
        var action = cmp.get("c.getproducts");
        action.setCallback(this,function(response){
            var state = response.getState();
			//alert(JSON.stringify(response.getReturnValue()));
            if(state == "SUCCESS"){
                cmp.set("v.wrapperList", response.getReturnValue());
            }
            
        });
        $A.enqueueAction(action);
        
       
    }
})

the data is displaying properly on the component but when i select some checkbox and click on save i am not abeling to get those selected records to the debug of the apex method i am getting internal salesforce error for this

what changes should  i make to the code please help
Best Answer chosen by imrohit
Nilesh C RathodNilesh C Rathod
Hi,

You can't directly pass wrapper list to Apex Controller. You need to make following changes to execute your code

JS Controller
 
var action = component.get("c.performAction");           
            action.setParams({
                positionRecords : JSON.stringify(selectedRecords)
            });

Apex Controller
 
public static void performAction(String positionRecords) {       
if(String.isNotBlank(positionRecords)) {
List<WrapperClass> records = (List<WrapperClass>)
			System.JSON.deserialize(positionRecords,List<WrapperClass>.class);

--- your code ---

}
----- logic -----
}

 

All Answers

Sampath SuranjiSampath Suranji
Hi,

I guess you are going to update/save some values in productRenewList when click save.
The better way I suggest you to create a list of Ids when user check each check box. instead of passing List<WrapperClass> to the 'performAction' method, pass the Ids of selected items which we already created.
then query them inside the apex method and do relevant update.

regards
Nilesh C RathodNilesh C Rathod
Hi,

You can't directly pass wrapper list to Apex Controller. You need to make following changes to execute your code

JS Controller
 
var action = component.get("c.performAction");           
            action.setParams({
                positionRecords : JSON.stringify(selectedRecords)
            });

Apex Controller
 
public static void performAction(String positionRecords) {       
if(String.isNotBlank(positionRecords)) {
List<WrapperClass> records = (List<WrapperClass>)
			System.JSON.deserialize(positionRecords,List<WrapperClass>.class);

--- your code ---

}
----- logic -----
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
You can do it as below 

1 . Create an Attribute of type Array that holds the record id when you select the checkbox 
2 . Use lightning:input  onChange action to add the records Id to above create attributes 
3.  On Click of save get the values from the attribues