SALESFORCE1 (AURA PLATFORM): How to pass values ​​from client code (JavaScript code) to server code (APEX)?

I am working on Salesforce1 (Aura platform). I am trying to pass values ​​from client side (javascript) to server side controller (Apex-code). I've tried using setParams();

JavaScript and @key

annotations in Apex, but @key

not supported in Apex.

Thanks in advance.

I've provided some sample code here ...

Application code :

     <aura:application>
      <PlumQ:example/>
     </aura:application>

      

COMPONENT code:

<aura:component model="PlumQ.ExampleServerSideController">

  <aura:attribute name="firstName" type="String" default="HELLO worlD"/>

  <ui:inputtext label="Name" aura:id="id1" value="{!v.firstName}" placeholder = "enter name" />

   <ui:button label="Native Aura Button" press="{!c.echo}"/>

</aura:component>

      

** client-side controller (JavaScript): **

 ({
   "echo" : function(component) {

          alert('in client-Side-process');

          var b =component.get("v.firstName");
          alert('firstnaaaaame:::::::::::::'+b);
           var a = component.get("m.serverEcho");
           alert('After ServerSide Process');
          a.setParams({ firstName : component.get("v.firstName") });



           a.setCallback(this, function(action) {

                        if (action.getState() === "ERROR") {
                                     alert("Server Error: " + action.getError()[0].message);
                          }
                        else{

                                     alert("From server: " + action.getReturnValue());

                       }
});

             $A.enqueueAction(a);

      

}})

server controller (APEX CLASS):

  public class ExampleServerSideController {


  @AuraEnabled
  public static String serverEcho(@Key("firstName") String firstName){

  System.out.println("In Example Trival controllerrrrr"+firstName);

  return ("From server: " +firstName);

   }

  }

      

+3


source to share


2 answers


Your code is almost right. You only need to put quotes ( "

) around firstName and remove the @Key annotation from your vertex controller. So your JS will look like this:

a.setParams({
  "firstName" : component.get("v.firstName")
});

      



And your top:

public static String serverEcho(String firstName)

      

+1


source


You don't need the @key notation for this, the string should work.

Try to put $ A.enqueueAction (a); inside of you closes like this:



({
  "echo" : function(component) {

      alert('in client-Side-process');

      var b =component.get("v.firstName");
      alert('firstnaaaaame:::::::::::::'+b);
       var a = component.get("m.serverEcho");
       alert('After ServerSide Process');
      a.setParams({ firstName : component.get("v.firstName") });



       a.setCallback(this, function(action) {

                    if (action.getState() === "ERROR") {
                                 alert("Server Error: " + action.getError()[0].message);
                      }
                    else{

                                 alert("From server: " + action.getReturnValue());

                   }
       $A.enqueueAction(a);

      }
  });

      

0


source







All Articles