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
sfdc dev 2264sfdc dev 2264 

Apex trigger help needed

Hi,

I need help on the following requirement as follows,

i am converting a lead to an account on coversion the account record gets created

I have a field called as "New Account Type" based on which i have to change the record type of account automatically

for eg : if the account type is "Agency" the account record type should be set as "Agency"

also i have 2 fields called 'IATA' and 'QID'

i have to copy the value of iata to qid for 'agency recordtype accounts' alone

I have written a trigger , but didnt work , kindly help me pls
 
MY TRIGGER CODE :

trigger IntakeFormTrigger on account (before insert, before update) {
Map<String, Id> typeMap = New Map<String, Id>();

   for(RecordType rt: [Select DeveloperName, Id From RecordType Where sObjectType = 'Account']) {
      typeMap.put(rt.DeveloperName, rt.Id);
   }

   for (Account agmt : trigger.new)  {

      recordtype agmtRT = [select id, developername from recordtype where id=:agmt.recordtypeid];
     // recordtype IFRT = [select id, developername from recordtype where developername = 'Intake_Form'];
      // If the Record Type = Intake Form
     // if (agmtRT.id == IFRT.id) { 
            // And the Agreement Category on the record = Services
            if (agmt.New_Account_Type__c == 'Agency') {
                // Then automatically change the Record Type to Agency.
                id recid = typeMap.get('Agency Account');
                recordtype rectype = [select id, developername from recordtype where id=:recid];
               agmt.RecordTypeid = rectype.id; 
            }
       //}
   }
}

Kindly help me with this 2 requirements ,

Thanks in Advance
Shashikant SharmaShashikant Sharma
Hi,

As you have created typeMap where Key is Record Type Developer Name so while fetching you need to provide key as Developer Name.

SO your code should be like below ( If developer name is something else then change accrdingly ). 
 
id recid = typeMap.get('Agency_Account');

You do not need to query it again and can assign directly like:
 
agmt.RecordTypeid = recid;

Thanks
Shashikant