I'm trying to create an Apex trigger that automatically creates a new record when some requirements are met. We have a custom object, Vendor Issues, which we would like to have new records created for when cases of a certain record type are created. I am unable to find any issues with the trigger itself, however when I run the debug logs, it shows the trigger stops calculating before it finishes. Below is the trigger:

trigger VIAutomation_Misshipments on Case (before insert, before update) 
    Final String FSW_FULFILLER = 'a0Y60000000aVa5';
    for (Case c : trigger.new)
        if (c.RecordType.Name == 'Misshipment' && c.GLOBAL_Associated_Fulfiller__c == FSW_fulfiller && c.Vendor_Issue_created__c == false)
            Vendor_Issue__c v = new Vendor_Issue__c();
            v.Vendor_Issue__c = 'Mis-Shipment';
            v.Resulted_In__c = 'Other';
            v.Related_Account__c = c.Account.Id;
            v.Summation__c = c.Description;
            v.Related_Fulfiller__c = FSW_FULFILLER;

            if (c.Order_ID__c != null)
                Order__c theOrder = [select Id, Name from Order__c where Id =: c.Order_ID__c];
                v.Related_Order__c = c.Order_ID__c;
                v.Name = 'VI for ' + theOrder.Name;
            if (c.Order_ID__c == null)
                v.Name = 'VI for Unknown Order ' + System.NOW().format();
                v.Order_not_in_SF__c = true;
            insert v;
            c.Vendor_Issue_created__c = true;               


And here is the debug logs where you can see it just stops running the trigger:

17:46:15.101 (101085416)|CODE_UNIT_STARTED|     [EXTERNAL]|01qZ0000000DOdX|VIAutomation_Misshipments on Case trigger event BeforeUpdate for [500Z0000009E7vt]
17:46:15.101 (101229299)|SYSTEM_METHOD_ENTRY|[4]|List<Case>.iterator()
17:46:15.101 (101250473)|SYSTEM_METHOD_EXIT|[4]|List<Case>.iterator()
17:46:15.101 (101259529)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
17:46:15.101 (101271428)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
17:46:15.101 (101358278)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
17:46:15.101 (101371611)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
17:46:15.191 (101383688)|CUMULATIVE_LIMIT_USAGE
  Number of SOQL queries: 6 out of 100
  Number of query rows: 6 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10


17:46:15.101 (101427244)|CODE_UNIT_FINISHED|VIAutomation_Misshipments on Case trigger event BeforeUpdate for [500Z0000009E7vt]

Any suggestions you can think of would be greatly appreciated. I am at a loss as to why this doesn't work.

EDIT: Thank you very much to user Bachovski, who did a great job explaining why this wasn't working.

  • 1
    I think it's in the for loop, but not able to get into the if statement. Examine your three parameters (System.debug, Assert) and see if they are what you think they are. – Caspar Harmer Apr 30 '15 at 0:27

I think your trigger doesn't even pass your first IF statement due trying to reference the record type name. You're doing c.RecordType.Name which means you're trying to access field on the parent object. In the trigger context you only have access to fields on the sObject that the trigger is running on. So in this case you can access RecordTypeId, but not RecordType.Name. What you need to do is, to create a map of record types for your object that will hold the record type Id and Name so that you can reference it later.

Here is an example code:

Map <Id, RecordType> caseRecordTypes = new Map <Id, RecordType> ([SELECT Id, Name FROM RecordType WHERE sObjectType = 'Case']);

for (Case c : trigger.new)
    if (caseRecordTypes.get(c.RecordTypeId).Name == 'Misshipment')
    // ...
  • Or as I like to do, define a RecordTypeName and / RecordTypeDeveloperName field calc. – Caspar Harmer Apr 30 '15 at 0:41
  • I knew there was something wrong with that if statement! Duh. – Caspar Harmer Apr 30 '15 at 0:42
  • You can retrieve both in the query and you can reference them from the value in the map :) I also tend to use DeveloperName rather than Name – Boris Bachovski Apr 30 '15 at 0:44

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.