Friday, November 29, 2019

Reading and Populating MAP in LWC

The following example uses wire adapter to get metadata about a specific object. The response includes metadata describing fields, child relationships, record type, and theme.
data.fields provides the list of all fields of contact object in a MAP with key value pair. objectInfo is a MAP which is populated with the values received in data.fields.
Javacript file:
/* eslint-disable guard-for-in */
/* eslint-disable no-console */
import { LightningElement, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import CONTACT_OBJECT from '@salesforce/schema/Contact';

export default class UserOnboard extends LightningElement {

    @track objectInfo = [];

    @wire(getObjectInfo, { objectApiName: CONTACT_OBJECT })
    wiredContact({ error, data }) {
        if (data) {
            
            //data.fields is a Map<String, Field> 
            for(let key in data.fields)
            {
               //Inserting Data into Map
                this.objectInfo.push({value:data.fields[key].apiName, isRequired:data.fields[key].required, key:key})  
            }
        } else if (error) {
            this.error = error;
            this.record = undefined;
        }
    }
   
}
html file:
<template>
    <lightning-card title="Wire Get Object Info" icon-name="custom:custom67">
        <div class="slds-m-horizontal_medium">
                
            <template for:each={objectInfo} for:item='item'>
                <li class="slds-listbox__item slds-search-dropdown-list-items" key={item.key}>
                   <div>{item.value}</div>
                 </li>
            </template>
        </div>
    </lightning-card>
</template>

Thursday, November 21, 2019

Reading URL Parameter in Aura Component


You can read the URL parameters in the following way through a lightning Aura Component.
Aura Component to display the query parameter key and values.
ReadUrlParam.cmp
<aura:component
  implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId"
  access="global"
>
  <aura:handler name="init" action="{!c.doInit}" value="{!this}" />
  <aura:attribute name="paramValues" type="Map" />
  <div>
    <aura:iteration items="{!v.paramValues}" var="pv" indexVar="key">
      {!pv.key} - {!pv.value}

      <br />
    </aura:iteration>
  </div>
</aura:component>
Controller to read the query parameter key and values.
ReadUrlParam.js
({
 doInit : function(component, event, helper) 
    {
        var querystring = location.search.substr(1);
        var paramValue = [];
        querystring.split("&").forEach(function(part) {
        var param = part.split("=");
        paramValue.push({value:decodeURIComponent(param[1]), key:param[0]});
            
        });
   component.set("v.paramValues",paramValue);
  }
})
Call the class by appending the following in the URL.
?first=Suman&last=Halder&middle=none

Learning Fun