Monday, January 31, 2022

How to write Test Class for Web Services

 Hello As we know that for Web Service  call out we need to call Apex class method  as per our requirement  .By default, test methods don’t support web service callouts, and tests that perform web service callouts fail.

Due failour test coverage will down and so built-in WebServiceMock interface help to resolve the failure and Test.setMock method  helps to increase the test coverage.

When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the Apex runtime to send

Steps to Write Test Class:-

  • You can annotate this class with @isTest because it is used only in a test context. In this way, you can exclude it from your org’s code size limit of 6 MB.
  • The class implementing the WebServiceMock interface can be either global or public.
NOTE:
Apex Test Classes will not let us conduct a HTTP callout; therefore, it cannot be used to test External APIs. However, 

there is a solution wherein Apex has an interface called HttpCalloutMock for standard callout tests.

HTTPCallout Class:

public class CarLocator {
    public static String getCarNameById(Integer id) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        String strResp = '';
        if (response.getStatusCode() == 200) {
           Map < String, Object > results = (Map < String, Object >) JSON.deserializeUntyped(response.getBody());
           Map < string, Object > cars = (Map < String, Object >) results.get('car');
           strResp = string.valueof(cars.get('name'));
        }
        return strResp ;
    }  
}
HTTP Mock Callout Class:

@isTest
global class CarLocatorMock implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest request) {
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"car": {"id":2, "name":"Test"}}');
        response.setStatusCode(200);
        return response; 
    }
}
Test Class:

@isTest 
private class CarLocatorTest {
    static testMethod void testPostCallout() {
        Test.setMock(HttpCalloutMock.class, new CarLocatorMock ());  
        String strResp = CarLocator getCarNameById(2);
    }
}

Friday, January 28, 2022

Conditional Rendering in LWC

 Hello All,

Today we are going to discus about conditional rendering in lighting web component ,If we want to hide some of the components from the HTML and show it based on conditions then we can use conditional rendering.
As we know that we have different concepts /syntax for conditional/logical operation .
  • Aura Components <Aura:if>,
  •  apex (if, else)      

  same way we have <template if:true> and <template if:false>in Lightning web component(LWC).

Sample Example LWC Code:

conditionalRenderingDemo.html

<!-- conditionalRenderingDemo.html-->
<template>
    <lightning-card conditionalRenderingDemoicon-name="custom:custom14">
        <div class="slds-m-around_medium">
            <lightning-input type="checkbox" label="Show details" onchange={handleChange}></lightning-input>
            <template if:true={isDivVisible= }>
                <div class="slds-m-vertical_medium">
                    These are the details!
                </div>
            </template>
        </div>
    </lightning-card>
</template>

// conditionalRenderingDemo.js
import { LightningElement } from 'lwc';
 
export default class conditionalRenderingDemo extends LightningElement {
    isDivVisible= false;
 
    handleChange(event) {
        this.areDetailsVisible = event.target.checked;
    }
}

  

Thursday, January 20, 2022

Share JavaScript Code Between Lightning Web Components

Hello Friends, as you know that writing the reusable code good into Development /implementation .

Create Shared Code

To share code between components, create an ES6 module and use the standard export statement to export the functionality that your module exposes. Your module can use the standard import statement to use other shared modules

utils LWC Component 

  export default shpwToast (variant='info', mode='dismissable', title, message) {
const event = new ShowToastEvent({ title: title, message: message, mode : mode, variant : variant }); return event; }
Note :- with in this component no html will be there.

Use Shared Code

To use the shared code in a JavaScript file, use the standard import statement. For example:

import {
    shpwToast 
} from 'c/utils';

This line imports the utils module in the c namespace.


Monday, January 17, 2022

Difference Between Salesforce Flows And Processes

Please find between flows and processes in Salesforce, using the terms interchangeably. Here are the major differences between the two:

  • Salesforce processes are more user-friendly with respect to setup and management as compared to Salesforce flows.
  • Flows allow users to add screens to enter data, while processes do not provide you with this facility.
  • A Salesforce flow can be invoked by users, triggered by a change in the records, or scheduled to run at a specific time and with a specific frequency. On the other hand, a Salesforce process runs automatically when the required criteria are met. Users can also invoke a process by creating one using the Process builder.
  • While Salesforce flows can be paused by users, processes cannot be paused and keep running until the criteria are being met.
  • While the actions of Salesforce processes are executed in the order of their appearance in the process definition, flows often have a more complicated order of operations.
  • Salesforce flows can be used for cycling through multiple Salesforce objects (related and unrelated). On the other hand, processes are limited to the base and related Salesforce objects.
  • While processes can be triggered only after a record has been saved, users can design Salesforce flows to be triggered when records are created, updated, or deleted.

Flow in Salesforce -Part1

 AS we know that Salesforce provides many automation tools. In this article, we are going to discuss about Flow in Salesforce.


What is Flow

Flow is an application inside the Salesforce that automates a business process by collecting data and performing operations in your org or an external system. Flow can fetch, delete, update and create records on multiple objects. Flows in Salesforce can be implemented in two ways

  1. Screen Flows
  2. Auto-launched Flow



Screen Flows

In this type of flow, there will be a series of screen elements to gather information from the user and perform some operation. Screen flows can be accessed from custom buttons, custom links, Visualforce Pages etc. This type of flow is implemented if a user interaction is needed in the process.

Auto-launched Flow

Auto-screenshot runs in the background without any user interaction. Auto-launched flows can be accessed from custom buttons, custom links, Visualforce Pages, process builder and Apex etc.