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
The_Cowen_GroupThe_Cowen_Group 

Lead Conversion Trigger - Alter RecordType

Hi!

 

I'm creating a (what I think should be) simple APEX trigger to set an Account record type to a custom Lead field when a Lead is converted to a Contact and a new Account is created. 

 

 

trigger LeadConvert on Lead (after update) {
    if (Trigger.new.size() == 1) {
        if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
            if (Trigger.new[0].ConvertedAccountId != null) {            
                // update the new account type
                Account a = [Select a.Id, a.Type, a.RecordType.Id, a.RecordType.Name From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
                a.RecordType.Id = [Select Id from RecordType where Name = :Trigger.new[0].Company_Type__c and SobjectType = 'Account'].Id;
                update a;
            }
        }
    }
}

 Unfortunately, when I try this trigger in the sandbox, I get this error:

 

Error: System.DmlException: Update failed. First exception on row 0 with id 00QQ0000005ZPRUMA4; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadConvert: execution of AfterUpdate caused by: System.SObjectException: Field is not writeable: RecordType.Id Trigger.LeadConvert: line 7, column 17: [] (System Code) External entry point

 

I would appreciate any insight on how to change this seemingly immutable field.

 

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
ericszulcericszulc

I pretty sure you should be updating Account.RecordTypeId not Account.RecordType.Id

All Answers

ericszulcericszulc

I pretty sure you should be updating Account.RecordTypeId not Account.RecordType.Id

This was selected as the best answer
The_Cowen_GroupThe_Cowen_Group

Thanks so much!