Concepts or coding lessons of Salesforce that you can implement easily

Lightning Web Component Interview Questions - Part 2

 11. Can I use multiple decorators for one property?

Ans:No, we cant use multiple decorators for same property.

12. Do we have application events in LWC?

Ans:We dont have application event as such in LWC like Aura rather we have LMS in LWC to communicate between components which are not part of same hierarchy.

13. How to navigate user from LWC component to record detail page?

Ans: We can do it using NavigationMixin service.

14. Get current user ID in LWC without apex?

Ans: Yes we can get current user ID without apex by simply importing

        import Id from ‘@salesforce/user/Id’

15. When do we face error of “Cant assign to Read only property” in LWC?

Ans: This error generally occurs on update of public property @api decorator variable value. Ideally you should clone the value then make the changes to it.

16. How can you make a Lightning Web Component available for use in the Salesforce App Builder?

Ans: You need to define a custom component in the meta.xml file of your LWC and set the isExposed attribute to true.

17. How do you make an HTTP callout from a Lightning Web Component?

Ans: You can use the fetch API to make HTTP callouts in LWC. Import the fetch method and use it to send requests to external services.

18. LWC has the @wire decorator to get the data from apex. But you are not getting the data from the server. What could be one of the reason?

Ans: First check whether you have used @AuraEnabled(cacheable=true) in the apex method.

19. How to get current record id in LWC

Ans: Use the 'lightning/pageReferenceUtils' module which provides utility functions to extract parameters, including the record ID, from the page URL.

        import { LightningElement } from ‘lwc’;

import { getRecordId } from ‘lightning/pageReferenceUtils’;

export default class MyComponent extends LightningElement {

recordId;

connectedCallback() {

this.recordId = getRecordId();

}

}

20. How to ensure that LWC respects FLS?

Ans: a. Check FLS in Apex :

Use the 'SECURITY_ENFORCED' annotation. The 'WITH SECURITY_ENFORCED' clause ensures that the query respects FLS, throwing an exception if the user lacks the necessary permissions.

public with sharing class AccountController {

@AuraEnabled(cacheable=true)

public static Contact getCon(Id conId) {

return [SELECT Id, Name FROM Contact WHERE Id = :conId WITH SECURITY_ENFORCED];

}

}

 b. Use Schema Methods in Apex

 c. Use Standard Components Where Possible: Use Lightning Data Service components


21. How do you use the Salesforce REST API in LWC ?

Ans: We can use the built-in fetch method to make API callout from LWC

import { LightningElement } from ‘lwc’;

export default class MyAPIComponent extends LightningElement {

connectedCallback() {

const endpoint = '/services/data/v53.0/query?q=SELECT+Name+FROM+Contact';

fetch(endpoint, {

method: 'GET',

headers: {

'Authorization': 'Bearer ' + authToken

}

})

.then(response => {

return response.json();

})

.then(data => {

console.log('Data '+data);

})

.catch(error => {

console.error('error '+error);

});

}


Subscribe blog for More updates !!! 



Read More: Lightning Web Component Interview Questions - Part 1
                    


Latest Salesforce Interview Questions and Answers:

loading...

No comments:

Post a Comment