1

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) {

  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!

3
  • 2
    why not use workflows and field updates?
    – cropredy
    Commented Jan 6, 2015 at 21:09
  • We are already hitting our limit on workflow rules and there would be too many possibilities to create one workflow rule per type of record type.
    – Matt
    Commented Jan 8, 2015 at 0:14
  • 1) # of wf rules can be increased with request to sfdc support; 2) the field update can be a CASE statement so you can do many possibilities in one field update - not sure this will help your specific use case but FYI
    – cropredy
    Commented Jan 8, 2015 at 0:46

2 Answers 2

3

First Issue: Looks like you are confusing RecordType.name values with RecordType.DeveloperName values, which is easy to do - the nomenclature in the system can be confusing. In the UI, you are asked for two types of names when creating a record type: "Record Type Label" and "Record Type Name". Record Type Label is the display name used throughout the UI, and corresponds to the api field RecordType.name. The constants you have in your code, such as 'Intake Form' are actually these label values - you can tell because they contain spaces. Record Type Name is the unique name used by the API, and corresponds to RecordType.DeveloperName. If you check out the pop-up help for this field when creating a record type, you'll see this:

The unique name used by the API and managed packages. The name must begin with a letter and use only alphanumeric characters and underscores. The name cannot end with an underscore or have two consecutive underscores

Like many parts of the Salesforce Admin UI, if you enter a Record Type Label of "Intake Form", the system will auto-populate Record Type Name with "Intake_Form". Unlike with SObject names and Field Names, the true value used in queries and code doesn't include "__c" on the end.

Second Issue: the map variable typeMap is a map using ID as a key, and the "RecordType" object type as values. The code typeMap.get('TEST Agreements') will never return a value because 'Test Agreements' isn't an ID value. To get the record type by name, you'll need to iterate over the results of the RecordType query and create a map of DeveloperName (or Name, it you prefer) to IDs, and then assign the found ID to agmt.RecordTypeId.

Here's a stab at rewriting your trigger, guessing at your actual DeveloperName values:

trigger IntakeFormTrigger on Apttus__APTS_Agreement__c (after insert, before update) {

  Map<String, Id> typeMap = New Map<ID,RecordType>();
  for(RecordType rt: [Select ID, DeveloperName From RecordType Where sObjectType = 'Apttus__APTS_Agreement__c']) {
    typeMap.put(rt.DeveloperName, rt.id);
  }

  for (Apttus__APTS_Agreement__c agmt : trigger.new)  {
    // If the Record Type = Intake Form
    if (agmt.RecordType.DeveloperName == 'Intake_Form') { //Confirm this DevName value!
      // 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.RecordTypeID = typeMap.get('TEST_Agreements'); //Confirm this DevName value!
      }
    }          
  }
}
1
  • Thanks Jason. I copied over your code but was getting an error because it Map<String, Id> wasn't matching up with New Map<Id, RecordType>. I changed that first line to "Map<String, Id> typeMap = New Map<String, Id>();" to see if it would work. No luck though. Nothing happens.
    – Matt
    Commented Jan 8, 2015 at 0:16
1

I was able to work on this and get it working. I think one of the issues was that i was trying to call agmt.RecordType.DeveloperName, but it was returning a null value. I had to create a RecordType variable using the Id found in the agmt record and also a separate RecordType variable with the record type I was running the if statement against and compare the two. Here is the code that ended up working for me.

trigger IntakeFormTrigger on Apttus__APTS_Agreement__c (before insert, before update) {
Map<String, Id> typeMap = New Map<String, Id>();

   for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Apttus__APTS_Agreement__c']) {
      typeMap.put(rt.DeveloperName, rt.Id);
   }

   for (Apttus__APTS_Agreement__c agmt : trigger.new)  {

      recordtype agmtRT = [select id, developername from recordtype where id=:agmt.recordtypeid];
      recordtype IFRT = [select id, developername from recordtype where developername = 'Intake_Form'];
      // If the Record Type = Intake Form
      if (agmtRT.id == IFRT.id) { 
            // And the Agreement Category on the record = Services
            if (agmt.Apttus__Agreement_Category__c == 'Services') {
                // Then automatically change the Record Type to Services Agreement.
                id recid = typeMap.get('Services_Agreement');
                recordtype rectype = [select id, developername from recordtype where id=:recid];
               agmt.RecordTypeid = rectype.id; 
            }
       }
   }
}

You must log in to answer this question.

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