0% found this document useful (0 votes)
546 views10 pages

LWC Interview

Salesforce interview questions

Uploaded by

Md Muffasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
546 views10 pages

LWC Interview

Salesforce interview questions

Uploaded by

Md Muffasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Real Scenario-Based

Questions on Lightning Web


Components (LWC)...
1. Can we call the @AuraEnabled function in the
apex class using wire ?

Ans: Function also needs to have cacheable = true


annotation ie should be like
@AuraEnabled(cacheable = true)

2. What do you mean by cacheable = true


annotations ?

Ans: First of all when you mark function as


cacheable = true it can only retrieve data i.e. it cant
have any DML.
It is used to improve component performance by
quickly showing cached data from client side
storage without waiting for server trip.
Remember to refresh the stale data provisioned by
Apex we have to use refreshApex as LDS doesn’t
manage data coming from Apex ( In case of wired
service)
3. @wire(getBoats,{boatTypeId :
this.boatTypeId})
getBoats(error,data){}
Will the above code deploy? (Assuming all
variables and apex class function exists).

Ans: No it wont when passing a variable in wire you


should always use $ along with variable, it should
be written like
@wire(getBoats,{boatTypeId : ‘$boatTypeId})

4. Why do we use $ when passing property in


wire function, what does it mean?

Ans: $ prefix tells the wire service to treat values


passed as a property of the class and evaluate it as
this.propertyName and the property is reactive. If
the property’s value changes, new data is
provisioned and the component renders.
5. See the below code and answer the following
question: [Scenario based Salesforce Lightning
Interview question with relevant answer]

If I want to refresh the wired data in the above


function, can I call refreshApex(this.someVar) ?
@wire(getBoats,{boatTypeId : ‘$boatTypeId’})
getBoats({error,data}){
if(data){
this.someVar = data;
this.error = undefined;
}
else if(error){
this.error = error;
this.someVar = undefined ;
}
}
Ans:

No we cant call refreshApex(this.someVar) rather


refreshApex is called on whole result provisioned
from the wire service not just the data part, we will
have to rewrite it as below :

@wire(getBoats,{boatTypeId : ‘$boatTypeId’})
getBoats(result){
this.mainResult = result;
if(result.data){
this.someVar = result.data;
this.error = undefined;
}
else if(result.error){
this.error = result.error;
this.someVar = undefined ;
}
}
Now we can refresh data as
refreshApex(this.mainResult)
6. Can we call a wire function inside a
javascript function like below :
searchBoats(event){
@wire(getBoats,{boatTypeId: ‘$boatTypeId’}
getBoats(data,error){
}
}
Assume searchBoats is being called on click of
button? Will I be able to deploy the code ?

Ans: No you cant call it like this , code will not


deploy. You will receive error as leading
decorators must be attached to class which
means decorators like wire can be directly under
class only not inside any other function.
Similarly if you try to define variable with @track
or api decorator inside a function it will fail.
7. When is the wire method/property called in
the lifecycle of a component ?
Ans: Wired service is called right after component
is created ie after constructor and is again called
when parameter that you are passing is made
available.

8. Is it mandatory to use data,error only in wired


method, can I use some other variable like
below

@wire(getBoats)
getBoats({myData,myError}){
if(mydata)
console.log(‘i am in data’);
else if(myError)
console.log(‘I am in error’); }
Will the code deploy successfully or I will receive
an error ?
9. What is the difference in below two codes ,
will they both compile ? Give same results ?

Code 1 :
@wire(getBoats)
getBoats({data,error}){
if(data)
console.log(‘print here’);
Else if(error)
console.log(‘print in else’);
}
@wire(getBoats,{})
getBoats({error,data}){
if(data)
console.log(‘print here’);
Else if(error)
console.log(‘print in else’);
}
Ans: Both will compile they are same.
10. What is the difference between
event.StopPropogation() and
Event.preventDefault()?
Ans: stopPropagation prevents further
propagation of the current event in the capturing
and bubbling phases. preventDefault prevents the
default action the browser makes on that event.

11. If we add required attribute in lightning-input


, will I get an error on leaving the field empty ?
Ans: No unless you also add logic in backend
javascript file to actually throw an error using
checkValidity and reportValidity.

12. Are quick actions supported for LWC


components?
Ans: Quick actions are supported by LWC in
Summer 21 or later orgs. LWC quick actions are
only supported on record pages.
Join real-time interview-ready
Salesforce training with latest
interview questions and
answers, Enroll today for free
demo!

+91 98604 38743

We Will cover Admin +


Development + LWC + CPQ

You might also like