Skip to main content

Posts

Showing posts from March, 2022

Why do we need getter in LWC?

 LWC template/component does not allow expression or any manipulation or array expression. To solve this problem "Getter" is solution. It atomically bind data from Java scripts to template. Error =  Invalid expression {employee[0]} - LWC1038: Template expression doesn't allow computed property accesslwc  <div class="slds-m-around_medium">            {employee[0]} <p> The sum of {num1} and {num2} is {num1+num2}  </div> Component code < template >     < lightning-card title = "Getters in LWC" >         < div class = "slds-m-around_medium" >             {firstEmployee}             < p > The sum of {num1} and {num2} is {sumResults} </ p >         </ div >     </ lightning-card > </ template > J ava script code When employee gets updated then automatical...

Why do we need track decorator in LWC?

 LWC engine can track changes or value assigned to a field or primitive properties automatically, however if field/properties is array or object type then LWC will not be able track value change automatically.  That is when @track is helpful, this will tell LWC engine to observe changes to the properties of object or elements of array decorated with @track. Here is my component code We  can also achieve using spread operator, the key here is instead of mutating same object/array we can create copy and make changes. LWC framework observes value assigned to field/property and if new value !=== previous value then component re-renders. this.address = {...this.address,"city":event.target.value} < template >     < lightning-card title = "2 way Data Binding" >         < div class = "slds-m-around_medium" >             < lightning-input type = "text" label = "Enter course Name"     ...

How to retrieve populated fields from a sObject ?

 As part of summer 16, the sObject method getPopulatedFieldsAsMap() was released. This method will return a map of populated field names and their corresponding values. The map contains only the fields that have been populated in memory for the SObject instance. https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getPopulatedFieldsAsMap Here are certain use cases to  this method. 1.   Fields are queried in the SOQL Statement Account  acc = [ SELECT  id,name,phone, Gender__pc   From   Account                 WHERE  id =  '0011F00000zJIvwQAG' ]; System .debug( 'acc->' +acc); Map < String ,  Object > accMap = acc.getPopulatedFieldsAsMap(); system .debug( 'Fields name-->' +accMap.keySet()); for  ( String  fieldName : accMap.key...