0

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"}]

Sfdc_123
  • 1
  • 1

1 Answers1

0

In this case your log will always return undefined because you are trying to access an object property of a string.

Notice that first you are stringifying the result variable with JSON.stringify(result) and then you are trying to access the property of the object.

You should just try to access the object property directly without JSON.stringify: console.log(result[0].Id). Or when using JSON.stringify you should include the property within the parenthesis: console.log(JSON.stringify(result[0].Id));

On another note, you shouldn't stringify the result at all before assigning it to this.cases.

Your LWC should look something like this:

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=result
            console.log('result is', result)            
            console.log('id is', result[0].Id)

            if(!this.cases[0].Id) // We are assuming that this.cases will always have an object at index 0, what if the array is empty? This should be handled as well.
            {

                const toastevent2=new ShowToastEvent(
                    {
                        title:'error',
                        message:'This case does not have any parent associated.',
                        variant:'error'
                    }
                )
                this.dispatchEvent(toastevent2)
            
            }
            else
            {
            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)
        })
        
        
    }
}
Tyler Edwards
  • 184
  • 2
  • 9