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
Devendra Hirulkar 3Devendra Hirulkar 3 

How to run a trigger for specific record types

Hello friends
i have created an trigger that  copy one obj to another 
but i have  three record type like recordtype1, recordtype2, recordtype3  when i selected recordtype3 only the case the trigger have been fire otherwise it will not fire 
so what i needed, to do this

following is my trigger

trigger copypro  on Subsc__c  (after insert,after update) 
{
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();
  set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's

  for (Subsc__c s : Trigger.new)
  {
    listIds.add(s.Company_Name__c);
      
    if(s.Product__c     != null)
    {
       cObjectID.add(s.Product__c    );//takes the Lookup Record & Add that ID's in cObjectID set
     }
  }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        
        for(Subsc__c s : trigger.new)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                 Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
                Account myacc = acc.get(s.Company_Name__c);
                 myacc.Product_Name__c =pro;
                update Acc.values();
            }
        }
    }
   
}
Best Answer chosen by Devendra Hirulkar 3
John PipkinJohn Pipkin
Akram, You definitely have the right idea. There are a couple of things in the code that need to be fixed. Mainly removing the DML call and SOQL statement to outside the FOR loop. Governor limits can be quickly reached by doing that. See below:
trigger copypro  on Subsc__c  (after insert,after update) 
{
    Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'recordtype3'].Id;
    List<Subsc__c> subscList = new List<Subsc__c>();

    for (Subsc__c s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            subscList.add(s);
    }

    Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
    List<Id> listIds = new List<Id>();
    set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's
    Map<ID, Account> updateMap = new Map<ID, Account>();
  
    for (Subsc__c s : subscList)
    {
        listIds.add(s.Company_Name__c);
      
        if(s.Product__c     != null)
        {
            cObjectID.add(s.Product__c    );//takes the Lookup Record & Add that ID's in cObjectID set
        }
    }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
        
        for(Subsc__c s : subscList)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                Account myacc = acc.get(s.Company_Name__c);
                if(myacc != null){ //always check for nulls to avoid null pointer exceptions
                    myacc.Product_Name__c =pro;
                    updateMap.put(myacc.Id,myacc);
                }
            }
        }
        update updateMap.values();
    }
   
}

 

All Answers

SarfarajSarfaraj
Hi, Use this,
 
trigger copypro  on Subsc__c  (after insert,after update) 
{
  Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'recordtype3'].Id;
  List<Subsc__c> subscList = new List<Subsc__c>();
  for (Subsc__c s : Trigger.new)
	if(s.RecordTypeId == recordTypeId)
		subscList.add(s);
  Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();
  set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's
  
  for (Subsc__c s : subscList)
  {
    listIds.add(s.Company_Name__c);
      
    if(s.Product__c     != null)
    {
       cObjectID.add(s.Product__c    );//takes the Lookup Record & Add that ID's in cObjectID set
     }
  }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        
        for(Subsc__c s : subscList)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                 Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
                Account myacc = acc.get(s.Company_Name__c);
                 myacc.Product_Name__c =pro;
                update Acc.values();
            }
        }
    }
   
}
--Akram
John PipkinJohn Pipkin
Akram, You definitely have the right idea. There are a couple of things in the code that need to be fixed. Mainly removing the DML call and SOQL statement to outside the FOR loop. Governor limits can be quickly reached by doing that. See below:
trigger copypro  on Subsc__c  (after insert,after update) 
{
    Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'recordtype3'].Id;
    List<Subsc__c> subscList = new List<Subsc__c>();

    for (Subsc__c s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            subscList.add(s);
    }

    Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
    List<Id> listIds = new List<Id>();
    set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's
    Map<ID, Account> updateMap = new Map<ID, Account>();
  
    for (Subsc__c s : subscList)
    {
        listIds.add(s.Company_Name__c);
      
        if(s.Product__c     != null)
        {
            cObjectID.add(s.Product__c    );//takes the Lookup Record & Add that ID's in cObjectID set
        }
    }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
        
        for(Subsc__c s : subscList)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                Account myacc = acc.get(s.Company_Name__c);
                if(myacc != null){ //always check for nulls to avoid null pointer exceptions
                    myacc.Product_Name__c =pro;
                    updateMap.put(myacc.Id,myacc);
                }
            }
        }
        update updateMap.values();
    }
   
}

 
This was selected as the best answer
Devendra Hirulkar 3Devendra Hirulkar 3
hello sir my problem will be solve
just one change needed that is
  NEW  (Id recordTypeId = Schema.SObjectType.Subsc__c.getRecordTypeInfosByName().get('recordtype3').getRecordTypeId();)

OLD(Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'recordtype3'].Id;)
John PipkinJohn Pipkin
I'm glad that your issue is resolved. The "old" and "new" way of getting the record type ids produce the same result, just different methods of attaining it. I believe the only difference accessing from Schema doesn't count against the SOQL limit, but not 100% sure.