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
Pedro Garcia GPedro Garcia G 

action.setParams doesn't work

Hi...

I need to navigate from one component (component "A") to another (component "B"), send parameters and request data from apex class.

Everything works except sending parameter to the apex class... here is my code:

Navigating to other component and send parameters:

Component A. controller
...
handleRowAction: function (component, event, helper) {

            
        var evt = $A.get("e.force:navigateToComponent");
        evt.setParams({
            componentDef : "c:CASCSVFile",
            componentAttributes: {
                selectedRow : JSON.stringify(event.getParam('row'))
            }
        });
        evt.fire();
 
       
    }
...

Component B XML
<aura:component controller="CASExportFile" >
 


        <!-- attributes -->
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="selectedRowsCount" type="Integer" default="0"/>
 
	<aura:attribute name="selectedRow" type="String" access="global"/>

...more code
Component B controller
init: function (component, event, helper) {

    var selectedItem = JSON.parse(component.get("v.selectedRow"));

    var action = component.get("c.csvtojson");
    
    	
        action.setParams({
            "selectedItem":  selectedItem
        });

    console.log('3.4>>>'+component.get("v.selectedRow"));
    console.log('3.5>>>'+selectedItem.Id);
    
        	action.setCallback(this, function(response) {
			var state = response.getState();
            

			if (state === "SUCCESS") {
                 
				var storeResponse = response.getReturnValue();


                console.log('3.6>>>'+Object.getOwnPropertyNames(storeResponse[0]));
                
                var headerlist = Object.getOwnPropertyNames(storeResponse[0]);
                
                var headerArray = [];
               
                for (var i in headerlist ) { 
                    
                    headerArray.push({ label: headerlist[i], fieldName: headerlist[i], type: 'text'})
                }
                
                headerArray.push({label: 'View', type: 'button', initialWidth: 135, typeAttributes: { label: 'View Details', name: 'view_details', title: 'Click to View Details'}});
                console.log('3.7>>>'+JSON.stringify(headerArray));

                 
                component.set('v.columns',headerArray);
             	component.set("v.data", storeResponse);
                component.set("v.filteredData", storeResponse);
                
                
                if(storeResponse != null && storeResponse.length > 0){

                    component.set("v.hasRecords", "true");
                }
                
                component.set('v.maxRowSelection', storeResponse.length);

			}
			else if (state === "RUNNING") {
                //@todo start the timer
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("3.>>Error message: " + 
                                 errors[0].message);
                        alert(errors[0].message);
                    }
                } else {
                    console.log("3.>>Unknown error");
                }
            }
         }); 
    
 
		$A.enqueueAction(action);
    },

Lightning Action console
User-added imageBrowser Log Console

User-added image

 
Best Answer chosen by Pedro Garcia G
Ajay K DubediAjay K Dubedi
Hi Pedro,
Try Below code it may helpful for you:
Component A. controller
handleRowAction: function (component, event, helper) {
           
        var evt = $A.get("e.force:navigateToComponent");
        evt.setParams({
            componentDef : "c:CASCSVFile",
            componentAttributes: {
                selectedRow : event.getParam('row')
            }
        });
        evt.fire();
 
      
    }
...
Component B XML
<aura:component controller="CASExportFile" >
 
        <!-- attributes -->
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="selectedRowsCount" type="Integer" default="0"/>
 
 <aura:attribute name="selectedRow" type="Object[]" access="global"/>
