You need to sign in to do that
Don't have an account?
How to pass parameters as wrapper class object from aura component to method in apex class
Lightning Application
Lightning component
Controller of component
Ape
x class
<aura:application > <c:WrapperComponent/> </aura:application>
Lightning component
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="person" > <aura:attribute type="person.WrapperClass" name="testAttribute" /> <lightning:input value="{!v.testAttribute.firstName}" name="first"/> <lightning:input value="{!v.testAttribute.lastName}" name="last"/> <button class="slds-button slds-button_brand" onclick="{!c.submit}">Submit</button> </aura:component>
Controller of component
({ submit : function(component, event, helper) { var action = component.get("c.getSuccess"); action.setParams({ obj : component.get('v.testAttribute') }); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { alert("From server: " + response.getReturnValue()); } else if (state === "INCOMPLETE") { alert("From server : Sorry server call is incomplete"); } else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(action); } })
Ape
global with sharing class person { public class WrapperClass { @AuraEnabled public String firstName; @AuraEnabled Public String lastName; } @AuraEnabled public Static String getSuccess(WrapperClass obj){ String r =''; if(obj.firstName == 'text' && obj.lastName=='text') { r='hi';} return r; } }
x class
error 1 - " Uncaught TypeError: Cannot read property 'firstName' of null throws at https://herishsurendran-dev-ed.lightning.force.com/auraFW/javascript/3uHUkqaEy5o9m3W8DAEYIw/aura_prod.js:544:410 "
error 2 - " Uncaught TypeError: Cannot read property 'lastName' of null throws at https://herishsurendran-dev-ed.lightning.force.com/auraFW/javascript/3uHUkqaEy5o9m3W8DAEYIw/aura_prod.js:544:410 "
You need to make a constructor for your wrapper class, which, may or may not be necessary for your example. To pass the wrapper class from your aura component to your apex class, you are going to need to serialize it. To receive it into your apex class
error 2 - " Uncaught TypeError: Cannot read property 'lastName' of null throws at https://herishsurendran-dev-ed.lightning.force.com/auraFW/javascript/3uHUkqaEy5o9m3W8DAEYIw/aura_prod.js:544:410 "
You have to create a constructor for that:
Example:
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi