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
UrvikUrvik 

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) {  
}
}

 
PuneetsfdcPuneetsfdc
Try your same code in before event ie before insert/update
sandeep sankhlasandeep sankhla
Hi rick13,

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 
Sunil02KumarSunil02Kumar
Hi Rick,

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