[SalesForce] How send the parameter recordId from lightning component to Apex controller

I have a problem when I try to send the recordId to my Apex Controller, in this case the Apex don't receive the parameter, so I can't use it into the apex controller.

The Lightning Component is in a Community.

Can you help me? Thanks.

Component

<aura:component controller="MyController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler name="init" action="{!c.doInit}" value="{!this}" access="global" />
    <aura:attribute name="artId" type="String" default="{!v.recordId}" access="global" />   
</aura:component>

Design

<design:attribute name="artId" label="Record ID" default="{!recordId}" />   

Controller JS

doInit : function(component, event, helper) {

    var action = component.get("c.setViewStat");    
    var artId = component.get("v.recordId");

    console.log(artId);

    action.setParams({
        "artId":artId
    });
}

Apex Controller

global with sharing class MyController{

    @AuraEnabled
    global static void setViewStat(String artId) {
        try{
            Code...
        }
        catch(Exception e){
            System.debug(e);
        }               
    }

Best Answer

You don't need "artId", so get rid of that:

<aura:component controller="MyController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:handler name="init" action="{!c.doInit}" value="{!this}" access="global" />
</aura:component>

You didn't actually call the server's method:

doInit : function(component, event, helper) {

    var action = component.get("c.setViewStat");    
    var artId = component.get("v.recordId");

    console.log(artId);

    action.setParams({
        "artId":artId
    });
    // Queue this action to send to the server
    $A.enqueueAction(action);
}

For completeness, artId is really an Id, so you should use it as such in your controller:

global with sharing class MyController{

    @AuraEnabled
    global static void setViewStat(Id artId) {
        try{
            Code...
        }
        catch(Exception e){
            System.debug(e);
        }               
    }
}
Related Topic