Hello Trailblazers,
I am sharing a trick "How to call Lightning Web Component from formula field" which might be helpful for you.
Use Case:
The requirement is to open a Lightning web component from the details page, on click of the field. Where the field value on the standard detail page will work as a link.
Approach:
I am going to create a formula field with a URL inside it. Now we need to associate the Lwc with a URL, for that we need to wrap the Lwc inside an Aura component. The component implements the URL addressable interface.
Update: If you just want to add query parameters and pass those to LWC from the lightning application, then you don't need to wrap the LWC inside an Aura Component, refer to the standard lwc docs Or visit my post: Stay on the same tab after page refresh lightning-tab in lwc.
Implementaion and Code
Let's create a URL addressable component first.
UrlAddressableAura.cmp
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable,force:appHostable" access="global" > <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:handler name="change" value="{!v.pageReference}" action="{!c.reInit}"/> <aura:attribute type="id" name="recordId" /> <c:myWebComponent recordId="{!v.recordId}" /> </aura:component>
UrlAddressableAuraController.js
({ doInit: function(cmp, evt, helper) { var myPageRef = cmp.get("v.pageReference"); var recordId = myPageRef.state.c__recordId; cmp.set("v.recordId", recordId); }, reInit : function(component, event, helper) { $A.get('e.force:refreshView').fire(); } })
Notes on the above code:
I have added my lightning web component in the aura component named
myWebComponent
.
As of now, Summer 20 release, lightning web components are not URL
addressable directly. So you need to wrap it in aura component.
You can pass the URL parameters also, like
c__recordId
, just
make sure that you append c__
before the name of the
parameters.
You can get the URL parameters in when the component is loaded, see the
doInit
method.
I have added
pagePeference
change handler reInit to refresh the
component.
Finally, create a formula field and link it with the component.
HYPERLINK("/lightning/cmp/c__yrlAddressableAura?c__recordId="+Id, "Label for link", "_self")
I have used the HYPERLINK function to create a link. This function has
three parameters, the related / full path, link label, and the target.
Also, you can set the label from other fields' values.
Happy learning!!
Hi Rahul,
ReplyDeleteThis is very helpful for me and also happy learning.
Thanking you,
Regards,
D.Achyuth
I am glad to know that it is helpful for you!!
DeleteHi Rahul,
ReplyDeleteThis really helped keep up the good work.
You are welcome Avinash!
Deletethanks helped a lot
ReplyDeleteYou are welcome dear Unknown.
DeleteI want to open my LWC within the same page where my formula field hyperlink is present.But its opening as new tab.Any idea on that
ReplyDeleteHi VK, for that you need to pass "_self" value in the third parameter of the hyperlink function. Check the documents here https://help.salesforce.com/articleView?id=sf.tips_for_using_hyperlink_formula_fields.htm&type=5
Deletedespite having self, its not opening on QLE cart page itself.You can try having an hyperlink in salesforce CPQ QLE cart
DeleteOhh didn't know about that, may be that's something restricted in CPQ OLE cart which I am not familiar with
DeleteIt helped me Rahul... Thanks for this post
ReplyDeleteHi Rahul
ReplyDeleteThank you and it helped me.
Do you know if there is a way to open it in a popup window. As we cannot call JavaScript from formula I was trying to use VF page but it did not work for me.
Please let me know if there is a way to open it as a popup window.
Thank You.
Hi Kunal, It's not possible. If you want to open it in a pop-up then I would suggest to use the quick action here.
Delete