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
dave23dave23 

How to set RecordType in a trigger?

Hi,

 

I am trying to set the RecordType in a before insert trigger for web to case input. The trigger code is:      

trigger CaseSetupTrigger on Case (before insert){
for (Case c: Trigger.new) {
c.RecordType = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case'];
}
}

I know the code above works as I can see the correct RecordTypeID in the debug log for some workflow rules that use the RecordType. The problem is that when the case is finally saved the RecordType has been overwritten with the default RecordType for unassigned cases. There are no workflow rules that set the RecordType. The workflow rules also use $RecordType.Name which is the name of the default recordType and not Canada as would be expected.

 

I have tried changing the case setting for "Choose the desired behavior to use when applying assignment rules to manually created records:" to "Keep existing Record Type" but this has no effect. 

 

I'm guessing that this has something to do with the order that triggers web to case and workflow rules fire but I can't seem to pin it down.

 

Any suggestions on how to get the record type set in a before insert trigger to actually stick for a web to case and not get changed?

 

Thanks

 

J&A-DevJ&A-Dev

Hm, I don't know if that was just a typo when you pasted your code, but change the last part of your SOQL to actually point to the Id field and the name of the field to RecordTypeId:

 

c.RecordTypeId = [select Id from RecordType where Name = 'Canada' and SobjectType = 'Case'].Id;

 

 

xnerd00xxnerd00x

also, you should really put that select statement outside of that for loop to handle bulk processing.

 

In addition, there is a RecordType object out there that you can use instead of using up a SOQL query.

 

Schema.SObjectType.Account.getRecordTypeInfosByName().get('Advertiser').getRecordTypeId();

 


 

dave23dave23

J&A-Dev - I was trying to simplify the code before posting so a few typos!

 

znerd00x - Thanks for the tip on the recordtype lookup. I had the query inside the loop as there was a lookup that was based on the input fields in the case involving some custom objects. For this example i had hard coded the lookup. I gather that using a list or set would be better than using the query inside the loop.

 

However the original problem still exists. From the Apex log it looks like SF does the case assignment rules after the trigger but before the workflow processing. Since the owner was blank the default assignment rules kick in and the default owner is assigned which in turn sets the record type to that of the default owner. So as a workaroujnd I set the owner in the trigger which then sets the record type and all works properly afterwards.

 

Thanks for your replies

 

Dave