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
Nishant Shrivastava 30Nishant Shrivastava 30 

getting value of Id from @wire method

Hi, Please check, there is a wireMethod which returned the value from apex
@wire(fetchRecord, {searchKey:'$searchKey', selectedLabel:'$selectedLabel'})recorddetails;

There is an Id value in recorddetails. I need to pass this value of Id in another variable for another invocation.
Could you please mention, how can i retrieve the value of recorddetails.Id

I shall be grateful to you for your kind cooperation.

Thanks and regards
@wire(fetchRecord, {searchKey:'$searchKey', selectedLabel:'$selectedLabel'})recorddetails;

 
Best Answer chosen by Nishant Shrivastava 30
Nishant Shrivastava 30Nishant Shrivastava 30
find out

 const searchKey = event.target.value;//this.template.querySelector('lightning-input').value;
        this.searchKey = searchKey;
        this.parentId1 = JSON.stringify(this.recorddetails.data);
       this.parentId1 = JSON.parse(this.parentId1.trim())//this.parentId1.trim();
        this.parentId = this.parentId1[0].Id;
        console.log('this.parentId---->', this.parentId);

All Answers

PINKY REGHUPINKY REGHU
Hi Nishant Shrivastava 30,

Please paste the apex code for the method as well.

 
Nishant Shrivastava 30Nishant Shrivastava 30

Thanks Pinky for your kind response. Please check all the code

 

Apex Code::

public with sharing class MoveAttachment {
    Public string getId {get;set;}
    @AuraEnabled(cacheable=true)
    public static List<EntityDefinition> methodName(){
        List<EntityDefinition> ed = new List<EntityDefinition>([SELECT id, QualifiedApiName, Label FROM EntityDefinition where IsCustomizable = true]);
        return ed;
    }
    
    @AuraEnabled(cacheable=true)
    public static List<sobject> fetchRecord(string searchKey, string selectedLabel){
        string key = '%'+searchkey+'%';
        String queryStr='select id, Name from ' + selectedLabel+' where Name Like :  key';
        system.debug(key);
        system.debug(selectedLabel);
        List<sobject> recorddetails = Database.query(queryStr);
        system.debug(recorddetails);
        return recorddetails;
    }
    
    @AuraEnabled(cacheable=true)
    public static List<Attachment> getAttachment(string parentId){
        List<Attachment> attachmentDetails = [select id, name from Attachment where parentId =: parentId];
        return attachmentDetails;
    }
    
    
}




Html Code :


<template>
    <lightning-card title="Source Object" icon-name="custom:custom67">
    <template  if:true={options}>
    <lightning-combobox
            name="List of Object"
            label="List of Object"
            placeholder="Select Object"
            options = {options}
            onchange={handleChange} >
        </lightning-combobox>
    <p>Selected value is: {selectedLabel}</p>
</template>
</lightning-card>
<lightning-card title="Record Details" icon-name="custom:custom63">
    <div class="slds-m-around_medium">
        <lightning-input type="search" onchange={handleKeyChange} class="slds-m-bottom_small" label="Search" value={searchKey}></lightning-input>
        <template if:true={recorddetails.data}>
            <template for:each={recorddetails.data} for:item="datas">
                <p key={datas.Id}>{datas.Name}</p>
            </template>
        </template>
        <lightning-button variant="success" label="Show Attachment" title="Successful action" onclick={optionss} class="slds-m-left_x-small"></lightning-button>
    </div>
</lightning-card>
<lightning-card title="Attachments" icon-name="custom:custom100">
    <div class="slds-m-around medium">
            <lightning-checkbox-group name="Checkbox Group"
                                      label="List of Attachment"
                                      options={attachmentDetails.data}
                                      value={values}
                                      onchange={handleChanges}></lightning-checkbox-group>
    </div>
</lightning-card>
</template>



Javascript code : commented included too

