{"id":6007,"date":"2021-02-12T12:28:00","date_gmt":"2021-02-12T12:28:00","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6007"},"modified":"2021-02-11T12:29:37","modified_gmt":"2021-02-11T12:29:37","slug":"using-aura-method-to-pass-value-from-child-component-to-parent-component-with-example","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/02\/12\/using-aura-method-to-pass-value-from-child-component-to-parent-component-with-example\/","title":{"rendered":"Using Aura method to pass value from Child component to Parent component (with example)"},"content":{"rendered":"\n<p>In this blog, we will discuss how to use <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.lightning.meta\/lightning\/ref_tag_method.htm\" target=\"_blank\">&lt;aura:method><\/a> to define a method as part of a component&#8217;s API. This enables you to directly call a method in a component\u2019s client-side controller instead of firing and handling a component event.<\/p>\n\n\n\n<p>The &lt;aura:method> tag has following attributes:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Attribute<\/td><td>Type<\/td><td>Description<\/td><\/tr><tr><td>name<\/td><td>String<\/td><td>The method name. In other words, you can use the method name to call the method in JavaScript code.<\/td><\/tr><tr><td>action<\/td><td>Expression<\/td><td>The client-side controller action to execute.<\/td><\/tr><tr><td>access<\/td><td>String<\/td><td>The access control for the method. Valid values are:<br><em>public<\/em>\u2014Any component in the same namespace can call the method. This is the default access level.<br><em>global<\/em>\u2014Any component in any namespace can call the method.<\/td><\/tr><tr><td>description<\/td><td>String<\/td><td>The method description.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>An &lt;aura:method> can optionally include parameters. We use an &lt;aura:attribute> tag within an &lt;aura:method> to declare a parameter for the method.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<p>In the below example, we are creating a Parent component aura method with two parameters. These two parameters are \u2018GreetingParam&#8217; and \u2018PersonNameParam&#8217;. After that, we assign these parameters default values of \u2018Hello\u2019 and \u2018John\u2019 respectively.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ParentComponent<\/strong><\/h4>\n\n\n\n<p>We can define the component using the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:component>\r\n&lt;aura:method name=\"greetingMethod\" action=\"{!c.getMessage}\" access=\"public\">\r\n\t&lt;aura:attribute name=\"GreetingParam\" type=\"String\" default=\"Hello\"\/> \r\n\t&lt;aura:attribute name=\"PersonNameParam\" type=\"String\" default=\"John\"\/> \r\n&lt;\/aura:method>\r\n&lt;\/aura:component>\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ParentComponentController<\/strong><\/h4>\n\n\n\n<p>Firstly, in Parent Component controller, we are getting the value of parameters and showing alert.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n    getMessage : function(component, event) {\r\n        \/\/get method paramaters\r\n        var params = event.getParam('arguments');\r\n        if (params) {\r\n            var param1 = params.GreetingParam;\r\n            var param2 = params.PersonNameParam;\r\n            alert(param1 + \" \" + param2);\r\n        }\r\n    }\r\n})\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ChildComponent<\/strong><\/h4>\n\n\n\n<p>Now in child component we are declaring aura:attribute of type Aura.Component, so we can store parent component reference. Also added button for calling child component controller method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:attribute name=\"parent\" type=\"Aura.Component\"\/>\r\n&lt;lightning:button variant=\"brand\" label=\"Invoke Parent Method\" onclick=\"{!c.callParentMethod}\" \/><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>ChildComponentController<\/strong><\/h4>\n\n\n\n<p>Similarly, in ChildComponentController, we have to get the parent component and calling it\u2019s aura method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n\tcallParentMethod : function(component, event, helper) {\r\n        \/\/Call Parent aura method\r\n        var parentComponent = component.get(\"v.parent\");                         \r\n\t\tparentComponent.greetingMethod()\r\n    }\r\n})<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Updated ParentComponent<\/strong><\/h4>\n\n\n\n<p>After that, in the Parent component, we need to add child component and pass the reference in it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:component>\r\n&lt;aura:method name=\"greetingMethod\" action=\"{!c.getMessage}\" access=\"public\">\r\n\t&lt;aura:attribute name=\"GreetingParam\" type=\"String\" default=\"Hello\"\/> \r\n\t&lt;aura:attribute name=\"PersonNameParam\" type=\"String\" default=\"John\"\/> \r\n&lt;\/aura:method>\r\n&lt;c:ChildComponent parent=\"{!this}\">&lt;\/c:ChildComponent>\r\n&lt;\/aura:component>\r<\/code><\/pre>\n\n\n\n<p>Here is output when users click on <strong>Invoke Parent Method <\/strong>button. Output is \u2018Hello John\u2019 because no value of parameter is passed. So it is showing default value.<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/02\/Aura-Compnent-Example.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/02\/Aura-Compnent-Example.png\" alt=\"Aura Component Example\"><\/a><\/center>\n<font size=\"2\"><center><i>Aura Component Example<\/i><\/center><\/font>\n\n\n\n<p>In the above example, we are using parameters of the parent component. We can also pass parameters from child component to parent component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n\tcallParentMethod : function(component, event, helper) {\r\n        \/\/Call Parent aura method\r\n        var parentComponent = component.get(\"v.parent\");                         \r\n\t\tparentComponent.greetingMethod('Happy coding','session')\r\n    }\r\n})<\/code><\/pre>\n\n\n\n<p>Now when the user click on the <strong>Invoke Parent Method<\/strong> button on the child component. The output will be \u2018Happy coding session\u2019 instead of \u2018Hello John\u2019 because we are passing the value of attributes.<\/p>\n\n\n\n<p><strong><em>aura:method always executes synchronously so method execution finishes before it returns.<\/em><\/strong><\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at\u00a0<a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/20\/how-to-add-lightning-web-components-as-custom-tabs-in-salesforce\/\" target=\"_blank\">How to add Lightning Web Components as Custom Tabs in Salesforce<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/20\/how-to-set-cookies-in-http-callout-apex-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to set cookies in HTTP Callout (APEX Salesforce)<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/22\/salesforce-how-to-get-row-index-of-lightning-table-rows\/\" target=\"_blank\" rel=\"noreferrer noopener\">Salesforce \u2013 How to get row index of lightning table rows<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2020\/09\/30\/adding-specific-nodes-to-mascontract-file-to-sync-records-from-sage-100-erp-to-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\">Adding specific nodes to MasContract File to sync records from Sage 100 ERP to Salesforce<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss how to use &lt;aura:method> to define a method as part of a component&#8217;s API. This enables you to directly call a method in a component\u2019s client-side controller instead of firing and handling a component event. The &lt;aura:method> tag has following attributes: Attribute Type Description name String The method name.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/02\/12\/using-aura-method-to-pass-value-from-child-component-to-parent-component-with-example\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1068,1070,1055,1069,1067],"class_list":["post-6007","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-aura-method","tag-child-parent-component","tag-lightning-aura-component","tag-lightning-aura-method","tag-lightning-method"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6007"}],"version-history":[{"count":1,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6007\/revisions"}],"predecessor-version":[{"id":6011,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6007\/revisions\/6011"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}