0

I need a field on this Create Ticket page to populate with data from a related object's (Company) field.

I have an Apex Class, Trigger, and Test class posted below.

When I run the test class I can see the Company field being inserted and having a valid ID that's passed, but when I set that field I assume that before Update would run as a field is being changed. It's not. I need the field to run before any saving to the database is done, as the workflow is as follows.

User enters in a company, the notes field on the Ticket form is populated with the notes field from the company.

The Trigger statements in the debug are not being printed. Log is "USER_DEBUG [16]|DEBUG|Company Note on Ticket:null"

Trigger

trigger UpdateCompanyNotes on Ticket__c (before update) {
    System.debug('Trigger logic opened');
    if (trigger.isBefore && trigger.isUpdate) {
        System.Debug('We are in the Trigger logic');
        CreateTicketCustomLogic.UpdateCompanyNotesField(trigger.new, trigger.oldMap);
    }
}

Apex Class

public class CreateTicketCustomLogic {
    public static void UpdateCompanyNotesField(List<Ticket__c> newList, Map<Id, Ticket__c> oldMap) {
        for (Ticket__c t: newList) {
            if (t.CompanyNumberLookup__c != oldMap.get(t.Id).CompanyNumberLookup__c) {
                if (String.isEmpty(t.CompanyNumberLookup__r.Company_Notes__c) == false) {
                   t.Company_Notes__c = t.CompanyNumberLookup__r.Company_Notes__c; 
                }
                else {
                    t.Company_Notes__c = '';
                }
                
            }
        }
    }
}

Test

@isTest
private class TestCreateTicketCustomLogic {
    static testMethod void validateUpdateCompanyNotesField() {
        Company__c c = new Company__c();
        c.Name = 'Test Name';
        c.Company_Notes__c = 'This is a test note';
        c.CompanyPhoneNo__c = '123 456 7890';
        c.CompanyEmail__c = '[email protected]';
        c.Services_Offered__c = 'Business ReKey';
        c.Company_Timezone__c = '(GMT-04:00) Eastern Daylight Time (America/New_York)';
        insert c;
        Test.startTest();
        Ticket__c t = new Ticket__c();
        t.CallInNumber__c = '123 456 7980';
        t.CompanyNumberLookup__c = c.Id;
        System.Debug('Company Note on Ticket:' + t.Company_Notes__c);
        insert t;
        Test.stopTest();
        System.assertEquals(t.Company_Notes__c, c.Company_Notes__c);
    }
}
3
  • Are you expecting to see your user interface update as you are entering data? That's not what triggers do - you must initiate a save for triggers to run.
    – David Reed
    Apr 1, 2023 at 3:35
  • Oof. Yea. I need the UI to update a different field once the company field is changed or populated. That field will be populated by a field on the company object. Apr 1, 2023 at 3:39
  • The standard UI will not update a different field. You must build a custom dat entry component if you want that to happen. As David Reed says, Apex triggers only fire after you save the record, so Apex will not do what you want. Apr 1, 2023 at 23:32

1 Answer 1

1

Triggers run only when a save event takes place. You cannot use a trigger to react to un-saved changes in the user interface.

Per your comment,

I need the UI to update a different field once the company field is changed or populated. That field will be populated by a field on the company object.

You cannot do that with a trigger. You will need to use a Flow or a code-based UI implementation, such as a Lightning Web Component.

You must log in to answer this question.

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