-2

Below is the trigger I've written on Case that although did not throw any syntax error but does throw below error when trying to create a new case.

Apex trigger DefaultPartName caused an unexpected exception, contact your administrator: DefaultPartName: execution of AfterInsert caused by: System.StringException: Invalid id: T012390000001jvk: Trigger.DefaultPartName: line 3, column 1

What am I missing here?

The requirement is, anytime a new case is created for specific Case Record Type, it should set a Part Name(API Name: Part_Number__c )field to a default value called "Not Product Related" for any other Case Record type, No action should happen on the Part Name field.

Below is the Trigger:

trigger DefaultPartName on Case (after insert) {
    for(Case c : Trigger.new){
        if(c.RecordTypeId == 'T012390000001jvk'){
            c.Part_Number__c = 'Not Product Related';
        } else
            c.Part_Number__c = '';
        insert c;
    }

}
7

Please note that the Apex Developer Guide explicitly recommends using a before trigger for field updates:

There are two types of triggers:

  • Before triggers are used to update or validate record values before they’re saved to the database.
  • After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

The key prefix (first three characters) for every RecordType is 012, not T01. You seem to have prepended a T to the Id value for some reason. But you should not hardcode your ids anywhere. It is bad practice and will not work when you migrate between environments.


Much better to use describes or queries to get the Id. Never hardcode the value.

Id myRecordTypeId = SObjectType.Case.getRecordTypeInfosByName()
    .get('My Record Type').getRecordTypeId();
for (Case record : trigger.new)
{
    if (record.RecordTypeId == myRecordTypeId)
    {
        // do stuff
    }
    else
    {
        // do other stuff
    }
}

Another note, if Part_Number__c is a Lookup field, you need to set the Id of the parent, just like you do when reading the RecordTypeId value. Note that you don't check RecordTypeId = 'Some Record Type Name', but rather check the actual Id value. It's the same with all lookups, whether getting or setting via code.

MyRelatedObject__c relatedObject = [SELECT Id FROM MyRelatedObject__c WHERE Name = ''Not Product Related'];

Note also you will need to create this record in your unit test before running your trigger.


Note that this line in an after insert trigger is going to throw errors and completely unnecessary:

insert c;

Remove this line. The record is already being inserted.

  • its an after insert trigger. inserting explicitly by insert c; is not required? After changing from RecordTypeId to RecordType.Name and I'm now getting following error: Error: Invalid Data. Review all error messages below to correct your data. Apex trigger DefaultPartName caused an unexpected exception, contact your administrator: DefaultPartName: execution of AfterInsert caused by: System.StringException: Invalid id: : Trigger.DefaultPartName: line 6, column 1 – – sfdcDev Oct 24 '17 at 19:26
  • @sfdcDev Is Part_Number__c a lookup? If so then you need to actually set the Id of the corresponding record, not its name... – Adrian Larson Oct 24 '17 at 19:41
3

The value you've got in quotes is not a valid id. They're either 15 or 18 characters.

RecordType records have a prefix of 012, so in theory if you delete the T you'll be in good shape - assuming that is a valid id in your org.

It would be better though to not hardcode the id value at all and either query for it using SOQL or use a describe call.

  • 1
    Except c.RecordType.Name will always be null in the trigger context. – Adrian Larson Oct 24 '17 at 18:53
  • 1
    details, details :-) – Mark Pond Oct 24 '17 at 18:59
  • so I've changed from RecordTypeId to RecordType.Name and it still throws the following error: Error: Invalid Data. Review all error messages below to correct your data. Apex trigger DefaultPartName caused an unexpected exception, contact your administrator: DefaultPartName: execution of AfterInsert caused by: System.StringException: Invalid id: : Trigger.DefaultPartName: line 6, column 1 – sfdcDev Oct 24 '17 at 19:18
  • Below is the revised trigger: 'trigger DefaultPartName on Case (after insert) { for(Case c : Trigger.new){ if(c.RecordTypeIName == 'TABS Support'){ c.Part_Number__c = 'Not Product Related'; } else c.Part_Number__c = ''; insert c; } }' – sfdcDev Oct 24 '17 at 19:22

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.