RP.
Live Engine

Case Files

The Debugging Arena

Theoretical knowledge meets real-world constraints. Every scenario below is based on actual challenges faced during Salesforce development and data migration.

DML Logic Case #1024

The "All-or-None" Dilemma

Scenario: You are importing 200 Contacts. Due to a validation rule, 5 Contacts will fail. The requirement is to save the other 195 valid records without failing the entire transaction.

"How do you handle this to allow partial processing and log the specific errors?"

// The Solution: Database Methods

Database.SaveResult[] srList = Database.insert(conList, false);

// Audit the results

for(Database.SaveResult sr : srList) {

if (!sr.isSuccess()) {

// Log failure details

System.debug(sr.getErrors()[0].getMessage());

}

}

Integrity Case #2048

The Identity Crisis

Scenario: You are creating a Task and trying to link it to an Account using the WhoId field. The code crashes. Why?

"Field Integrity Exception: id value of incorrect type"

// The Diagnosis

WhoId = Account.Id; // ERROR!

// The Fix

myTask.WhatId = acc.Id; // Links to Objects

myTask.WhoId = con.Id; // Links to People

Apex Logic Case #3072

Automated Contact Provisioning

Scenario: You need to write a method that accepts a first name, last name, and email to create a new Contact record.

Ensure the LastName is not blank before processing.
Use a List to prepare for bulkified operations.
Allow partial success during insertion.

// The Implementation

public class UseCase_1 {

public static void createContactRecord(String fName, String lName, String email) {

List<Contact> conList = new List<Contact>();

Contact con = new Contact();

if (!String.isBlank(lName)) {

con.FirstName = fName;

con.LastName = lName;

con.Email = email;

conList.add(con);

}

if (conList.size() > 0) {

Database.insert(conList, false); // Partial success

}

}

}

Utility Logic Case #4096

Calculating Simple Interest

Scenario: You need to build a reusable method that calculates Simple Interest based on Principal, Rate of Interest, and Time (in years).

Perform a Null Check on all inputs to prevent NullPointerException.
Return the result as a Decimal for high precision.
Ensure a default return value of 0 if any input is missing.

// The Implementation

public class UseCase_2 {


// Returns SI = (P * R * T) / 100

public static Decimal calculateIntrest(Decimal p, Decimal r, Integer t) {

if (p != null && r != null && t != null) {

Decimal si = (p * r * t) / 100;

return si;

}

return 0;

}

}

Messaging API Case #5120

Automated Email Dispatcher

Scenario: You need to create a service method that accepts a single email address and sends a high-priority notification email directly from Salesforce.

Utilize the Messaging.SingleEmailMessage class for outbound mail.
Construct an email with a custom Subject and Plain Text Body.
Execute the send command using a List of messages for bulk compatibility.

// The Implementation

public with sharing class UseCase_3 {


public static void sendEmail(String email) {

List<String> emailList = new List<String>{email};


// Initialize Message

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setSubject('URGENT- Take a Look');

mail.setPlainTextBody('This is a notification triggered via Apex for you');

mail.setToAddresses(emailList);


// Send the List

Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

}

}

Relational SOQL Case #6144

Parent-Child Combo Validator

Scenario: You need to verify if a specific Contact is associated with a specific Account by name. This is common for data validation during integrations.

Input Sanitization: Guard against null strings for both names.
Traverse the relationship from Contact to Account.Name.
Optimize performance using LIMIT 1 for a fast boolean check.

// The Implementation

public with sharing class UseCase_4 {

public static Boolean checkNameExist(String conName, String accName) {


if (conName == null || accName == null) {

return false;

}


// Relationship Query

List<Contact> conList = [SELECT Id FROM Contact

WHERE Name = :conName AND Account.Name = :accName LIMIT 1];


return conList.size() > 0 ? true : false;

}

}

Date Arithmetic Case #7168

Voting Rights Verification

Scenario: You are tasked with determining if a citizen is eligible to vote. The logic must calculate their current age based on a provided Date of Birth relative to today's date.

Capture the current system date using System.today().
Calculate the delta using the daysBetween() method.
Apply a threshold of 18 years for the boolean return.

// The Implementation

public with sharing class UseCase_6 {

public static Boolean checkVotingRights(Date dateOfBirth) {

Date toDaysDate = System.today();


// Convert total days into years

Integer totalDaysBetween = (dateOfBirth.daysBetween(toDaysDate)/365);


if (totalDaysBetween >= 18) {

return true;

}

return false;

}

}

Arithmetic Logic Case #8192

The Bill Splitter Utility

Scenario: You are building a feature that allows users to split a total bill amount equally among a specific number of friends.

Ensure Null Safety to prevent crashes if inputs are missing.
Perform a clean Integer Division to return the per-person cost.
Return a default value of 0 if the calculation cannot be performed.

// The Implementation

public with sharing class UseCase_7 {

public static Integer splitBill(Integer friends, Integer bills) {

Integer splitedBill = 0;


if (friends != null && bills != null) {

splitedBill = bills / friends;

}


return splitedBill;

}

}