• The_Cowen_Group
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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!

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!