+ Start a Discussion
anitha20anitha20 

how to write trigger for recortype

Can any one tel me how to write trigger for recortype

anil 007anil 007

hii anitha,

 

try this one

 

trigger rcdtypechg on Contact (before update) {


set<Id> chngRcdtype=new set<Id>();
    
    for(Contact co : Trigger.New)
    {
        if(co.What_are_you_applying_to__c=='Interlochen Summer Arts Camp')
            chngRcdtype.add(co.Id);
            
    }
    
    List<Enrollment_Opportunity__c> app=[Select Id,RecordTypeId from Enrollment_Opportunity__c where Enrollment_Opportunity__c.Applicant__c IN:chngRcdtype];
    for(Enrollment_Opportunity__c ap:app)
    {
        ap.RecordTypeId='012T00000004cfd';
    }
    
    update app;
    
}

sales4cesales4ce

To add up,

As a best practice, never hard-code an  ID value in your code.

You can get the Recordtype Id by querying RecordType Object.

 

trigger rcdtypechg on Contact (before update) {


set<Id> chngRcdtype=new set<Id>();
   RecordType rectype=[Select Id From RecordType Where Name='Your RecordType Name']; 
    for(Contact co : Trigger.New)
    {
        if(co.What_are_you_applying_to__c=='Interlochen Summer Arts Camp')
            chngRcdtype.add(co.Id);
            
    }
    
    List<Enrollment_Opportunity__c> app=[Select Id,RecordTypeId from Enrollment_Opportunity__c where Enrollment_Opportunity__c.Applicant__c IN:chngRcdtype];
    for(Enrollment_Opportunity__c ap:app)
    {
        ap.RecordTypeId=recType.Id;
    }
    
    update app;
    
}

 

Hope it helps.

 

Sales4ce