1

I'm having some issue with having a trigger run after inserting an object from a Rest API call. I've confirmed that the object is being created, and I've tested the other sections of the code manually and they are fine.

Here's my code

Sensor_Event__c event = new Sensor_Event__c();
event.JSON_Event__c = requestBody.toString();
insert event;

(I've checked and the object has the correct event with the correct string)

Here's my trigger

trigger ProcessSensorEvent on Sensor_Event__c (after insert) {
    
        for(Sensor_Event__c event : Trigger.new) {
            SensorsHandler.SensorEvent sensorEvent = new SensorsHandler.SensorEvent();
            sensorEvent = SensorsHandler.parseSensorJSON(event.JSON_Event__c);
            SensorsHandler.generateMonitorFromEvent(sensorEvent);
        }
   
}

I've tried inserting the same object from the exernal rest API in manually and the trigger code runs properly, and the object is created by the rest api without issue, but when the object is created by the REST API, the trigger doesn't run. Any help would be appreciated.

4
  • 3
    How do you know the trigger isn't being run? If the record is inserted, then its triggers will execute
    – cropredy
    Commented Mar 20, 2023 at 18:26
  • 2
    Adding on to that, the log that would show the trigger running is going to be tied to the user you're authorizing as when you make the API call. Be sure to set up the appropriate trace flags (Environment -> Logs -> Debug Logs) for that user.
    – Derek F
    Commented Mar 20, 2023 at 18:31
  • In the trigger I have some code that updates another object(Monitor__c). When I insert the event through the Developer Console it updates as expected, but when coming through the REST Api, the (Monitor__c) object isn't being updated, even when the Sensor_event is inserted
    – Kevin Yao
    Commented Mar 20, 2023 at 18:31
  • So it seems that the trigger is running, but due to some issues with the SOQL querys (it seems that none of the query's are returning any data when used with the RestResource Site Profile) it was failing - I didn't notice it because I wasn't checking the logs for that profile user.
    – Kevin Yao
    Commented Mar 20, 2023 at 20:08

1 Answer 1

1

When a record is inserted and you can see it in the database, then it is guaranteed the trigger ran.

Now, you might not see the effects of the trigger for several reasons

  • The trigger is coded to not do anything for the given running user
  • The trigger is coded to not do anything given the presence of certain metadata like Custom Settings
  • The trigger actually runs, perhaps delegating to helper classes and because of sharing rules for the running user, no related records are being retrieved so loops fall through and process zero records

Since your example has the request coming from an external source, check which user is the running user and examine the debug logs for that user to gain insight

1
  • The trigger was running - the issue was that the user was a guest and the DML operations were failing and I wasn't seeing the logs from it
    – Kevin Yao
    Commented Mar 21, 2023 at 17:38

You must log in to answer this question.

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