function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt Cooper 7Matt Cooper 7 

Apex Trigger to Update Record Type Based on Field Value

I have been working on building a trigger that would allow my users to use a single Intake Form which would then change the record type based on the values they provide on the Intake Form page layout. Basically, people can come in and make selections based on their circumstances. Those selections will determine which type of record type the record should be. Below is the code that I have been working on that does not seem to be working.
trigger IntakeFormTrigger on Apttus__APTS_Agreement__c (after insert, before update) {
 
    // Get a list of record types.
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'Apttus__APTS_Agreement__c']);
  
    for (Apttus__APTS_Agreement__c agmt : trigger.new)
    {
        // If the Record Type = Intake Form
        if (agmt.RecordType.DeveloperName == 'Intake Form')
        {
            // And the Agreement Category on the record =TEST
                if (agmt.Apttus__Agreement_Category__c == 'TEST')
                {
                    // Then automatically change the Record Type to TEST Agreements.
                    agmt.RecordType = typeMap.get('TEST Agreements');
                }
        }          
    }
}



Any help would be great! Thanks!
Joel RichardsJoel Richards
Hi Matt,
Try just using the RecordTypeId instead of RecordType.DeveloperName eg:
 
trigger IntakeFormTrigger on Apttus__APTS_Agreement__c (after insert, before update) {
 
    // Get a list of record types.
    Map<ID,RecordType> typeMap = New Map<ID,RecordType>([Select ID, DeveloperName From RecordType Where sObjectType = 'Apttus__APTS_Agreement__c']);
  
  // Get the IntakeForm RecordTypeId
    id IntakeFormRT = Schema.SObjectType.Apttus__APTS_Agreement__c.getRecordTypeInfosByName().get('Intake Form').getRecordTypeId(); 
	
    for (Apttus__APTS_Agreement__c agmt : trigger.new)
    {
        // If the Record Type = Intake Form
        if (agmt.RecordTypeId == IntakeFormRT)
        {
            // And the Agreement Category on the record =TEST
                if (agmt.Apttus__Agreement_Category__c == 'TEST')
                {
                    // Then automatically change the Record Type to TEST Agreements.
                    agmt.RecordType = typeMap.get('TEST Agreements');
                }
        }          
    }
}