Tuesday, 26 December 2023

getPicklistValuesByRecordType Adaptor


getPicklistValuesByRecordType JS

import { LightningElement, wire } from 'lwc';

import {getPicklistValuesByRecordType, getObjectInfo} from 'lightning/uiObjectInfoApi'
import ACCOUNT_OBJECT from '@salesforce/schema/Account'
export default class GetPicklistValuesByRecordTypeDemo extends LightningElement {
    ratingOptions
    industryOptions
    selectedRating
    selectedIndustry
    @wire(getObjectInfo, {objectApiName:ACCOUNT_OBJECT})
    objectInfo

    @wire(getPicklistValuesByRecordType, {objectApiName:ACCOUNT_OBJECT, 
        recordTypeId:'$objectInfo.data.defaultRecordTypeId'})
        picklistHandler({data, error}){
            if(data){
                console.log(data)
                this.ratingOptions = this.picklistGenerator(data.picklistFieldValues.Rating)
                this.industryOptions = this.picklistGenerator(data.picklistFieldValues.Industry)
            }
            if(error){
                console.error(error)
            }
        }

    picklistGenerator(data){
        return data.values.map(item=>({"label":item.label, "value":item.value}))
    }

    handleChange(event){
        const {name, value} = event.target
        console.log(name +'==>' +value)
        if(name === 'industry'){
            this.selectedIndustry = value
        }
        if(name === 'rating'){
            this.selectedRating = value
        }
    }
}

getPicklistValuesByRecordType Html

<template>
<lightning-card  variant="Narrow"  title="Hello" icon-name="standard:account">
<div class="slds-var-p-around_medium">
    <template if:true={ratingOptions}>
    <lightning-combobox
        name="rating"
        label="Rating"
        value={SelectedRating}
        placeholder="Select Rating"
        options={ratingOptions}
        onchange={handleChange} ></lightning-combobox>
    </template>
    <template if:true={industryOptions}>
         <lightning-combobox
        name="industry"
        label="Industry"
        value={SelectedIndustry}
        placeholder="Select Industry"
        options={industryOptions}
        onchange={handleChange} ></lightning-combobox>
    </template>
</div>
</lightning-card>
</template>

Wednesday, 20 December 2023

LWC getPicklistValues

getPicklistValues

Use this wire adapter to get the picklist values for a specified field.

Syntax

import {lightningElement } from 'lwc';

import {getPicklistValues} from 'lightning/uiObjectInfoApi';

import  STATUS_Field from '@salesforce/schema/Account.status';

export default class Example LightningEvent{

@wire(getPicklistValues,{recordTypeId: '021XXXXXXXXXXXXX',fieldApiName:STATUS_Field})

propertyorfunction

}

Tuesday, 19 December 2023

LWC getObjectInfo adapter

Example 1  

getObjectInfoadapter js

import { LightningElement,track } from 'lwc';

import {getObjectInfoApi} from 'lightning/uiObjectInfoApi';

import Contact_OBJECT from '@salesforce/schema/Contact';

export default class AccountDisplatRecords extends LightningElement {

defaultRecordTypeId

@wire(getObjectInfoApi, {objectApiName:Contact_OBJECT})

objectInfo({data,error}){

if(data){

// check Inspect all the atribute we get it 

 this.defaultRecordTypeId = data.defaultRecordTypeId

}if(error){

  console.error(error)

}

}

}

getObjectInfoadapter html

<tempalte>

<lightning-card title = "getObjectInfo Adapter">

<div class="slds-var-p-around_medium">

 {defaultRecordTypeId}

</div>

</lightning-card>

</tempalte>

Another Example 2

getObjectInfoadapter js

import { LightningElement,track } from 'lwc';

import {getObjectInfoApi} from 'lightning/uiObjectInfoApi';

import Contact_OBJECT from '@salesforce/schema/Contact';

export default class AccountDisplatRecords extends LightningElement {

defaultRecordTypeId

@wire(getObjectInfoApi, {objectApiName:Contact_OBJECT})

objectInfo

}

