Tuesday, November 8, 2022

Create Quick Actions with Lightning Web Components

 


To set up a Lightning web component as a quick action on a record page, define the metadata in <component>.js-meta.xml

Define a lightning__RecordAction target and  specify actionType as ScreenAction for a screen action that opens in a window or Action for a headless action that executes when clicked.

callLwc.html

<template>
</template>

callLwc.js

import { LightningElement,api,track } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent'

export default class CallLwc extends LightningElement {
@api recordId;

@api invoke(){
alert('Record Id is: '+this.recordId;
}

callLwc.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction">
<actionType>Action</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>

To Create Quick Action:

As we are creating this action for the Contact Record Page, it is an object-specific action.

  • Navigate to the Contact Object  in Setup.
  • Under Button, Links, and Actions.
  • Create a new Action.
  • Select LWC as Action Type.
  • Select your LWC component 
  • Give label to your Action
  • Name will be auto Populated
  • Click Save Button
  • Edit the Page Layout, and select Mobile & Lightning Actions
  • Drag the new Quick Action to the Lightning Experience Actions section 
  • You might have to override the standard actions before you’re able to add custom actions 

If you have any comments or queries about this, please feel free to mention them in the comments section below.

Testing HTTP Callouts by Implementing the HttpCalloutMock Interface in Salesforce


This is a full example that shows how to test an HTTP callout. 

CalloutClass

====================

public class CalloutClass {
public static HttpResponse getInfoFromExternalService() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://example.com/example/test');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
return res;
}
}


JSON2ApexMock Apex Class

==============================

 @isTest

global class JSON2ApexMock implements HttpCalloutMock {

HttpResponse res;
global JSON2ApexMock(HttpResponse r)
{
res=r;
}
global HttpResponse respond(HttpRequest req){
return res;
}
}


Callout Test Class
=====================================
@isTest
private class CalloutClassTest {
@isTest static void testCallout() {

// Create a fake response
HttpResponse res=new HttpResponse();
res.setBody('{"Description": "200"}');
res.setHeader('Content-Type', 'application/json;charset=UTF-8');
res.setStatusCode(200);

// Set mock callout class
Test.setMock(HttpCalloutMock.class, new JSON2ApexMock(res));
// Call method to test.
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
HttpResponse res = CalloutClass.getInfoFromExternalService();
// Verify response received contains fake values
String contentType = res.getHeader('Content-Type');
System.assert(contentType == 'application/json');
String actualValue = res.getBody();
String expectedValue = '{"Description":"200"}';
System.assertEquals(actualValue, expectedValue);
System.assertEquals(200, res.getStatusCode());
}
}


If you have any comments or queries about this, please feel free to mention them in the comments section below.

Download Files from Salesforce in zip format at once

 

In certain situations, we may need to download many files from Salesforce at once.

Simple URL hack:
By including the file's content version Ids in the URL, you may download all files in a zip archive. Everyone remarked that it is not the preferred technique, and I may call it a URL hack.

Below is the URL format that you need to use:

https://tridentpvtltd.lightning.force.com/sfc/servlet.shepherd/version/download/068aaaaaaaaaa/0684bbbbbbbbR/068cccccccccc


You may get the files as zip files by simply pasting this URL.