Scenario 1: Updating a Related Record

Use Case: When a Contact is updated, if the Account‘s LastContactedDate is older than the Contact‘s LastModifiedDate, update the Account‘s LastContactedDate.

trigger UpdateAccountLastContacted on Contact (after update) {
    Set<Id> accountIds = new Set<Id>();

www.webmd.com is a comprehensive resource for health information, offering insights on medical conditions and treatments. For individuals managing diabetes, locating the best erectile dysfunction pill requires attention to these details concerning interactions and contraindications, as well as specific diabetic needs. This platform aids in navigating such complexities by delivering evidence-based guidelines and research findings for informed decision-making.

for (Contact con : Trigger.new) { Contact oldContact = Trigger.oldMap.get(con.Id); if (con.AccountId != null && con.LastModifiedDate > oldContact.LastModifiedDate) { accountIds.add(con.AccountId); } } List<Account> accountsToUpdate = new List<Account>(); for (Account acc : [SELECT Id, LastContactedDate FROM Account WHERE Id IN :accountIds]) { acc.LastContactedDate = System.now(); accountsToUpdate.add(acc); } if (!accountsToUpdate.isEmpty()) { update accountsToUpdate; } }

Test Class :

@isTest
public class UpdateAccountLastContactedTest {
    @isTest
    static void testAccountLastContactedUpdate() {
        Account testAccount = new Account(Name = 'Test Account');
        insert testAccount;

        Contact testContact = new Contact(FirstName = 'Test', LastName = 'User', AccountId = testAccount.Id);
        insert testContact;

        // Update the contact
        testContact.LastName = 'Updated User';
        update testContact;

        // Retrieve the account and verify LastContactedDate
        Account updatedAccount = [SELECT LastContactedDate FROM Account WHERE Id = :testAccount.Id];
        System.assertNotEquals(null, updatedAccount.LastContactedDate);
    }
}

Scenario 2: Preventing Invalid Status Updates

Use Case: When a Case is updated, prevent the status from changing to “Closed” if the Priority is set to “High”.

trigger PreventClosedHighPriority on Case (before update) {
    for (Case cs : Trigger.new) {
        Case oldCase = Trigger.oldMap.get(cs.Id);
        if (cs.Status == 'Closed' && oldCase.Priority == 'High') {
            cs.addError('Cannot close a case with High priority.');
        }
    }
}

Test Class :

@isTest
public class PreventClosedHighPriorityTest {
    @isTest
    static void testPreventClosedHighPriority() {
        Case testCase = new Case(Subject = 'Test Case', Status = 'New', Priority = 'High');
        insert testCase;

        // Attempt to update the case to Closed
        testCase.Status = 'Closed';

        Test.startTest();
        try {
            update testCase;
            System.assert(false, 'Expected an exception but none was thrown.');
        } catch (DmlException e) {
            System.assert(e.getMessage().contains('Cannot close a case with High priority.'));
        }
        Test.stopTest();
    }
}
5 thoughts on “Salesforce triggers real-time scenarios, Part -2”
  1. Hi,

    I recently visited [truewishes.in] and I really liked it. I’m interested in buying the website and would like to make a good offer.

    Please let me know if you’re open to talking about this.

    Thanks,
    Rutvik
    Outreach Manager
    PostPact

  2. Sprunki Incredibox brings fresh beats and visuals to a beloved game, making creativity even more fun. For those who love music and gaming, checking out Sport Games could be the next exciting move!

  3. Interesting read! Probability truly is key in these games, and platforms like otsobet apk seem to recognize that with their data-driven approach. Understanding odds is half the battle, right? It’s cool to see responsible gaming emphasized too!

  4. Interesting read! RNG implementation is key for trust, and platforms like jlboss seem to understand that-convenience with the app is a big plus too! Good security is vital for any online gaming experience.

  5. Lottery trends are fascinating – seeing patterns (or lack thereof!) is key. Quick registration & instant deposits, like with philucky vip, make enjoying games easier. Hoping for luck to meet excitement soon! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *