Monday, October 18, 2021

Pass data or parameters from child to parent component in LWC Salesforce

In this post i will explain how to pass parameters from child component to parent component in LWC salesforce.


Pass data or parameters from child to parent component in LWC Salesforce

parentComp.cmp :

<template>
    <div style ="padding: 30px; background-color: white;">
        <div><b>Parent Div</b></div>
        <div>Input typed in child component is : <b>{childOutput}</b></div>
    </div>
      <c-child-comp onpass={showValue}></c-child-comp>
</template>


parentComp.js :

import { LightningElement } from 'lwc';
export default class ParentComp extends LightningElement {
    childOutput;
    showValue(event)
    {
         this.childOutput = event.detail;
    }
}


childComp.cmp :

<template>
    <div style ="margin-top:10px; background-color:wheat; padding: 30px;">
        <div>
            <b>Child Div</b>
        </div>
        <lightning-input type="text" label="Enter some text in child div" onchange={handleChnge}></lightning-input>
    </div>
</template>


childComp.js :

import { LightningElement, track } from 'lwc';
export default class ChildComp extends LightningElement {
    @track value;
    handleChnge(event){
        this.value = event.target.value;

        this.dispatchEvent( new CustomEvent'pass', {
            detail: this.value
        } ) );
    }
}


Thanks, 
Lovesalesforceyes

Sunday, October 17, 2021

How to write SOQL query in Java

In this post i will tell you how to write SOQL apex query in java. 


Before reading this post i will recommend you to go through Salesforce java integration post.
Variable baseUri in the code is basicallly your org's instance. To know more about how to get it refer my recommended post.

Java Code :

HttpClient httpClient = HttpClientBuilder.create().build(); String url = baseUri + "/services/data/v48.0/query?q=/services/data/v50.0/query?q=Select+id,name+from+account"; HttpGet httpGet = new HttpGet(url); httpGet.addHeader(oauthHeader); httpGet.addHeader(prettyPrintHeader); response = httpClient.execute(httpGet);

In java you have to use special character encoding to write the SOQL. Below are some of the examples for referance.

SOQL Apex : select id from account where name like '%d%'

SOQL Java : select+id+from+account+where+name+like+'%25d%25'

SOQL Apex : select id,name from account where name = 'Pyramid Construction'

SOQL Java : select+id,name+from+account+where+name+=+'Pyramid+Construction'


For metadata objects(Tooling api objects):


Tooling api objects are those which contain records in the form of metadata. You can query tooling api objects in java like below:

String url = baseUri +"/services/data/v52.0/tooling/query?q=+Select+id,Name+from+MetadataContainer"


Thanks, 
Lovesalesforceyes

Thursday, October 14, 2021

How to use ternary or conditional operator in LWC html Salesforce

This post explains how to use conditional(ternary) operators in LWC salesforce.


Template expression doesn't allow Conditional Expressions. You can not use if-else conditions directly in LWC html. There are specific tags in LWC to use conditional rendering.
  • <template if:true>
  • <template if:false>

use ternary or conditional operator in LWC html Salesforce


ternaryOpt.cmp :


<template>
    <div style = "background-color:wheat; padding: 20px;">
        <lightning-button label="Change" onclick={Change}></lightning-button>
        <div>
        <template if:true={ConditionalCheck}>
                    Hurry!! button clicked :)
            </template>
            <template if:false={ConditionalCheck}>
                    Button is not clicked yet :(
            </template>
        </div>
    </div>
</template>


ternaryOpt.js :


import { LightningElement,track } from 'lwc';
export default class TernaryOpt extends LightningElement {
    @track ConditionalCheck = false;
    Change(){
        this.ConditionalCheck = true;
    }
}


Thanks, 
Lovesalesforceyes

Friday, October 8, 2021

Convert number to Indian format value in LWC JS salesforce

This post explains how to convert any number to indian format number in LWC Javascript salesforce.


The indian format of 1423443 is 14,23,443.
Note: Method toLocaleString() is applied on decimal and float values. So if your number is in string format then parse your value before using this method.

NumberFormat.cmp :


<template>
    <div style="background-color:wheat; padding:20px;">
        <lightning-input type="Number" onchange ={Valuelabel="Enter Text"></lightning-input>
        <br/>
        <lightning-button type="submit"
            name="Convert Number"
            label="Convert Number"
            onclick={FormatNumber}>
        </lightning-button>
        <br/>
        <div>
            Indian format of given number is: {formatNum}
        </div>
    </div>
</template>

NumberFormat.js :


import { LightningElement,track } from 'lwc';
export default class NumberFormat extends LightningElement {
        @track Number;
        @track formatNum;

        Value(event){
            this.Number = event.target.value;
            console.log(this.Number);
        }
        FormatNumber(event){
            var indian = parseFloat(this.Number).toLocaleString('en-IN');
            this.formatNum = indian;
        }
    
}

Output :


Convert number to Indian format


Thanks, 
Lovesalesforceyes


Wednesday, October 6, 2021

Override global CSS in salesforce community builder

This post explains how to Override global CSS in salesforce community builder.

  • Setup -> All sites
  • Click builder in front of your community
  • On the left side click on the theme icon.

Override global CSS in salesforce community builder

  • On the top right side on modal click on bottom arrow icon.


Override global CSS in salesforce community builder

  • Click on Edit CSS option
  • Modal will open after you click on Edit CSS. select Use overrides.
  • After that CSS window will open where you can change the styles accordingly.

NOTE: Global CSS will reflect on every page and every component of your community. So be sure before overriding the CSS. In most of the scenarios global CSS will only override if you want to change the font or theme color etc. In other scenarios use the component CSS.

Thanks, 
Lovesalesforceyes

Monday, October 4, 2021

How to cover try and catch block coverage in apex test class salesforce

This post explains how to cover the apex try and catch block in salesforce.

Sometimes It becomes important to handle the catch block to complete 75 percent coverage in apex class. There are two lines of code you have to implement in apex class in order to cover both try and catch blocks.
  • First you have to create a custom exception class.
  • Use Test.isRunningTest() at the end of your try block. This statement will only run when you are running a test class.
  • In Test.isRunningTest() throw a custom exception which you have created in earlier steps.
NOTE: The code is only based on that you want to cover both try and catch blocks.

SampleClass.apx :


public class SampleClass {
    //create custom exception class
    class MyException extends Exception{}
    public static void Testing(){
        try
            {
                String s = 'In try block';
                system.debug(s);
                // When test class is running it throw an exception
                if(Test.isRunningTest())
                {
                    throw new MyException();
                }
            }
        catch(Exception e)
            {
                String s = 'In catch';
                system.debug(s);
            }
    }
}

SampleClassTest.apx :


@isTest
public class SampleClassTest {
    Public  static testmethod void testSapIntegration(){
        SampleClass.Testing();
    }
}

Output :

Cover try catch block in apex test class


Thanks, 
Lovesalesforceyes

Get selected radio button value in LWC

This post explains how to get selected radio button in LWC. Below is demo of output : To use the radio button in LWC you have to use the  li...