Category Archives: Salesforce

Mass Delete Salesforce Records Using Flow

Business Use Case: There is a custom field “IsActive” on “Contact” object. The Admin user wants to delete all inactive Contacts from “Account” record.

Here I’ve created a flow and the flow is invoked by a Custom Button, and that Custom Button is configured on Account object page layout. User will be able to access in both Classic & Lightning experience.

1. Go to Setup | Quick Find – Search Flows – Click Flow | New Flow

2. Create a variable as “varAccountId”

3. Create a sObject Collection Variable as “varCollectionOfContacts”

4. Create a Fast Lookup to get inactive Contacts with Account Id

5. Create a Decision to check Account has available inactive contacts.

6. Create Fast Delete to delete Contacts

8. Set the Fast Lookup as a starting element, indicated by the green down arrow. Then, connect the flow element “Fast Lookup” to “Decision” and “Decision” to “Fast Delete”.

9. Activate the created flow.

10. Create a Custom Button on “Account” object to invoke the Flow with AccountId parameter.

11. Add above created Custom button into Account page layout.

Salesforce Schedulable Apex Test Class

Batch Class:

global class AccountBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id, Name, Industry FROM Account';                
        return Database.getQueryLocator(query);     
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList){        
        for(Account acc : accList){          
            acc.Industry = 'Banking';        
        }        
        update accList;       
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
}

Schedulable Class:

global class AccountBatchScheduled implements Schedulable {
    
    global void execute(SchedulableContext sc) {
        AccountBatch objBatch = new AccountBatch(); 
        Database.executebatch(objBatch);
    }
}

Test Class:

@isTest
private class AccountBatchScheduledTest{
    
    static testmethod void schedulerTest(){
        
        String cronexpression = '0 0 0 15 3 ? *';

        Account acc = new Account();
        acc.Name = 'Test Account';
        Insert acc;
        
        Test.startTest();
        String jobId = System.schedule('ScheduleBatchApexClass',  cronexpression, new AccountBatchScheduled());
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(cronexpression, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        Test.stopTest();
    }
}

Send Email From a Custom Button in Salesforce

Sample Code:

{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}

var mail = new sforce.SingleEmailMessage();

mail.targetObjectId = "{!Contact.Id}";
mail.toAddresses = "{!Contact.Email}";
mail.templateId = "00X28000000Utjy";
mail.whatId = "{!Case.Id}";

var result = sforce.connection.sendEmail([mail]);

if (result[0].success == 'true') {
    alert("Email sent successfully.");
} else {
    alert("Email sending failed.");
}

Note: Only User, Contact, Lead, or Person Account objects are allowed for targetObjectId.

If you need to refer specific attributes then download Partner WSDL and refer complexType SingleEmailMessage.

<?xml version="1.0" encoding="UTF-8"?>
<complexType name="SingleEmailMessage">
   <complexContent>
      <extension base="tns:Email">
         <sequence>
            <element name="bccAddresses" minOccurs="0" maxOccurs="25" type="xsd:string" nillable="true" />
            <element name="ccAddresses" minOccurs="0" maxOccurs="25" type="xsd:string" nillable="true" />
            <element name="charset" type="xsd:string" nillable="true" />
            <element name="documentAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:ID" />
            <element name="entityAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:ID" />
            <element name="fileAttachments" minOccurs="0" maxOccurs="unbounded" type="tns:EmailFileAttachment" />
            <element name="htmlBody" type="xsd:string" nillable="true" />
            <element name="inReplyTo" minOccurs="0" type="xsd:string" nillable="true" />
            <element name="optOutPolicy" type="tns:SendEmailOptOutPolicy" nillable="true" />
            <element name="orgWideEmailAddressId" minOccurs="0" maxOccurs="1" type="tns:ID" nillable="true" />
            <element name="plainTextBody" type="xsd:string" nillable="true" />
            <element name="references" minOccurs="0" type="xsd:string" nillable="true" />
            <element name="targetObjectId" type="tns:ID" nillable="true" />
            <element name="templateId" type="tns:ID" nillable="true" />
            <element name="templateName" minOccurs="0" type="xsd:string" nillable="true" />
            <element name="toAddresses" minOccurs="0" maxOccurs="100" type="xsd:string" nillable="true" />
            <element name="treatBodiesAsTemplate" type="xsd:boolean" nillable="true" />
            <element name="treatTargetObjectAsRecipient" type="xsd:boolean" nillable="true" />
            <element name="whatId" type="tns:ID" nillable="true" />
         </sequence>
      </extension>
   </complexContent>
</complexType>