getObjectInfoadapter html

<tempalte>

<lightning-card title = "getObjectInfo Adapter">

<div class="slds-var-p-around_medium">

 defaultRecordTypeId: {objectInfo.data.defaultRecordTypeId}

 defaultapiName: {objectInfo.data.apiName}

</div>

</lightning-card>

</tempalte>


 getObjectInfos


Use this wire adapter to get metadata for multiple objects. The response includes metadata describing

the fields, child relationships, record type, and theme for each objects


Syntax


import {LightningElement, wire} from 'lwc';

import {getObjectInfos} from 'lightning/uiObjectInfoApi';

import CONTACT_OBJECT from '@salesforce/schema/Contact';

import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class GetObjectInfosExample extends LightningElement{

objectapirecords = [ACCOUNT_OBJECT,CONTACT_OBJECT];

@wire(getObjectInfos,{objectApiNames : $objectapirecords})

propertyorFunction

}

Parent to Child Passing Data Use @wire

Parent to Child Passing Data Value 

public class DisplayAccountRecordsget{

public static list<contact> getAccountRecords(){

return [select id,lastname,firstname,phone from contact limit 10];

}

}

parenttochildpassdata js

-----------------------------

import {LightningElement} from 'lwc';

import getrecords from '@salesforce/apex/DisplayContactRecordsget.getContactRecords';

export default class parenttochildpassdata extends LightningElement {

datadisplayed

@wire(getrecords)

datawire({data,error})

if(data){

this.datadisplayed = data;

} else if(error){

 console.log('Error feacting data from Apex:'+error);

}

parenttochidpassdata html

<template>

<lightning-card>

<c-childtoparentpassdata contacts = {datadisplayed}><c-childtoparentpassdata>

</lightning-card>

</template>

childtoparentpassdata js

import {lightningElement , api} from 'lwc';

export default class childtoparentpassdata extends LightningElement {

@api contacts

}

childtoparentpassdata html

<template>

<lightning-card variant="Narrow"  title="Parent to Child" icon-name="standard:contact">

<template if:true={contacts}>

<template for:each={contacts} for:item = "conlist">

<li key={conlist.id}> Contact FirstName - {con.FirstName}

                      Contact LastName -  {con.LastName} 

                      Contact Phone - {con.Phone}</li>

</template>

</lightning-card>

</template>

Child To Parent Data Passing Value

Chtil to Parent Passing Data Apex 

public class DisplayAccountRecords{

public static list<account> getAccountRecords(){

return [select id,name,phone from account];

}

}

Childaccountinsertrecord  Js Component Name

import { LightningElement  } from 'lwc';

import getrecords from '@salesforce/apex/DisplayRecords.getAccountRecords';

export default class ChildaccountinsertComponent extends LightningElement {

@wire(getrecords)

wirdata({data,error}){

if(data){

 const childcomponentdata = new CustomEvent('dataready',{detail:data});

  this.dispatchEvent(childcomponentdata);

} else if(error){

console.log('Error feacting data from Apex:'+error);

}

}

}

Childaccountinsertrecord  Js Component Name

import { LightningElement  } from 'lwc';

import getrecords from '@salesforce/apex/DisplayRecords.getdisplayedrecords';

export default class ChildaccountinsertComponent extends LightningElement {

@wire(getrecords)

wirdata({data,error}){

if(data){

 const childcomponentdata = new CustomEvent('dataready',{detail:data});

  this.dispatchEvent(childcomponentdata);

} else if(error){

console.log('Error feacting data from Apex:'+error);

}

}

}

childaccountinsertrecord  html Component Name

<template>

<!- I can not   displayed  any data ->

</template>

Parentaccountinsertrecord Js Component Name

import { LightningElement  } from 'lwc';

export default class ParentaccountinsertComponent extends LightningElement {

receivedData

handleDataReady(event){

this.receivedData = event.detail

}

}

Parentaccountinsertrecord html Component Name

<template>

<lightning-card  title= "Parent Comonent">

<div>

<c-childaccountinsertrecord  ondataready = {handleDataReady}></c-childaccountinsertrecord>

</div>

<template if:true={receivedData}>

<template for:each={receivedData} for:item={acc}>

