You need to sign in to do that
Don't have an account?
cignus
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
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
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 ????
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.
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;
}
}
}
Thanks man
This one worked. can you explain the difference with what I wrote?