+ Start a Discussion
Krish NKrish N 

Need help with Trigger test class

Hello Guys, I'm new to Apex Triggers. I need help in writing a test class for the below Trigger. Whenever a platform event message (unique Id) is published from a lightning flow, the trigger updates the Service Appoinmtment record whose Id is the event message. Please let me know if I should make any changes to the trigger. 
trigger platformtrigger on Notification__e (after insert) {
    for(notification__e ev: Trigger.new)
        ServiceAppointment SADispatched = [SELECT Id, ParentRecordId FROM ServiceAppointment WHERE Id = :ev.message__c];
        SADispatched.description = 'Automate Process';
        SADispatched.FSL__Auto_Schedule__c = true;
        update SADispatched;    

Claire CrabtreeClaire Crabtree
I'm somewhat of a beginner myself, but I documentation that may help. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

Below is a snippet that suggests that you cannot refer to :ev.message__c, but must first declare a String variable and assign the value of of ev.message__c to it and then reference it in your SOQL statement.

However, unlike inline SOQL, dynamic SOQL can’t use bind variable fields in the query string. The following example isn’t supported and results in a Variable does not exist error:
MyCustomObject__c myVariable = new MyCustomObject__c(field1__c ='TestField');
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE field1__c = :myVariable.field1__c');

You can instead resolve the variable field into a string and use the string in your dynamic SOQL query:
String resolvedField1 = myVariable.field1__c;
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE field1__c = ' + resolvedField1);