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
NightShadeNightShade 

URGENT Help Needed :-Copy RecordTypeId into a field

Hi ,

 

I need to copy the RecordTypeId into a field called Prev_Record_Type which is a text field on opportunity. There are multiple record types on the Object and I need to get the selected recordtypId.Can someone please help me with the Trigger code since  i am not all experienced in this and urgently need help .

 

Thanks and Regards

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Not sure what the use case is, but before you code it, you can do that via workflow unless there is some complex logic there - just use a formula to update the text field

All Answers

TinkuTinku

use this for comparing the recordtypes:

 

    RecordType accRecType=[Select Id From RecordType Where Name=:'RecordTypeName And SobjectType=:'Account' limit 1];

 

And inside your if loop you can compare the two recordtype ID for an opportunity.

 

    for(Opportunity opp:Trigger.New)

{

     if(opp.RecordTypeId=accRecType.Id)

{

}

}

 

 

 

Cory CowgillCory Cowgill

Before you go down the Trigger path, consider if you can use Field History to accomplish this.

 

You can use Field History to track the changes of the Record Type.

 

Setup -> Customize -> Opportunity -> Fields - > Enable Field Tracking. Then select "Opportuinty Record Type".

 

 

NightShadeNightShade

I tried writing the trigger as follows and got the below error . Please advice .

 

Thanks

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger RcdType caused an unexpected exception, contact your administrator: RcdType: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.RcdType: line 4, column 26

 

 

 

trigger RcdType on Opportunity (before insert, before update) {
      
   
   RecordType oppRecType=[Select Id From RecordType Where Name=:'RecordTypeName' And SobjectType=:'Opportunity' limit 1];
   
    for(Opportunity opp:Trigger.New)

{

     if(opp.RecordTypeId==oppRecType.Id)

{
   opp.Prev_Record_Type__c =opp.RecordTypeId;
}

}

}
  
BritishBoyinDCBritishBoyinDC

Not sure what the use case is, but before you code it, you can do that via workflow unless there is some complex logic there - just use a formula to update the text field

This was selected as the best answer
hitzhitz

hi,

 

Use debug log to find error..

 

you get this error because  your query result comes with no record [zero record];

 

plz check your record

 

 

 

NightShadeNightShade

Thank you all I resolved the problem by using a workflow which fires only when the record is created to copy the record type Id .