function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JohnDuraiJohnDurai 

how to get record id afternavigation in LWC

Hi - I have created a LWC button to create case using Navigation and I placed in CampaignMember Object. my question here is from my scenario CamapiagnMember is child object and Case is Parent object, I have custom lookup (case__C) field in Camapaign member object. Now, from camapaign member record page i am clicking create case button and creating a case after saving it navigates case reocrd page, but after this i want to get this caserecord id and map it with case__C field in campaign object. is it possible to make or suggest some solution for this? Below is my JS. Thanks!

import { LightningElement, api } from 'lwc';
import {NavigationMixin} from 'lightning/navigation';
import {encodeDefaultFieldValues} from 'lightning/pageReferenceUtils';
import getCMRecord from '@salesforce/apex/HypercareCMManagement.getCMRecord';
//import Case_OBJECT from '@salesforce/schema/Case';
export default class CreateCaseHypercare extends NavigationMixin(LightningElement) {
    campaignMemberRecord;
    error;
    @api recordId;
    //***Navigate to Case Record Page***//
    handleNavigateToCase(){
        getCMRecord({recordID : this.recordId})
       
        .then(result => {
            this.campaignMemberRecord = result;
           
            console.log('caseRecord value is : ' +this.caseRecord);
            const defaultValueCase = encodeDefaultFieldValues({
                RecordTypeId:'0122v000001XBmvAAG',
                AccountId : this.campaignMemberRecord.AccountId__c,
                ContactId: this.campaignMemberRecord.ContactId
            })
            this[NavigationMixin.Navigate]({
                type:'standard__objectPage',
                attributes:{
                    objectApiName: 'Case',
                    actionName:'new'
               
                },
                state:{
                    defaultFieldValues: defaultValueCase
                }
               
             
            })
        })
        .catch(error => {
            this.error = error;
            console.log('error msg'+this.error);
        });

    }
 
}
SwethaSwetha (Salesforce Developers) 
HI JohnDurai ,

To get the Case record ID after creating it through navigation and map it to the case__c field in the Campaign Member object,you can try these steps:

- Create the Case record using navigation and capture the created Case's record ID.
 - Use an event or callback to pass the Case record ID back to the LWC.
- In your LWC, update the Campaign Member record with the Case record ID.

Related: https://salesforce.stackexchange.com/questions/374750/navigate-to-record-page-how-to-get-the-id-of-the-record
https://www.linkedin.com/pulse/how-get-record-id-your-lwc-when-you-want-place-component-kumari
https://www.greytrix.com/blogs/salesforce/2022/02/09/how-to-fetch-current-record-id-from-lightning-web-components-lwc/

Thanks