Skip to content

Instantly share code, notes, and snippets.

@sfdevmaster
sfdevmaster / FilesDownloaderCtrl.java
Last active July 27, 2025 17:40
Downloading File From Content Document Using Base64 File
/**
* What it does:
Defines a nested FileWrapper class that holds:
fileName: Name of the file.
mimeType: MIME type like application/pdf or image/png.
base64Data: Encoded file content as a base64 string.
The method getFileAsBlob():
Fetches the most recent ContentDocumentLink for the given parentId.
Retrieves the latest ContentVersion for that ContentDocumentId.
Converts the binary data to a Base64 string.
@sfdevmaster
sfdevmaster / filesDownloader.html
Last active July 25, 2025 04:27
Downloading File Using Client Side Rest Api Call out from LWC
<template>
<lightning-card title="Download File From Rest Api Response" icon-name="utility:download">
<div class="slds-p-around_medium">
<template if:true={isLoading}>
<lightning-spinner alternative-text="Loading ZIP..." size="medium"></lightning-spinner>
</template>
<lightning-button
label="Download"
onclick={handleDownload}
disabled={isLoading}
@sfdevmaster
sfdevmaster / FilesDownloaderCtrl.java
Last active July 25, 2025 05:19
Downloading All Attachment As a Zip File
/**
* What it does:
The method getAttachmentsAsZip():
Accepts a parentId, which is the ID of the record (e.g., a Case or Account) whose attachments we want to fetch.
Queries for all attachments associated with that record.
Uses the Compression.ZipWriter to create a ZIP archive containing all attachments.
Returns the ZIP file as a Base64-encoded string, which can be used in a Lightning Web Component (LWC) to trigger a download.
*/
public with sharing class FilesDownloaderCtrl {
/**
@sfdevmaster
sfdevmaster / FilesDownloaderCtrl.java
Last active July 23, 2025 16:08
Downloading Attachment Using Salesforce URL in browser
/**
* What it does:
The method getAttachmentDownloadUrl():
Accepts a parentId, which is the ID of the record (e.g., a Case or Account) whose attachment we want to fetch.
Queries for the most recently modified Attachment associated with that record.
Constructs and returns a standard Salesforce file download URL using the Attachment ID:
/servlet/servlet.FileDownload?file=<AttachmentId>
This URL will trigger the browser to download the file when opened.
*/
public with sharing class FilesDownloaderCtrl {
@sfdevmaster
sfdevmaster / FilesDownloaderCtrl.java
Last active July 25, 2025 05:23
Downloading Attachment Using Base64 File
/**
* What it does:
Defines a nested FileWrapper class that holds:
fileName: Name of the file.
mimeType: MIME type like application/pdf or image/png.
base64Data: Encoded file content as a base64 string.
The method getAttachmentAsBlob():
Fetches the most recently updated Attachment record for the given parent record.
Converts the binary content (Body) to base64.
Wraps it into a FileWrapper object and returns it to the frontend.
@sfdevmaster
sfdevmaster / LazyLoadingContactController.java
Last active July 6, 2025 07:41
🖥️ Client Side Lazy Loading In HTML Table With Scroll Bar - LWC
//LazyLoadingContactController.apex
public class LazyLoadingContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getAllContacts() {
return [
SELECT Id, FirstName,LastName, Email
FROM Contact WHERE Name Like 'E%' LIMIT 1000];
}
}
@sfdevmaster
sfdevmaster / CaseController.java
Last active July 27, 2025 12:25
Using Chart.js in LWC
public without sharing class CaseController {
@AuraEnabled(cacheable=true)
public static List<AggregateResult> getCaseData(String statuses) {
List<String> statusList = String.isNotBlank(statuses) ? statuses.split(',') : null;
return [
SELECT Status, COUNT(Id)
FROM Case
WHERE Status IN :statusList
GROUP BY Status
];
/**
* ContactGrabber Apex Class
* This class provides methods to retrieve related contacts for an account,
* and fetch account details.
*/
public with sharing class ContactGrabber {
/**
* Retrieves all contacts related to a given account.
* @param acctId The Id of the account for which to retrieve related contacts.
* @return A list of Contact records related to the specified account.
<!--childValidation.html-->
<template>
<div>
<div id="test" data-id="test1"></div>
<lightning-card title="Child Validation Example">
<div class="slds-m-around_medium">
<lightning-input
label="Name"
data-id="nameField"
class="textareaclass"
@sfdevmaster
sfdevmaster / StudentApiCalloutAction.java
Created May 18, 2025 13:18
StudentApiCalloutAction Class - To demonstarte, how to use apex as an invocable action in flow
/**
* StudentApiCalloutAction
* ----------------------
* @description
* This Apex class is designed to perform a callout to an external API to fetch user data based on
* the university name provided as input. It processes the response and organizes the data into
* a list of Contact records, which are then returned to the caller.
*
* Key Features:
* - Uses the `@InvocableMethod` annotation to allow invocation from Flow or Process Builder.