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

Record Type field update trigger
I have a following trigger to update the record type lookup. I'm taking value from the text field and trying to update lookup but its not working. can someone please help?
trigger UpdateLookup on Manager__c (after insert, after update) { try { set<string> rtset = new set<string>(); for (Manager__c inq : trigger.new) { rtset.add(inq.Record_Type__c); } Map<String, RecordType> rtmap = new Map<String, RecordType>([Select id from RecordType Where Name in :rtset]); for (Manager__c inq : trigger.new) { inq.RecordTypeId = rtmap.get(inq.Record_Type__c).id; } } catch(Exception e) { } }
You have one text field on Manager__c and then based on that text field you want to provide the recordtYpeId is this the req??
You should use before insert and before update event in this....instead of after insert and after update..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I assume you are storing recordtype name in inq.Record_Type__c field.
Map<String, RecordType> rtmap = new Map<String, RecordType>([Select id from RecordType WhereName in :rtset]);
Above query returns map with key as id of recordtype and value as recordtype detail. But in your case, you have recordtype name and need to find id of recordtype. So create a map<String,id> with name as key and value as recordtype id. Below is updated code :
trigger UpdateLookup on Manager__c (after insert, after update) {
try {
set<string> rtset = new set<string>();
for (Manager__c inq : trigger.new) {
rtset.add(inq.Record_Type__c);
}
Map<String,id> rtmap=new Map<String,id> ();
for(RecordType rr:[Select id,name from RecordType Where Name in :rtset]){
rtmap.put(rr.name,rr.id);
}
for (Manager__c inq : trigger.new) {
inq.RecordTypeId = rtmap.get(inq.Record_Type__c).id;
}
}catch(Exception e) {
}
}
Hope this will help you.
[If it solves your problem, please mark it as solution]
Thanks,
Sunil Kumar