0

Wrote an apex trigger to update associated opportunities when a specific field is updated (which reflects the account's signup stage in our backend).

trigger updateopportunity on Account (after update) {
    
    set<Id> accountIds = new set<Id>();
    for (Account acc : Trigger.new)
    {
        Account old = Trigger.oldMap.get(acc.Id);
        if (acc.CD_Status__c=='pending' && acc.CD_Status__c != old.CD_Status__c) 
        {
        accountIds.add(acc.Id);
        }
    }
    if (!accountIds.isEmpty()) {
        List <Opportunity> opps = [
            select Id, StageName
            From Opportunity
            where AccountId in: accountIds
            and RecordTypeId ='0128d000000pBdUAAU'
            and StageName != 'Started SignUp'
            ];
        if (!opps.isEmpty()) {
                for (Opportunity opp : opps) {
                opp.StageName = 'Started SignUp';
            }
            update opps;
        }
    }
}

Read that to deploy in production, I needed to run Apex Class Tests? from the developer console. Not sure how exactly I am meant to format this. the best I could come up wiht, but to no success, was below.

Basically tried creating both an Account and related Opportunity, and then updating said Account's CD_Status__c to 'pending' (the requirement for the trigger), but not entirely sure how to go about initiating the test. I keep getting errors

@isTest
public class updateopportunity {
    @isTest static void Testupdateopportunity() {
        // set up user
        User u1 = [select id from User WHERE email='[email protected]'];
        
        //Run as u1
        System.RunAs(u1)
        
        //New Account Record
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.industry = 'Test';
        acc.BillingCountry = 'GB';
        acc.Account_Stage__c='Demo Booked';
        acc.Website='test.com';
        acc.Type='Prospect';
        insert acc;
        
        //New Opportunity
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.CloseDate = system.today().addDays(7);
        opp.StageName = 'Demo Completed';
        opp.Type = 'New Business';
        opp.RecordTypeId = '0128d000000pBdUAAU';
        opp.AccountId = acc.Id;
        opp.demobookedby__c='Rod';
        opp.datedemobooked__c=system.today();
        insert opp;
        
        //Update Account Field
        acc.CD_Status__c='pending';
        acc.Account_Stage__c='Started SignUp';
        updated.acc;
        
        test.stopTest()
        
        //check result of test - but not too sure how to go about this or if this is the correct format at all
        Select opp.StageName WHERE opp.AccountId=acc.Id
       
    }
}
1
  • 3
    It's just opp = [select stagename from opportunity where Id = :opp.id] then use system.assert to verify the stage has changed.
    – sfdcfox
    Oct 17, 2022 at 18:25

1 Answer 1

1

sfdcfox is correct, as always.

But I wanted to also recommend taking some time to learn best practices. Your triggers should contain 0 business logic. But instead make a call to your Account Trigger Handler that knows what needs to be done in the After Update context. Hard coded record Ids are also a major no no. You could replace that with custom meta data etc..

2
  • 1
    Thanks for the feedback. couple of questions. Not sure I follow what you mean by "After Update context". I simply want to reflect the onboarding stages of our prospects on the opportunity, but not store that info on the opportunity, as it relates to the prospect (and eventual customer). Where would be the best place to learn best practices?
    – Rod
    Oct 18, 2022 at 10:11
  • Apex triggers run in varous contexts, before insert, after update etc.. Typically you want some sort of a trigger handler. In your Account trigger there would not be much more than a call to something like ``` AccountHandler.run() ``` Your trigger handler would be some sort of a super class. And the concrete implementations Account, Opportunity etc.. would provide implementations of that should happen during these trigger contexts. A nice example is Keven O'haras' trigger handler. github.com/kevinohara80/sfdc-trigger-framework Oct 19, 2022 at 12:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .