+ Start a Discussion
NJDeveloper019NJDeveloper019 

Trigger not working

I am new to triggers.  I am trying to build a trigger that will update a number field in a parent object before a delete or after an insert.  The workflow is as follows. (I only did the insert piece first just to try to get it working)

 

1. Check to see if the there is an insert from submit__c

2. Build a list of all submit records that have the same parent job order as the newly inserted submit record

3. Select all job orders that have the same id as the job order of the newly inserted record (should only be 1)

4. Foreach of these job orders (should be 1), set submit_count__c to the size of the list from step 2 and add to updatedCount

5. update updatedCount (job order that needs to have its submit_count__c updated)

 

This does not seem to be working at all.  How can I test to make sure the trigger is firing?  I've tried hardcoding jo.Submit_Count__c but still nothing.  Either something is wrong in my trigger code or the trigger isnt firing.  Any help would be greatly appreciated.

 

Following is the trigger code:

trigger rollupSubmits on Submit__c (before delete, after insert)
{
    List<Job_Order__c> updatedCount = new List<Job_Order__c>();
   
    if (Trigger.isInsert)
    {
        for (Submit__c s : Trigger.new)
        {
            List<Submit__c> submits = [SELECT Id FROM Submit__c WHERE Job_Order__c = :s.Job_Order__r.Id];
           
            for (Job_Order__c jo : [SELECT Id, Submit_Count__c FROM Job_Order__c WHERE Id = :s.Job_Order__r.Id])
            {
                jo.Submit_Count__c = submits.size();
                //jo.Submit_Count__c = 20;
                updatedCount.add(jo);
            }
           
            update updatedCount;
        }
    }
   
}

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
The trigger is working fine, however your queries are not, as s.Job_Order__r.Id will be null because no related data is populated in triggers, you should be using the foreign key field, not the related object (e.g. s.Job_Order__c) in your queries.

All Answers

SuperfellSuperfell
The trigger is working fine, however your queries are not, as s.Job_Order__r.Id will be null because no related data is populated in triggers, you should be using the foreign key field, not the related object (e.g. s.Job_Order__c) in your queries.
This was selected as the best answer
NJDeveloper019NJDeveloper019
Thanks alot, Simon.