Skip to content

Instantly share code, notes, and snippets.

@sfdevmaster
sfdevmaster / RollUpBOnA.java
Last active July 5, 2025 05:25
Roll up summary trigger To Roll Up Child Record Count and sum total values of child records on Parent Record using maps
//Trigger to update the Total__c and Record_Count__c fields of parent records A__c
// when child records B__c are inserted, updated, undeleted or deleted.
trigger RollUpBOnA on B__c (
after insert,
after update,
after delete,
after undelete
) {
// Declare a set to store the Ids of the parent object A__c
Set<Id> parentIds = new Set<Id>();
@sfdevmaster
sfdevmaster / UpdateContactCountOnAccount.java
Last active May 17, 2025 10:23
UpdateContactCountOnAccount trigger To Roll Up Contacts Count on Account using aggregate query
/**
* UpdateContactCountOnAccount Trigger
* ----------------
* This trigger is designed to update the Number_of_Contacts__c field on the Account object
* whenever a Contact record is inserted, updated, deleted, or undeleted.
* The purpose is update the Number_of_Contacts__c field on the related Account record.
* Key Features:
* - Identifies Contact records linked to an Account record (via AccountId).
* - Counts the number of Contact records for each Account record.
* - Updates the Number_of_Contacts__c field on the Account record.
@sfdevmaster
sfdevmaster / RollUpBOnA.java
Last active July 5, 2025 05:27
Roll up summary trigger To Roll Up Child Record Count and sum total values of child records on Parent Record using aggregate query
/**
* RollUpBOnA Trigger
* ----------------
* This trigger is executed after a B__c record is inserted, updated, deleted, or undeleted.
* The purpose is to update the Total__c and Record_Count__c fields on the related A__c record.
* Key Features:
* - Identifies B__c records linked to an A__c record (via A_No__c).
* - Counts the number of B__c records and sums their Value__c field for each A__c record.
* - Updates the Total__c and Record_Count__c fields on the A__c record.
* - Ensures any updates made re-trigger workflows or additional triggers, if applicable.
@sfdevmaster
sfdevmaster / ContactCreationBatch.java
Last active May 16, 2025 17:32
ContactCreationBatch
/**
* @description - This batch class createS contacts for accounts created in the last 7 days.
* It tracks success and error records and sends an email with the results as CSV attachments.
*/
public class ContactCreationBatch implements Database.Batchable<sObject>, Database.Stateful {
// Stateful variables to track results
private List<ContactWrapper> successList;
private List<ContactWrapper> failureList;
@sfdevmaster
sfdevmaster / AccountUpdateBatch.java
Last active May 16, 2025 17:27
AccountUpdateBatch
/**
* @description - This batch class updates the description of Account records created today.
* It tracks success and error records and sends an email with the results as CSV attachments.
*/
public class AccountUpdateBatch implements Database.Batchable<sObject>, Database.Stateful {
// Stateful variables to track results
private List<String> successRecords;
private List<String> errorRecords;
/* eslint-disable no-else-return */
// Import the required modules
import { LightningElement, api } from "lwc";
import getFieldTypes from "@salesforce/apex/GenericRecordController.getFieldTypes";
import getRecords from "@salesforce/apex/GenericRecordController.getRecords";
import { NavigationMixin } from "lightning/navigation";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { loadStyle } from "lightning/platformResourceLoader";
import customStyle from "@salesforce/resourceUrl/datatableStyle";
/**
<template>
<!-- Rendering a Lightning card -->
<lightning-card title="">
<!-- Displaying a Lightning spinner -->
<template if:true={showSpinner}>
<div id="spinnerDiv" style="height: 10rem">
<div class="slds-spinner_container">
<div role="status" class="slds-spinner slds-spinner_medium">
<span class="slds-assistive-text">Loading</span>
<div class="slds-spinner__dot-a"></div>
/*
* @Description : The EncryptionCtrl class is a public Apex class that enforces sharing rules,
* meaning it respects the user's sharing settings when accessing data.
* It contains methods for retrieving and decrypting an AES key and handling
* encrypted card numbers. The MY_KEY constant holds a sample encryption key,
* which can be accessed in test classes due to the @TestVisible annotation.
*/
public with sharing class EncryptionCtrl {
// Define a private static constant string MY_KEY, which holds a sample encryption key.
@sfdevmaster
sfdevmaster / EncryptionExample.html
Last active May 16, 2025 03:21
Using Encryption in LWC via Crypto JS
<!--HTML File: Data Input and Submission Button-->
<!-In the following code, we create an LWC that masks the credit card number on the UI.
It encrypts the actual value before sending it to Apex.-->
<template>
<lightning-card title="Encryption In LWC">
<div class="slds-m-around_medium">
<lightning-combobox name="cardType" label="Card Type" placeholder="Please select..."
options={cardTypes} onchange={handleChange} required>
</lightning-combobox>
<lightning-input name="cardNumber" type="text" label="Card Number" min-length="15" max-length="16"
@sfdevmaster
sfdevmaster / ContactTrigger.java
Created May 14, 2025 08:28
ContactTrigger With Recursion
/**
* ContactTrigger
* ----------------
* This trigger is executed after a Contact record is updated.
* The purpose is to update the Title of a Contact to include a reference to the related Account's Name.
*
* Key Features:
* - Identifies Contacts linked to an Account (via AccountId).
* - Queries the related Account's Name and updates the Contact's Title to reference it.
* - Ensures any updates made re-trigger workflows or additional triggers, if applicable.