4

I'm trying to make a trigger that automatically creates a Location Record when a new Account is made, but only if the Account is type 'Customer - Direct' or 'Customer - Partner'. The trigger correctly creates the Location Record if I remove the if statement that checks the record type, but that defeats the purpose of my trigger. Here's my code:

trigger SetsLocationAddress on Account (after insert) {

    List<PSI_Location__c> locationToInsert = new List<PSI_Location__c>();

    //finds record type
    String rtCustomerDirect = 'Customer - Direct';
    String rtCustomerPartner = 'Customer - Partner';
    Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Campaign.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtInfoCustomerDirect =  rtMapByName.get(rtCustomerDirect);
    Schema.RecordTypeInfo rtInfoCustomerPartner =  rtMapByName.get(rtCustomerPartner);
    id rtTypeIdCustomerDirect = rtInfoCustomerDirect.getRecordTypeId();
    id rtTypeIdCustomerPartner = rtInfoCustomerPartner.getRecordTypeId();

    for(Account accountInLoop : Trigger.new )
    {
        //tests if account is type Customer
        if( accountInLoop.RecordTypeId == rtTypeIdCustomerDirect || accountInLoop.RecordTypeId == rtTypeIdCustomerPartner )
        {
            //creates new location record based on shipping address on account record 
            locationToInsert.add(new PSI_Location__c(
            Name = 'Location 1',            
            Account__c = accountInLoop.Id, 
            Street_Address__c = accountInLoop.ShippingStreet, 
            City__c = accountInLoop.ShippingCity, 
            State__c = accountInLoop.ShippingState, 
            Zip_Code__c = accountInLoop.ShippingPostalCode));  


        }
    }

    insert locationToInsert;
}

As you can see, the first block of code is supposed to reference the record type id so that the if statement can work, but when I compile and try to create a new account to test the trigger, I get this error:

"SetsLocationAddress: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetsLocationAddress: line 11, column 1"

I don't know why it's complaining about a NullPointerException, because the trigger is only supposed to work after the account is created when it can test the record type.

If anyone can lend a hand, any and all help is greatly appreciated!

  • Line 11 is where I define the id rtTypeIdCustomerDirect. The code didn't post very well. – Matt Mc Jun 18 '15 at 15:52
  • Also, I know that I could just check if the picklist selection for Account Type is either Customer - Direct or Customer - Partner, but my employer told me that he wants the trigger to be able to run only if the record type id is of type customer. – Matt Mc Jun 18 '15 at 15:54
  • Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Campaign.getRecordTypeInfosByName(); This code gets record type of Campaign object. Please change it to Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Account.getRecordTypeInfosByName(); – Vigneshwaran G Jun 18 '15 at 16:07
  • Thanks, I made the change, but I'm still getting the same error. – Matt Mc Jun 18 '15 at 16:13
2

The error indicates that this line of code didn't find a reference in the map and that the variable rtInfoCustomerDirect is null.

Schema.RecordTypeInfo rtInfoCustomerDirect = rtMapByName.get(rtCustomerDirect);

Then on line 11, you attempt to call a method on that null variable and the error is thrown. rtInfoCustomerDirect.getRecordTypeId();

To be safe, your code should check both of those Schema.RecordTypeInfo variables to be not null before you call a method on them.


Your describe is getting record type values from the Campaign object, not the Account object. It appears you just need to change Campaign to Account here:

Schema.SObjectType.Campaign.getRecordTypeInfosByName();

Schema.SObjectType.Account.getRecordTypeInfosByName();

The easiest way to troubleshoot is to add system.debug(rtMapByName.keyset()); after line 8 and then look at the debug log to see what the literal text is on the keys in the map. You will probably find that the string in the map doesn't exactly match "Customer - Direct". (Maybe a typo, extra whitespace characters, etc.)

  • Thanks, I changed the line to Schema.SObjectType.Account.getRecordTypeInfosByName(); but I'm still getting the same error. I'll also add in the null check like you advised. – Matt Mc Jun 18 '15 at 16:16
  • @MattMc, I also added some detail about troubleshooting to view what is contained in the map. – Mark Pond Jun 18 '15 at 16:18
1

Instead of doing multiple describe calls you can get the recordtypeid using below method.

Id variablename = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();

You should use your record type name within single quotes.

Hope this helps

1

I found the solution. I changed the Accounts Object settings to make the user select the Account record type before they were allowed to enter any more information (and before the trigger fires), so now my code works as intended. If anyone is interested, here's my final code:

trigger SetsLocationAddress on Account (after insert) {

    List<PSI_Location__c> locationToInsert = new List<PSI_Location__c>();

    //finds record type
    String rtCustomer = 'Customer';
    Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Account.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtInfoCustomer =  rtMapByName.get(rtCustomer);
    id rtTypeIdCustomer = rtInfoCustomer.getRecordTypeId();

    for(Account accountInLoop : Trigger.new )
    {
        //tests if account is type Customer
        if( accountInLoop.RecordTypeId == rtTypeIdCustomer )
        {
            //creates new location record based on shipping address on account record 
            locationToInsert.add(new PSI_Location__c(
                Name = 'Location 1',            
                Account__c = accountInLoop.Id, 
                Street_Address__c = accountInLoop.ShippingStreet, 
                City__c = accountInLoop.ShippingCity, 
                State__c = accountInLoop.ShippingState, 
                Zip_Code__c = accountInLoop.ShippingPostalCode));  


        }
    }

    insert locationToInsert;
}

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.