I created LWC Button and when click on button if record has ParentId the it will fetch the dtails other wise it will not fetch any details.
I am unable to get Id of record in console and
LWC
`import { LightningElement,api, wire,track } from 'lwc';
import updateParentCase from '@salesforce/apex/JSbuttons.updateParentCase'
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class JSbuttons extends LightningElement {
@api recordId
@track cases
handleclick()
{
console.log('recordid is'+this.recordId)
updateParentCase({recordId:this.recordId})
.then((result)=>{
this.cases=JSON.stringify(result)
console.log('result is'+JSON.stringify(result))
console.log('id is'+JSON.stringify(result)[0].Id)
console.log('Id is'+this.cases[0].Id)
if(this.cases[0].Id == null)
{
const toastevent2=new ShowToastEvent(
{
title:'error',
message:'This case does not have any parent associated.',
variant:'error'
}
)
this.dispatchEvent(toastevent2)
}
else
{
console.log(this.recordId);
console.log('result is'+JSON.stringify(result))
const toastevent1=new ShowToastEvent(
{
title:'Success',
message:'Mother Case Updated Successfully.',
variant:'success'
}
)
this.dispatchEvent(toastevent1)
}
})
.catch((error)=>{
const toastevent=new ShowToastEvent(
{
title:'error',
message:'An Error has Occurred.'+error.message,
variant:'error'
}
)
this.dispatchEvent(toastevent)
})
}
}
`
Apex class
public class JSbuttons {
@AuraEnabled
public static List<Case> updateParentCase(string recordId){
system.debug('record id is'+recordId);
case updateCase = new Case();
System.debug('Get the case record Id details'+recordId);
list<Case> c = [SELECT id,CaseNumber,ParentId,RtlyUptd__c from case where id =: recordId AND ParentId != null];
System.debug('Get the case details'+c);
List<case> tobeupdatedcases = new List<Case>();
if(c.size() >0){
for(case cc : c){
updateCase.id = cc.ParentId;
updateCase.RtlyUptd__c = 'Feedback Provided';
tobeupdatedcases.add(updateCase);
}
}
if(tobeupdatedcases.size() > 0){
system.debug(tobeupdatedcases);
update tobeupdatedcases;
}
return tobeupdatedcases;
}
}
I tried to fetch in console by using console.log('id is'+JSON.stringify(result)[0].Id) it is showing undefined but I can able to fetch record result is[{"Id":"5001w00000BNm****D","RtlyUptd__c":"Feedback Provided"}]