Advertisement
airton-junior

NavigationMixin

Apr 12th, 2023 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.04 KB | None | 0 0
  1. https://sfwiseguys.wordpress.com/2020/11/15/lwc-navigation/
  2.  
  3. Let’s jump into the code
  4. To use the Navigation Service in your LWC , import it in your JS file-
  5.  
  6. 1
  7. import { NavigationMixin } from 'lightning/navigation';
  8. And apply the NavigationMixin function to your Component’s base class –
  9.  
  10. 1
  11. 2
  12. export default class My_lwc extends NavigationMixin(LightningElement) {
  13. }
  14. The NavigationMixin adds two APIs to your component’s class. Since these APIs are methods on the class, they must be invoked with this reference.
  15.  
  16. [NavigationMixin.Navigate](pageReference, [replace]): A component calls this[NavigationMixin.Navigate] to navigate to another page in the application.
  17. [NavigationMixin.GenerateUrl](pageReference): A component calls this[NavigationMixin.GenerateUrl] to get a promise that resolves to the resulting URL. The component can use the URL in the href attribute of an anchor. It can also use the URL to open a new window using the window.open(url) browser API.
  18. Navigation Service Examples
  19. RECORD PAGES :
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. 11
  31. 12
  32. 13
  33. 14
  34. 15
  35. 16
  36. 17
  37. 18
  38. 19
  39. 20
  40. 21
  41. 22
  42. 23
  43. 24
  44. 25
  45. 26
  46. 27
  47. 28
  48. 29
  49. 30
  50. 31
  51. 32
  52. 33
  53. 34
  54. // Navigate to View Account Record Page
  55. navigateToViewAccountPage() {
  56. this[NavigationMixin.Navigate]({
  57. type: 'standard__recordPage',
  58. attributes: {
  59. recordId: this.yourRecordId,
  60. objectApiName: 'Account',
  61. actionName: 'view'
  62. },
  63. });
  64. }
  65. // Navigate to Edit Account Record Page
  66. navigateToEditAccountPage() {
  67. this[NavigationMixin.Navigate]({
  68. type: 'standard__recordPage',
  69. attributes: {
  70. recordId: this.yourRecordId,
  71. objectApiName: 'Account',
  72. actionName: 'edit'
  73. },
  74. });
  75. }
  76. // Navigate to Clone Account Record Page
  77. navigateToCloneAccountPage() {
  78. this[NavigationMixin.Navigate]({
  79. type: 'standard__recordPage',
  80. attributes: {
  81. recordId: this.yourRecordId,
  82. objectApiName: 'Account',
  83. actionName: 'clone'
  84. },
  85. });
  86. }
  87. //Communities don’t support the actionName values clone or edit.
  88. 2. OBJECT PAGES –
  89.  
  90. 1
  91. 2
  92. 3
  93. 4
  94. 5
  95. 6
  96. 7
  97. 8
  98. 9
  99. 10
  100. 11
  101. 12
  102. 13
  103. 14
  104. 15
  105. 16
  106. 17
  107. 18
  108. 19
  109. 20
  110. 21
  111. 22
  112. 23
  113. 24
  114. 25
  115. 26
  116. 27
  117. 28
  118. 29
  119. 30
  120. 31
  121. 32
  122. 33
  123. 34
  124. 35
  125. 36
  126. 37
  127. 38
  128. 39
  129. 40
  130. 41
  131. 42
  132. 43
  133. 44
  134. 45
  135. 46
  136. 47
  137. 48
  138. 49
  139. 50
  140. 51
  141. 52
  142. 53
  143. 54
  144. 55
  145. 56
  146. 57
  147. 58
  148. 59
  149. 60
  150. 61
  151. 62
  152. 63
  153. 64
  154. 65
  155. 66
  156. 67
  157. 68
  158. 69
  159. 70
  160. 71
  161. 72
  162. 73
  163. 74
  164. 75
  165. 76
  166. 77
  167. 78
  168. 79
  169. 80
  170. 81
  171. // Navigate to New Account Page
  172. navigateToNewAccountPage() {
  173. this[NavigationMixin.Navigate]({
  174. type: "standard__objectPage",
  175. attributes: {
  176. objectApiName: "Account",
  177. actionName: "new"
  178. },
  179. });
  180. }
  181.  
  182. // Navigation to Account List view(recent)
  183. navigateToAccountRecentListView() {
  184. this[NavigationMixin.Navigate]({
  185. type: "standard__objectPage",
  186. attributes: {
  187. objectApiName: "Account",
  188. actionName: "list"
  189. },
  190. state: {
  191. filterName: "Recent"
  192. },
  193. });
  194. }
  195. //filterName = StringID or developer name of object"s list view.
  196.  
  197.  
  198. // Navigate to a New Account page with default field values:
  199. // Name: Salesforce,
  200. // OwnerId: 005XXXXXXXXXXXXXXX,
  201. // AccountNumber: ACXXXX,
  202. // NumberOfEmployees: 35000
  203. navigateToAccountDefault() {
  204. this[NavigationMixin.Navigate]({
  205. type: “standard__objectPage”,
  206. attributes: {
  207. objectApiName: "Account",
  208. actionName: "new"
  209. },
  210. state: {
  211. defaultFieldValues = "AccountNumber=ACXXXX,Name=Salesforce,NumberOfEmployees=35000,OwnerId=005XXXXXXXXXXXXXXX",
  212. nooverride: "1"
  213. }
  214. });
  215. }
  216.  
  217.  
  218. // Navigation to Case object home page
  219. navigateToCaseHome() {
  220. this[NavigationMixin.Navigate]({
  221. type: "standard__objectPage",
  222. attributes: {
  223. objectApiName: "Case",
  224. actionName: "home"
  225. }
  226. });
  227. }
  228.  
  229.  
  230. //Navigate to Reports Standard Tab
  231. navigateToReports() {
  232. this[NavigationMixin.Navigate]({
  233. type: "standard__objectPage",
  234. attributes: {
  235. objectApiName: "Report",
  236. actionName: "home"
  237. },
  238. });
  239. }
  240. //Navigate to Files Home/Standard tab
  241. navigateToFilesHome() {
  242. this[NavigationMixin.Navigate]({
  243. type: "standard__objectPage",
  244. attributes: {
  245. objectApiName: "ContentDocument",
  246. actionName: "home"
  247. },
  248. });
  249. }
  250. //In communities, actionName = list and home are the same.
  251. //In managed package, prefix the custom object with ns__
  252. 3. RECORD RELATIONSHIP PAGE –
  253. A page that interacts with a relationship on a particular record in the org. Only related lists are supported.
  254.  
  255. 1
  256. 2
  257. 3
  258. 4
  259. 5
  260. 6
  261. 7
  262. 8
  263. 9
  264. 10
  265. 11
  266. 12
  267. 13
  268. 14
  269. // Navigation to Contact related list of Account
  270. navigateToContactRelatedList() {
  271. this[NavigationMixin.Navigate]({
  272. type: 'standard__recordRelationshipPage',
  273. attributes: {
  274. recordId: this.recordId,
  275. objectApiName: 'Account',
  276. relationshipApiName: 'Contacts',
  277. actionName: 'view'
  278. },
  279. });
  280. }
  281. //actionName = Only view is supported.
  282. //relationshipApiName = The API name of the object’s relationship field.
  283. 4. APP PAGES –
  284.  
  285. 1
  286. 2
  287. 3
  288. 4
  289. 5
  290. 6
  291. 7
  292. 8
  293. 9
  294. 10
  295. 11
  296. 12
  297. 13
  298. 14
  299. 15
  300. 16
  301. 17
  302. 18
  303. 19
  304. 20
  305. 21
  306. 22
  307. 23
  308. 24
  309. 25
  310. 26
  311. 27
  312. 28
  313. 29
  314. 30
  315. 31
  316. 32
  317. 33
  318. 34
  319. 35
  320. 36
  321. 37
  322. 38
  323. 39
  324. 40
  325. 41
  326. 42
  327. //Navigate to a Standard App
  328. navigateToSalesApp() {
  329. this[NavigationMixin.Navigate]({
  330. type: 'standard__app',
  331. attributes: {
  332. appTarget: 'standard__Sales',
  333. }
  334. });
  335. }
  336.  
  337. //Navigate to a Custom App
  338. navigateToMyCustomApp() {
  339. this[NavigationMixin.Navigate]({
  340. type: 'standard__app',
  341. attributes: {
  342. appTarget: 'c__MyCustomApp',
  343. }
  344. });
  345. }
  346. // Pass either the appId or appDeveloperName to the
  347. // appTarget. The appId is the DurableId field on the AppDefinition object.
  348. // For standard apps, the namespace is standard__. For custom
  349. // apps, it’s c__. For managed packages, it’s the namespace
  350. // registered for the package.
  351.  
  352. //Navigate to App Record Page in an App
  353. navigateToAppRecordPage() {
  354. this[NavigationMixin.Navigate]({
  355. type: 'standard__app',
  356. attributes: {
  357. appTarget: 'standard__LightningSales',
  358. pageRef: {
  359. type: 'standard__recordPage',
  360. attributes: {
  361. recordId: '001xx000003DGg0XXX',
  362. objectApiName: 'Account',
  363. actionName: 'view'
  364. }
  365. }
  366. }
  367. });
  368. }
  369. 5. LIGHTNING COMPONENT PAGES :
  370.  
  371. To make an addressable LWC, embed it in an Aura component that implements the lightning:isUrlAddressable interface.
  372.  
  373. 1
  374. 2
  375. 3
  376. 4
  377. 5
  378. 6
  379. 7
  380. 8
  381. 9
  382. 10
  383. 11
  384. 12
  385. 13
  386. 14
  387. 15
  388. 16
  389. 17
  390. // Navigate to Lightning Component with Params
  391. navigateToLC() {
  392. this[NavigationMixin.Navigate]({
  393. type: "standard__component",
  394. attributes: {
  395. //Here myCustomAura is name of Aura component
  396. //which implements lightning:isUrlAddressable
  397. componentName: "c__myCustomAura"
  398. },
  399. state: {
  400. c__counter: '5',
  401. c__recId: 'XXXXXXXXXXXXXXXXXX'
  402. }
  403. });
  404. }
  405. //You can pass any key and value in the state object. The key
  406. //must include a namespace, and the value must be a string.
  407. In Aura component, retrieve the sent params using component.get in the init controller method –
  408.  
  409. 1
  410. 2
  411. component.get("v.pageReference").state.c__counter);
  412. component.get("v.pageReference").state.c__recId);
  413. Retrieval of params in LWC is described in a detailed section below.
  414.  
  415. 6. CUSTOM TABS :
  416.  
  417. navItemPage – A page that displays the content mapped to a custom tab. Visualforce tabs, Web tabs, Lightning Pages, and Lightning Component tabs are supported.
  418.  
  419. 1
  420. 2
  421. 3
  422. 4
  423. 5
  424. 6
  425. 7
  426. 8
  427. 9
  428. 10
  429. 11
  430. 12
  431. 13
  432. // Navigate to Custom Tab - VF tabs, Web tabs, Lightning Pages, and Lightning Component Tabs
  433. navigateToTab() {
  434. this[NavigationMixin.Navigate]({
  435. type: 'standard__navItemPage',
  436. attributes: {
  437. apiName: 'MyCustomTabAPIName'
  438. },
  439. state: {
  440. c__counter: '5',
  441. c__recId: this.recId
  442. }
  443. });
  444. }
  445. 7. WEB PAGES
  446.  
  447. 1
  448. 2
  449. 3
  450. 4
  451. 5
  452. 6
  453. 7
  454. 8
  455. 9
  456. 10
  457. 11
  458. 12
  459. 13
  460. 14
  461. 15
  462. 16
  463. 17
  464. 18
  465. 19
  466. 20
  467. 21
  468. // Navigate to an External URL
  469. navigateToWebPage() {
  470. this[NavigationMixin.Navigate]({
  471. type: 'standard__webPage',
  472. attributes: {
  473. url: 'http://salesforce.com'
  474. }
  475. });
  476. }
  477. //Navigate to a Visualforce page
  478. navigateToVF() {
  479. this[NavigationMixin.GenerateUrl]({
  480. type: 'standard__webPage',
  481. attributes: {
  482. url: '/apex/myVFPage?id=' + this.recId
  483. }
  484. }).then(generatedUrl => {
  485. window.open(generatedUrl);
  486. });
  487. }
  488. // Here we use NavigationMixin.GenerateUrl to form the URL and then navigate to it in the promise.
  489. 8. KNOWLEDGE ARTICLE :
  490.  
  491. 1
  492. 2
  493. 3
  494. 4
  495. 5
  496. 6
  497. 7
  498. 8
  499. 9
  500. 10
  501. 11
  502. 12
  503. // Navigate to Knowledge Article record
  504. navigateToKnowledgeArticle() {
  505. this[NavigationMixin.Navigate]({
  506. type: 'standard__knowledgeArticlePage',
  507. attributes: {
  508. articleType: 'MyArticleType',
  509. urlName: 'Mar-2020'
  510. }
  511. });
  512. }
  513. //The articleType is API name of the Knowledge Article record.
  514. //The urlName is the article's URL.
  515. 9. NAMED PAGES (standard) & FILE PREVIEW
  516.  
  517. 1
  518. 2
  519. 3
  520. 4
  521. 5
  522. 6
  523. 7
  524. 8
  525. 9
  526. 10
  527. 11
  528. 12
  529. 13
  530. 14
  531. 15
  532. 16
  533. 17
  534. 18
  535. 19
  536. 20
  537. 21
  538. 22
  539. 23
  540. 24
  541. 25
  542. 26
  543. 27
  544. // Navigate to Named Pages - A standard page with a unique name.
  545. navigateToNamedPage() {
  546. this[NavigationMixin.Navigate]({
  547. type: 'standard__namedPage',
  548. attributes: {
  549. pageName: 'home'
  550. }
  551. });
  552. }
  553. // pageName possible Values - home , chatter, today, dataAssessment, filePreview
  554.  
  555.  
  556. // Navigate to filePreview Page
  557. navigateToFilePreview() {
  558. this[NavigationMixin.Navigate]({
  559. type: "standard__namedPage",
  560. attributes: {
  561. pageName: "filePreview"
  562. },
  563. state: {
  564. // assigning single ContentDocumentId
  565. selectedRecordId: this.id
  566. }
  567. });
  568. }
  569. // recordIds: '069xx0000000005AAA,069xx000000000BAAQ',
  570. // selectedRecordId:'069xx0000000005AAA'
  571. 10. NAMED PAGES & LOGIN PAGE (community)
  572.  
  573. 1
  574. 2
  575. 3
  576. 4
  577. 5
  578. 6
  579. 7
  580. 8
  581. 9
  582. 10
  583. 11
  584. 12
  585. 13
  586. 14
  587. 15
  588. 16
  589. 17
  590. 18
  591. 19
  592. 20
  593. 21
  594. 22
  595. // Navigate to standard page used in Lightning communities
  596. navigateToCommPage() {
  597. this[NavigationMixin.Navigate]({
  598. type: 'comm__namedPage',
  599. attributes: {
  600. name: 'Home'
  601. }
  602. });
  603. }
  604. // Supported pages in communities - Home, Account Management, Contact Support,
  605. // Error, Login, My Account, Top Articles, Topic Catalog, Custom page
  606.  
  607. // Navigate to Authentication page in Lightning communities
  608. navigateToLoginPage() {
  609. this[NavigationMixin.Navigate]({
  610. type: 'comm__loginPage',
  611. attributes: {
  612. actionName: 'login'
  613. }
  614. });
  615. }
  616. // Supported actionName = login, logout
  617. Now that we have seen what all kinds of Navigation scenarios are provided, let’s also look at another feature, which is the URL generation done by NavigationMixin.GenerateUrl , as shown below-
  618.  
  619. 1
  620. 2
  621. 3
  622. 4
  623. 5
  624. 6
  625. 7
  626. 8
  627. 9
  628. 10
  629. 11
  630. 12
  631. 13
  632. 14
  633. 15
  634. 16
  635. recordPageUrl; // variable to be associated to anchor tag.
  636.  
  637. // Generate a URL to a User record page
  638. generateURLforLink() {
  639. this[NavigationMixin.GenerateUrl]({
  640. type: 'standard__recordPage',
  641. attributes: {
  642. recordId: '005B0000001ptf1XXX',
  643. actionName: 'view',
  644. },
  645. }).then(generatedUrl => {
  646. this.recordPageUrl = generatedUrl;
  647. });
  648. }
  649. // NavigationMixin.GenerateUrl returns the Generated URL in the promise.
  650. // We can even use this in window.open(generatedUrl) command
  651. Retrieving Params in LWC
  652. We use CurrentPageReference to get a reference to the current page in Salesforce. Page URL formats can change in future releases. Hence, to future proof your apps, use page references instead of URLs.
  653.  
  654. 1
  655. 2
  656. 3
  657. import { CurrentPageReference } from 'lightning/navigation';
  658. @wire(CurrentPageReference)
  659. pageRef;
  660. The key-value pairs of the PageReference state property are serialized to URL query parameters.
  661.  
  662. Example of an LWC encoding defaultFieldValues in state and then navigating to another LWC and decoding them –
  663.  
  664. 1
  665. 2
  666. 3
  667. 4
  668. 5
  669. 6
  670. 7
  671. 8
  672. 9
  673. 10
  674. 11
  675. 12
  676. 13
  677. 14
  678. 15
  679. 16
  680. 17
  681. 18
  682. 19
  683. 20
  684. 21
  685. 22
  686. 23
  687. 24
  688. 25
  689. // navSenderLWC.js
  690. import { LightningElement } from 'lwc';
  691. import { NavigationMixin } from 'lightning/navigation';
  692. import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
  693.  
  694. export default class navSenderLWC extends NavigationMixin(LightningElement) {
  695.  
  696. navigateWithParams() {
  697. const encodedValues = encodeDefaultFieldValues({
  698. FirstName: 'Waseem',
  699. LastName: 'Sabeel'
  700. });
  701.  
  702. this[NavigationMixin.Navigate]({
  703. type: 'standard__objectPage',
  704. attributes: {
  705. objectApiName: 'Contact',
  706. actionName: 'new'
  707. },
  708. state: {
  709. defaultFieldValues: encodedValues
  710. }
  711. });
  712. }
  713. }
  714. And we use the Wired PageReference on the navigated LWC, and decode the field values as below –
  715.  
  716. 1
  717. 2
  718. 3
  719. 4
  720. 5
  721. 6
  722. 7
  723. 8
  724. 9
  725. 10
  726. 11
  727. 12
  728. 13
  729. 14
  730. // navReceiverLWC.js
  731. import { LightningElement, wire } from 'lwc';
  732. import { CurrentPageReference } from 'lightning/navigation';
  733. import { decodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
  734.  
  735. export default class navReceiverLWC extends LightningElement {
  736.  
  737. @wire(CurrentPageReference)
  738. setCurrentPageRef(pageRef) {
  739. if (pageRef.state.defaultFieldValues) {
  740. const decodedValues = decodeDefaultFieldValues(pageRef.state.defaultFieldValues);
  741. }
  742. }
  743. }
  744. Note – All encoded default field values in the state are passed as strings.
  745.  
  746. So that was all about Lightning Navigation Scenarios and Examples.
  747.  
  748. A Note on Limitations –
  749.  
  750. The lightning/navigation service is supported only in Lightning Experience, Lightning communities, and the Salesforce app. It isn’t supported in other containers, such as Lightning Components for Visualforce, or Lightning Out. This is true even if you access these containers inside Lightning Experience or the Salesforce app.
  751.  
  752. For more detailed information , kindly refer to the Official Documentation provided here –
  753.  
  754. All PageReference Types with attribute details - https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_page_reference_type
  755. Developer Guide - https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_navigate
  756. lightning-navigation Component reference - https://developer.salesforce.com/docs/component-library/bundle/lightning-navigation/documentation
  757. Thank You!
  758.  
  759.  
  760.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement