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
ThomasmThomasm 

Trigger Based on record type

I am looking to have my trigger used based on the record type of the custom object.  So only use the trigger if recordtype = daily visit.  Here is the trigger i am using.  Any suggestions on how this could work

 

 trigger SV_Cases on SiteVisit__c (before insert) {
 List<Case>cases=new List<Case>();
    
    for(sitevisit__C b:Trigger.new)
        {
             
     if(b.baseboard_stairway__c == 'False')
    {
    Case newCase=new Case(subject='Test',Status='New',Origin='Web');
    insert newCase; 
}
}

}

donobdonob
trigger SV_Cases on SiteVisit__c (before insert) {
 List<Case>cases=new List<Case>();
    
    for(sitevisit__C b:Trigger.new){
             
       if( sitevisit__c.RecordType = 'daily visit' ){
          if(b.baseboard_stairway__c == 'False'){
             Case newCase=new Case(subject='Test',Status='New',Origin='Web');
             cases.add( newCase ); 
          }
       }
    }
    if( !cases.isEmpty() ){
       insert cases;
    }
}

 

The above code will loop through your Sitevisit__c object, check if the Record Type is  daily visit, and if so continue with the code.  I moved the insert statement out of the for loop in order to bulkify it and avoid hitting governor limits (good practice).  

I believe this should work.  Have fun.

Subhash GarhwalSubhash Garhwal

Hi

 

You can check record type Name Like this way

 

trigger SV_Cases on SiteVisit__c (before insert) {

 

List<Case>cases=new List<Case>();

 

for(SiteVisit__c sVisit : [Select Id, RecordTypeId, RecordType.Name From SiteVisit__c Where Id IN : Trigger.newMap]) {

if(sVisit.RecordType.Name == 'Test') {
//Your Logic here
}
}

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you 

ThomasmThomasm

i am getting a compile error: Variable does not exist: sitevisit__C.REcordType

Subhash GarhwalSubhash Garhwal
Please try this one
//To get Id
sitevisit__C.RecordTypeId

//To get name
sitevisit__C.RecordType.Name