Fix Error “Cannot assign to read only property” and print Proxy Object of LWC

Quick code snippet to read content of Proxy object in Lightning web component & resolve error Cannot assign to read only property

As its code-snippet post, its going to be very short but frequently used.

Salesforce decorator @track is reactive , which means if value in JavaScript changes, equivalent HTML UI would also change. You don’t need to mark primitive data types as track, as its enabled by default for them. However for complex structure / object we need to mark variable as @track.

Whenever any JavaScript object marked as @track, Salesforce wraps that object resulting in proxy object. If you try to print that on console, you would not be able to see its value.

That being said, there is no change in way you access it, it’s exactly like any other Javascript object we use. So, if you want to see content of proxy object, just call below method and pass your proxy object.

//Get Javascript object equivalent to Proxy
    proxyToObj(obj){
        return JSON.parse(JSON.stringify(obj));
    }

Another way is, just keep another variable without @track decorator to see content. However problem would be – you need to make sure to keep it in sync every time content of original @track object changes.

Fixing error – Cannot assign to read only property

Above error occurs when we try to change public property on LWC marked as @api

@api someVar
.... some code

somemethod(event){
    //error on below line - Cannot assign to read only property
     someVar.isSelected = true;
}

Above code block would throw an error as we are trying. to change property on object declared by @api

@api someVar
.... some code

somemethod(event){
    //clone @api variable using method we just created
    let tmpObj = this.proxyToObj(someVar);
     tmpObj.isSelected = true;
    //now assign this tmp variable back to original
   someVar = tmpObj;
}

To fix above error, use can do something like

Using Custom Label in Lightning Web Components – LWC

Quick Code Snippet on how to use custom label in Lightning Web Components – LWC

Below is code snippet on using custom labels in Lightning Web Components.

Assume, we have custom label named welcomeText and exitText

In Javascript file of LWC, we have to follow below syntax

// CustomlabelExample.js
import { LightningElement } from 'lwc';
 
// Import custom labels
import welcomeText from '@salesforce/label/c.welcomeText';
import exitText from '@salesforce/label/c.exitText';


export default class CustomLabelExample extends LightningElement { 

    // Expose the labels to use in the template.
    label = {
        welcomeText,
        exitText
    };
}

Notice, how we imported label and used c. if there is no namespace.

import welcomeText from '@salesforce/label/c.welcomeText';
<!-- CustomlabelExample.html -->
<template>
    Hi , {label.welcomeText} !!! 
   <!-- some more code --> 
   Sorry to see you going, {label.exitText}
</template>

Reference :

Get Current User Id in Salesforce

How to get Current Logged in user Id in Apex, Visualforce, Lightning Component and Formula fields in Salesforce

Quick Code Snippets of how to get Current logged in user ids in Salesforce

Apex

System.debug('Current User Id - '+UserInfo.getUserId());

Visualforce

<apex:page>
  <h1>Visualforce Page</h1> 
   <p>UserId: {!$User.Id}</p>
   <p>User Email: {!$User.Email}</p>
</apex:page>

Aura Component

let currentUser = $A.get("$SObjectType.CurrentUser.Id");
Console.log(currentUser);

Lightning Web Components (LWC)

Using wire

import USER_ID from '@salesforce/user/Id';
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

Formula Fields

$User.Id

Get Picklist Values in Apex

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , , with 2 Comments on Get Picklist Values in Apex

Code snippet of reading Picklist values in Salesforce Apex

In Interview question part 4, we discussed how dynamic Apex can be used to retrieve metadata information about Objects or fields on Object.

While writing highly configurable code, there are situation where we want to avoid hardcoding picklist values and read it realtime from field.

Below code snippet takes two parameter, Object and picklist field name. On basis of these two inputs, we can read either Picklist label or Picklist value (API Name)

String objectName = 'Contact';
String fieldName ='LeadSource';
  
Schema.SObjectType s = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
    System.debug(pickListVal.getLabel() +' '+pickListVal.getValue());
}