You need to sign in to do that
Don't have an account?

Trigger not writing record type name... Really annoying
Sorry... what am I doing wrong here??
A simple trigger to get the record type name so I can run logic on it in workflow rules....
trigger ticketBefore on ticket__c (before insert, before update) { //Get the recods that triggered this event List<ticket__c> tkts = [ select recordType.name, recordTypeName__c from ticket__c WHERE id IN :Trigger.newMap.keySet() ]; for (ticket__c t : tkts) { t.recordTypeName__c = t.recordType.name; system.debug('\n\nTHE RECORD TYPE ID IS: ' + t.recordTypeId); system.debug('\n\nTHE TICKET RECORD TYPE ID IS: ' + t.recordTypeName__c); } }
I don't see why this doesnt work... It's really frustrating...
DEBUG LOG returns results correct but it doesn't write to the field?!?
16.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
11:47:05.587|EXECUTION_STARTED
11:47:05.587|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
11:47:05.587|CODE_UNIT_STARTED|[EXTERNAL]|01qA0000000ooan|ticketBefore on ticket trigger event BeforeUpdate for [a0XA0000001eHhf]
11:47:05.588|SOQL_EXECUTE_BEGIN|[4]|Aggregations:0|
select
recordType.name,
recordTypeName__c
from ticket__c
WHERE id IN :Trigger.newMap.keySet()
11:47:05.589|METHOD_ENTRY|[9]|MAP.keySet()
11:47:05.589|METHOD_EXIT|[9]|MAP.keySet()
11:47:05.596|SOQL_EXECUTE_END|[4]|Rows:1
11:47:05.596|METHOD_ENTRY|[15]|System.debug(ANY)
11:47:05.596|USER_DEBUG|[15]|DEBUG|
THE RECORD TYPE ID IS: 012A0000000pCocIAE
11:47:05.596|METHOD_EXIT|[15]|System.debug(ANY)
11:47:05.596|METHOD_ENTRY|[16]|System.debug(ANY)
11:47:05.596|USER_DEBUG|[16]|DEBUG|
THE TICKET RECORD TYPE ID IS: Logistics
11:47:05.596|METHOD_EXIT|[16]|System.debug(ANY)
11:47:05.596|CUMULATIVE_LIMIT_USAGE
11:47:05.596|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 20
Number of query rows: 1 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 100
Number of script statements: 4 out of 10200
Maximum heap size: 0 out of 3000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 20
11:47:05.596|CUMULATIVE_LIMIT_USAGE_END
11:47:05.597|CODE_UNIT_FINISHED|ticketBefore on ticket trigger event BeforeUpdate for [a0XA0000001eHhf]
11:47:05.605|CODE_UNIT_STARTED|[EXTERNAL]|Validation:ticket:a0XA0000001eHhf
11:47:05.605|VALIDATION_RULE|03dA0000000lFPK|TKT100
11:47:05.606|VALIDATION_FORMULA|subject__c = null|subject__c=BES Server problems
11:47:05.606|VALIDATION_PASS
11:47:05.606|CODE_UNIT_FINISHED|Validation:ticket:a0XA0000001eHhf
11:47:05.661|CODE_UNIT_FINISHED|TRIGGERS
11:47:05.662|EXECUTION_FINISHED
Ok... I built one based on yoru model that works in both circumstances!!
for (ticket__c t : Trigger.new) {
rtIds.add(t.recordTypeId);
}
List<RecordType> rts = [
select
id,
name
from recordType
WHERE id IN :rtIds
];
for (integer i = 0; i < trigger.new.size(); i++) {
Trigger.new[i].recordTypeName__c = rts[i].name;
All Answers
In your code, t is not a reference to a ticket__c in Trigger.new; It's a reference to a ticket__c returned by your query.
You would need to update that ticket__c to get the changes to take effect - but that would be bad practice since you're in an update trigger for ticket__c.
You should iterate over the new list instead of requerying the ticket__c table.
You're right of course, but it still returns null...
Ah, right. I think this is what you need then.
Thats much better, thank you, except now there is a different problem.
It appears that the update is always one update behind.
This leads me to believe that the record type id is changed on the record AFTER the update is requested... Which AFAIK, you cannot accomodate for, because I can't write a self referencial after update trigger... I mean.. .an after update cannt trigger an update on itself. I suppose cause it coul dmake inifinate updates...
Any ideas?
Thanks for your help...
Oh, right. That still uses the old record type id. Well the fix is not pretty, but I think it will work.
l want to have your apex babies.
Thank you.
I'm sorry... I've run into another problem... it doesnt work on inserts... Just updates...
boy this seems needlessly complex...
Ok... I built one based on yoru model that works in both circumstances!!
for (ticket__c t : Trigger.new) {
rtIds.add(t.recordTypeId);
}
List<RecordType> rts = [
select
id,
name
from recordType
WHERE id IN :rtIds
];
for (integer i = 0; i < trigger.new.size(); i++) {
Trigger.new[i].recordTypeName__c = rts[i].name;
Awesome. =D