1

Trying to write a before trigger that wipes out the value of a field, Firm_Association__c if Consulting_Firm__c is either edited or deleted. They are both ID fields. I have never written a trigger before so this is totally new. I don't think I am doing it right. Can someone pls help this newbie? Thank you!

trigger FirmAssociationTrigger on Opportunity (before update) {
    for(Opportunity a : Trigger.new) {
        Opportunity oldFirmValue = Trigger.oldMap.get(a.Consulting_Firm__c);
        if(a.Consulting_Firm__c != oldFirmValue.Consulting_Firm__c);
        update a.Firm_Association__c = '';
    } 
}
2

If statements should have a statement after them:

if(condition) {
  doSomething
}

The update keyword performs a DML, but you don't need a DML here. Just assign a new value.

You can't use '' to assign a blank value to an ID field, use null instead.

You need to use the Id of the Opportunity in Trigger.oldMap/Trigger.newMap.

trigger FirmAssociationTrigger on Opportunity (before update) {
    for(Opportunity a : Trigger.new) {
        Opportunity oldFirmValue = Trigger.oldMap.get(a.Id);
        if(a.Consulting_Firm__c != oldFirmValue.Consulting_Firm__c) {
            a.Firm_Association__c = null;
        }
    } 
}
2
  • Thank you so much! Now I am struggling with the test class and it's posted below. Any help would be fantastic! – APEX Newbs Dec 2 '20 at 4:05
  • @Newbs Insert an account, two consulting firms, and an opportunity with the value assigned to one of the firms. Then in your test, change it to the other consulting firm. Query the opportunity back from the database and us System.assert to verify the output. – sfdcfox Dec 2 '20 at 8:37
0

Yes, you can use a before trigger to do such a task. And you should use the before trigger, instead of the after trigger for such a case. In before triggers, you don't call an update on the current records. The definition of "before" here is that it happens before the system saves the record to the database. This is essentially a hook point where you can add your logic before that.

Here is the Trigger Execution Order if you are curious on what exactly happens before and after the before trigger.

0

You guys, awesome so far. So this is what I have for the trigger, which is working beautifully in my sandbox.

trigger FirmAssociationTrigger on Opportunity (before update) {
    for(Opportunity a : Trigger.new) {
        // Get the before change values
        Opportunity oldFirmValue  = Trigger.oldMap.get(a.Id);
        
        // Compare Consulting_Firm__c
        if(a.Consulting_Firm__c  != oldFirmValue.Consulting_Firm__c) {
            a.Firm_Association__c = null;
        }
        
        // Compare AccountID
        if(a.AccountId != oldFirmValue.AccountID) {
            a.Firm_Association__c = null;
        }

        //Compare Primary_Consultant_Name__c
        if(a.Primary_Consultant_Name__c != oldFirmValue.Primary_Consultant_Name__c) {
            a.Firm_Association__c = null;
        }
    } 
}

The problem is, like I said this was my first trigger, Which means I don't know how to write a test class. So far what I have is this.

@isTest
public class FirmAssociationTriggerTest {

    @isTest static void wipeFirmAssociation() {
        // Populate Lookup Account Data
        Account clientFirmAccount = new Account();
            clientFirmAccount.Name = 'Client Firm';
            insert clientFirmAccount;
            
        Account consultingFirmAccount = new Account();
            consultingFirmAccount.Name = 'Consultant Firm';
            insert consultingFirmAccount;
        
        Contact consultantContact = new Contact();
            consultantContact.FirstName = 'Test';
            consultantContact.LastName = 'Contact';
            insert consultantContact;                
            
        Account clientFirmAccount1 = new Account();
            clientFirmAccount1.Name = 'Client Firm1';
            insert clientFirmAccount1;
            
        Account consultingFirmAccount1 = new Account();
            consultingFirmAccount1.Name = 'Consultant Firm1';
            insert consultingFirmAccount1;
        
        Contact consultantContact1 = new Contact();
            consultantContact1.FirstName = 'Test1';
            consultantContact1.LastName = 'Contact1';
            insert consultantContact1;              
        
        // Create an Opportunity and Set Required fields
        Opportunity opp = new Opportunity();
        opp.AccountID = clientFirmAccount.Id;
        opp.Consulting_Firm__c = consultingFirmAccount.Id;
        opp.Primary_Consultant_Name__c = consultantContact.Id;
        opp.StageName = 'Developmental';
        opp.CloseDate = System.today();
        insert opp;
        
        opp.Consulting_Firm__c = consultingFirmAccount1.Id;
        update opp;

    }
}

Obviously the struggle is that all three fields are lookup fields. I am getting no coverage. What am I doing wrong?

2
  • We can't "answer" this, as you posted this as an answer (that's okay, it happens frequently enough with newer visitors). However, if you want to post this as an actual new question, we can give it the proper attention it deserves. As an aside, are you sure you're not getting coverage? It should at least get about 77% coverage by my estimates. – sfdcfox Dec 2 '20 at 8:41
  • Let me post this as another question =) but yes I am getting no coverage :'( – APEX Newbs Dec 2 '20 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.