  <li> Account ID :- {acc.id} </li>

  <li> Account Name :- {acc.Name} </li>

  <li> Account Phone :- {acc.Phone} </li>

</template>

</lightning-card>

</template>

Rest API Callout Service Example

 Rest API Callout Service Example 

public class RestAPIDisplayValue {

@AuraEnabled(cacheable=true)

public static String getRestAPI(Integer id){

Http htp = new Http();

HttpRequest req = new HttpRequest();

req.setMethod('GET');

req.setHeader('Content-Type', 'application/json');

req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);

HttpResponse res = htp.send(req);

String strReq = '';

if(res.getStatusCode() == 200){

map<String,object> result = (Map<String,object>)JSON.deserializeUntyped(res.getBody());

map<String,object> animals = (Map<String,object>) result.get('animal');

    strReq = String.valueOf(animals.get('name'));

    System.debug('datadisplaid'+strReq);

}

 return strReq;

}

}

RestAPI DisplayedRecords

----------------------------

import { LightningElement,track } from 'lwc';

import getrestapi from '@salesforce/apex/RestAPIDisplayValue.getRestAPI';

export default class RestAPIDetail extends LightningElement {

@track animalId;

@track animalName;

handleInputChange(event){

    this.animalId = event.target.value

}

 getRestapiDetail(){

     getrestapi({

         id: this.animalId

         })

      .then(result => {

      console.log('result'+result);

       this.animalName = result;

      })

      .catch(error,{

      });

}

}

Displayed we get response

------------------------

<template>

<lightning-card  variant="Narrow"  title="Animal Locator" icon-name="utility:animal_and_nature">

<div>

    <lightning-input label="Enter Animal ID" type="number" value={animalId} onchange={handleInputChange}></lightning-input>

    <lightning-button variant="base" label="Get Animal Name" onclick={getRestapiDetail}></lightning-button>

    <div if:true={animalName}>

                <p>Animal Name: {animalName}</p>

    </div>

</div>

</lightning-card>

</template>

Displayed Apex Get Data Use @wire

 Apex Get Data 

public class DisplayRecordWire {

@AuraEnabled(cacheable=true)

public static list<contact> Displayrecords(){

  return [select id,lastname,firstname,phone from contact limit 10];

}

}


DisplayRecordWire.js

import { LightningElement,wire } from 'lwc';

import getcontactswire from '@salesforce/apex/DisplayRecordWire.Displayrecords';

export default class DisplayRecordsWire extends LightningElement {

 wiredisplayed

@wire(getcontactswire)

contactrecord({data,error}){

 if(data){

     this.wiredisplayed = data

    console.log('displayed records'+data)

 } if(error){

     console.error('displayrecordserror'+error)

 }

}

}

DisplayRecordWire HTML


<template>


    <lightning-card title="Contact Records" icon-name="standard:contact">


        <div class="slds-m-around_medium">


            <template if:true={wiredisplayed}>


                <table class="slds-table slds-table_cell-buffer slds-table_bordered">


                    <thead>


                        <tr>


                            <th>Last Name</th>


                            <th>First Name</th>


                            <th>Phone</th>


                        </tr>


                    </thead>

                    <tbody>

                        <template for:each={wiredisplayed} for:item="contact">

                            <tr key={contact.Id}>

                                <td>{contact.LastName}</td>

                                <td>{contact.FirstName}</td>

                                <td>{contact.Phone}</td>

                            </tr>

                        </template>

                    </tbody>

                </table>

            </template>

            <template if:true={error}>

                <p>Error retrieving contact records: {error}</p>

            </template>

        </div>

    </lightning-card>

</template>