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
cignuscignus 

Record Type Updating Problem

Hi

 

I have added two record types(RT1,RT2) to Contact. I want to set the value to RT2 if some condition is met. (RT1 is default)

 

I added this code to my controller before updating.

		c.RecordType =  [select Id from RecordType where Name = 'RT2' and SobjectType = 'Contact'];
		update c;

 

When I create a new Contact the RecordType is correctly set to RT1.

But when I want to change it to RT2 it does not change. (the line is executed and the recordTypeId value is changed (I debugged))

 

Then I moved this codes into a trigger

trigger updateRecordType on Contact (before update) {
    system.debug('Entered Contact Trigger');
    for (Contact c: Trigger.new) {
        system.debug('c.RecordType before trigger = '+c.RecordType.Name +' '+ c.RecordType.id +' '+c.RecordTypeId );
        if (condition){
            c.RecordType =  [select Id,Name from RecordType where Name = 'RT2' and SobjectType = 'Contact'];
            system.debug('c.RecordType = '+c.RecordType.Name +' '+ c.RecordType.id +' '+c.RecordTypeId );
        }else{
            c.RecordType =  [select Id,Name from RecordType where Name = 'RT1' and SobjectType = 'Contact'];
            system.debug('c.RecordType = '+c.RecordType.Name +' '+ c.RecordType.id +' '+c.RecordTypeId );
        }
    }
}

 From all the debug lines I get the correct RT values, but still RT is not changed after the record is saved.

 

Cheers

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

In you condition assigned the record type id to the contact contact.recordtype id field to change the record type.

 

Try the below code snippet as reference:

trigger updateRecordType on Contact (before update)

{

                system.debug('Entered Contact Trigger');

                for (Contact c: Trigger.new)

                {

                                system.debug('c.RecordType before trigger = '+c.RecordType.Name +' '+ c.RecordType.id +' '+c.RecordTypeId );

                                if (condition)

                                {

                                                RecordType conrt =  [select Id,Name from RecordType where Name = 'RT2' and SobjectType = 'Contact'];

                                                system.debug('conrt.RecordType = '+conrt.RecordType.Name +' '+ conrt.RecordType.id +' '+conrt.RecordTypeId );

                                                c.RecordTypeId= conrt.id;                                                           

                                }

                                else

                                {

                                                RecordType conrt  =  [select Id,Name from RecordType where Name = 'RT1' and SobjectType = 'Contact'];

                                                system.debug('conrt.RecordType = '+conrt.RecordType.Name +' '+ conrt.RecordType.id +' '+conrt.RecordTypeId );

                                                c.RecordTypeId= conrt.id;

                                }

                }

}

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

I haven’t found that your trigger is for insert also. This is only for update as I can see in your code. Now can you please tell me the condition what you are using inside the if ????

 

 

cignuscignus

I did not need the trigger for insert because it was working fine when I was inserting the record.

The problem happens when I want to update the record.

 

The condition is a simple condition of checking some field's value against null. ( FirstName == null ). and there is no problem with condition because I get the correct debug lines in my console.

Navatar_DbSupNavatar_DbSup

Hi,

In you condition assigned the record type id to the contact contact.recordtype id field to change the record type.

 

Try the below code snippet as reference:

trigger updateRecordType on Contact (before update)

{

                system.debug('Entered Contact Trigger');

                for (Contact c: Trigger.new)

                {

                                system.debug('c.RecordType before trigger = '+c.RecordType.Name +' '+ c.RecordType.id +' '+c.RecordTypeId );

                                if (condition)

                                {

                                                RecordType conrt =  [select Id,Name from RecordType where Name = 'RT2' and SobjectType = 'Contact'];

                                                system.debug('conrt.RecordType = '+conrt.RecordType.Name +' '+ conrt.RecordType.id +' '+conrt.RecordTypeId );

                                                c.RecordTypeId= conrt.id;                                                           

                                }

                                else

                                {

                                                RecordType conrt  =  [select Id,Name from RecordType where Name = 'RT1' and SobjectType = 'Contact'];

                                                system.debug('conrt.RecordType = '+conrt.RecordType.Name +' '+ conrt.RecordType.id +' '+conrt.RecordTypeId );

                                                c.RecordTypeId= conrt.id;

                                }

                }

}

 

This was selected as the best answer
cignuscignus

Thanks man

This one worked. can you explain the difference with what I wrote?