import { LightningElement, wire, track } from 'lwc'; 
import methodName from '@salesforce/apex/MoveAttachment.methodName';
import fetchRecord from '@salesforce/apex/MoveAttachment.fetchRecord';
import getAttachment from '@salesforce/apex/MoveAttachment.getAttachment';


export default class attachmentMove extends LightningElement {
    record= [];
    records= [];
    recordss=[];
    resultss=[];
    result= [];
    results= [];
    error= [];
    data = [];
    errors= [];
    datas = [];
    errorss= [];
    datass = [];
    dataa = [];
    eror;
    datasss =[]
    @track parentId=[];
    @track searchKey;
    @track selectedLabel;
    @track parameterObject;
    @track recordId;
    @wire(methodName)
    wiredsobjectName({error, data}){
        if(data){
            if(data.length>0){
                for(let i=0;i<data.length;i++)
            {
                this.record.push ({label: data[i].Label, value: data[i].QualifiedApiName });
                console.log('check=====>', this.record[i]);
            }
        }
            this.result = this.record;
        } else if (error){
            this.error = error;
            this.result = undefined;
        }
    }
    get options() {
        console.log('option',this.result);
        return this.result;
    }
    
    handleChange(event) {
        this.selectedLabel = event.detail.value;
    }
    
    @wire(fetchRecord, {searchKey:'$searchKey', selectedLabel:'$selectedLabel'})recorddetails;
    @wire(getAttachment, {parentId:'$parentId'})attachmentDetails;
    get parentIds(){
        this.parentId = this.recorddetails.data.id;
        console.log('parentId---->', this.parentId);
        return this.recorddetails.data.Id;
    }

    get optionss(){
        return this.attachmentDetails.data;
    }
    get values(){
        return this.attachmentDetails.data;
    }
    get selectedValues(){
        return this.values.join(',');
    }

    handleChanges(e){
        this.value = e.detail.value;
        
    }

    handleKeyChange(event) {
        const searchKey = event.target.value;//this.template.querySelector('lightning-input').value;
        this.searchKey = searchKey;
        //this.parentId.push({key : this.recorddetails.Id});
        this.parentId = this.recorddetails.data;
        
        console.log('this.parentId---->', this.parentId);
        return this.recorddetails;
    }

}

/*
@wire(fetchRecord)
    wiredRecordDetails({errors, datas}){
        if(datas){
            if(datas.length>0){
                for(let i=0;i<datas.length;i++)
            {
                this.records.push ({label: datas[i].Name, value: datas[i].Id });
                console.log('check record=====>', this.records[i]);
                this.parentId = datas[i].Id;
                console.log('this.parentId---->', this.parentId);
            }
        }
            this.results = this.records;
        } else if (errors){
            this.errors = errors;
            this.results = undefined;
        }
    }
    @wire(getAttachment)
    wiredAttachmentDetails({errorss, datass}){
        if(datass){
            if(datass.length>0){
                for(let i=0;i<datass.length;i++)
            {
                this.recordss.push ({label: datass[i].Name, value: datass[i].Id });
                console.log('check attachment=====>', this.recordss[i]);
                this.parentId = datass[i].Id;
                console.log('parentId---->',this.parentId[0]);
            }
        }
            this.resultss = this.recordss;
        } else if (errorss){
            this.errorss = errorss;
            this.resultss = undefined;
        }
    }
    */




Actually it's providing value for recorddetails properly on html but I need to pass the recorddetails.Id value to the parentId so that i can retrieve related attachment.
Nishant Shrivastava 30Nishant Shrivastava 30
find out

 const searchKey = event.target.value;//this.template.querySelector('lightning-input').value;
        this.searchKey = searchKey;
        this.parentId1 = JSON.stringify(this.recorddetails.data);
       this.parentId1 = JSON.parse(this.parentId1.trim())//this.parentId1.trim();
        this.parentId = this.parentId1[0].Id;
        console.log('this.parentId---->', this.parentId);
This was selected as the best answer