...more code
Component B controller
init: function (component, event, helper) {
 var selectedItem = component.get("v.selectedRow");
 var action = component.get("c.csvtojson");
  
  action.setParams({
   "selectedItem":  JSON.stringify(selectedItem)
  });
 console.log('3.4>>>'+component.get("v.selectedRow"));
 console.log('3.5>>>'+selectedItem.Id);
  action.setCallback(this, function(response) {
  var state = response.getState();
  
  if (state === "SUCCESS") {
   
   var storeResponse = response.getReturnValue();
   console.log('3.6>>>'+Object.getOwnPropertyNames(storeResponse[0]));
   
   var headerlist = Object.getOwnPropertyNames(storeResponse[0]);
   
   var headerArray = [];
    
   for (var i in headerlist ) {
    
    headerArray.push({ label: headerlist[i], fieldName: headerlist[i], type: 'text'})
   }
   
   headerArray.push({label: 'View', type: 'button', initialWidth: 135, typeAttributes: { label: 'View Details', name: 'view_details', title: 'Click to View Details'}});
   console.log('3.7>>>'+JSON.stringify(headerArray));
   
   component.set('v.columns',headerArray);
   component.set("v.data", storeResponse);
   component.set("v.filteredData", storeResponse);
   
   
   if(storeResponse != null && storeResponse.length > 0){
    component.set("v.hasRecords", "true");
   }
   
   component.set('v.maxRowSelection', storeResponse.length);
  }
  else if (state === "RUNNING") {
   //@todo start the timer
  }
  else if (state === "ERROR") {
   var errors = response.getError();
   if (errors) {
    if (errors[0] && errors[0].message) {
     console.log("3.>>Error message: " +
        errors[0].message);
     alert(errors[0].message);
    }
   } else {
    console.log("3.>>Unknown error");
   }
  }
  });
 $A.enqueueAction(action);
},
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Pedro,
Try Below code it may helpful for you:
Component A. controller
handleRowAction: function (component, event, helper) {
           
        var evt = $A.get("e.force:navigateToComponent");
        evt.setParams({
            componentDef : "c:CASCSVFile",
            componentAttributes: {
                selectedRow : event.getParam('row')
            }
        });
        evt.fire();
 
      
    }
...
Component B XML
<aura:component controller="CASExportFile" >
 
        <!-- attributes -->
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="selectedRowsCount" type="Integer" default="0"/>
 
 <aura:attribute name="selectedRow" type="Object[]" access="global"/>
...more code
Component B controller
init: function (component, event, helper) {
 var selectedItem = component.get("v.selectedRow");
 var action = component.get("c.csvtojson");
  
  action.setParams({
   "selectedItem":  JSON.stringify(selectedItem)
  });
 console.log('3.4>>>'+component.get("v.selectedRow"));
 console.log('3.5>>>'+selectedItem.Id);
  action.setCallback(this, function(response) {
  var state = response.getState();
  
  if (state === "SUCCESS") {
   
   var storeResponse = response.getReturnValue();
   console.log('3.6>>>'+Object.getOwnPropertyNames(storeResponse[0]));
   
   var headerlist = Object.getOwnPropertyNames(storeResponse[0]);
   
   var headerArray = [];
    
   for (var i in headerlist ) {
    
    headerArray.push({ label: headerlist[i], fieldName: headerlist[i], type: 'text'})
   }
   
   headerArray.push({label: 'View', type: 'button', initialWidth: 135, typeAttributes: { label: 'View Details', name: 'view_details', title: 'Click to View Details'}});
   console.log('3.7>>>'+JSON.stringify(headerArray));
   
   component.set('v.columns',headerArray);
   component.set("v.data", storeResponse);
   component.set("v.filteredData", storeResponse);
   
   
   if(storeResponse != null && storeResponse.length > 0){
    component.set("v.hasRecords", "true");
   }
   
   component.set('v.maxRowSelection', storeResponse.length);
  }
  else if (state === "RUNNING") {
   //@todo start the timer
  }
  else if (state === "ERROR") {
   var errors = response.getError();
   if (errors) {
    if (errors[0] && errors[0].message) {
     console.log("3.>>Error message: " +
        errors[0].message);
     alert(errors[0].message);
    }
   } else {
    console.log("3.>>Unknown error");
   }
  }
  });
 $A.enqueueAction(action);
},
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Pedro Garcia GPedro Garcia G
It works!!! Thanks