+ Start a Discussion
HelloSanHelloSan 

how to get the recordtype name from the opportunity object and assign to the text field in the child object in apex trigger,how can i achieve this

Best Answer chosen by HelloSan
David ZhuDavid Zhu
Hi,
You can use the following code to get recordtype name in opportunity trigger.
 
for(opportunity opp : trigger.new)
    {
        string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
    }

David

All Answers

KaranrajKaranraj
You don't need apex trigger for this. You can do it by sinply creating formula field in the child object
Opportunity__r.RecordType.Name

Thanks,
Karanraj
http://www.karanrajs.com
HelloSanHelloSan
thanks karan but my requirement is to achieve this in apex trigger,when the opportunity is created the child field of data type text should assign with opportunity recordtype name.
David ZhuDavid Zhu
Hi,
You can use the following code to get recordtype name in opportunity trigger.
 
for(opportunity opp : trigger.new)
    {
        string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
    }

David
This was selected as the best answer