+ Start a Discussion

lwc to pass id to another component

how to pass record id from one component to another in lightning web component ??

please do tell using the example,, suppose i have one component in which i am creating campaign and after clicking on save button it jumps to another component and in  that component i want to create a campaign member for the (id) campaign which is created in previous component.

thank you
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sonu,

You could make use of events to pass information between different components, below is the link to the example I found: https://salesforce.stackexchange.com/questions/219130/passing-id-between-2-lightning-components

The information in above link is below for quick reference:

Use a lightning event to communicate the survey Id to the parent component, then the parent can pass it along to the other component either using an aura:attribute and binding the new value into the child component's survey Id attribute (recommended for your case), or by calling an aura:method defined on the child component and passing the survey Id as a parameter into that method.

See how to pass information from a child to a parent via a component event here: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_component_example.htm

As a general rule of thumb, component events are used to pass information to the parent, and the parent can use methods to pass/call the child component to do something.

Bonus Edit:

A really great article from Philippe Ozil about best practices for communicating between components: Salesforce Developers Blog: Lightning Inter-Component Communication Patterns

I hope you find this useful and in case if this comes in handy can you please choose this as the best answer so that it can be used by others in the future.

Hi Anutej,
thank you for your response. The answer you shared is for aura component, but i dont have the knowledge of aura component,, I am trying to learn Lightning WEB components, I want to  pass id using web components, can you please share the info for web components.

ANUTEJANUTEJ (Salesforce Developers) 
So I found a few similar usecases of passing data between different components, below are the links that has an implementation:

>> https://medium.com/@varma6228/how-to-pass-data-from-one-lwc-component-to-another-lwc-component-lightning-web-component-887f7c928eff

>> https://salesforcediaries.com/2019/07/24/lwc-communication-part-1-passing-data-from-parent-to-child-component/

I hope this helps.
onclick of save you need to write logic for saving campaign record, after successfl save you can get campaign id. then you can render campign memeber component with campign id and write logic for campign members in child component.

chek this link for How to invoke/call child lightning web component function from parent lightning web component (https://www.salesforcepoint.com/2020/06/how-to-invoke-call-child-lightning-web.html)