Trigger on a certain Account Record Type

2k Views Asked by At

So i have written a trigger that works perfectly fine and does exactly what i want it to do, but the thing is that it does the job on all the account, and i want it to work only on one record type of the accounts. Can someone tell me what to add to my trigger so i can make it work only on one record type?

The following is my handler class:

public class AP03_OpportunityLineItem {
    public static void preventmultipleOpportunityLineItems(List<OpportunityLineItem> listOppLineItems){
        Set<Id>opportunityIds = new Set<Id>();
        // get all parent IDs
        for(OpportunityLineItem oli : listOppLineItems)
        {
            //Condition to pick certain records
            opportunityIds.add(oli.OpportunityId);
        }
        // query for related Opportunity Line Items
        Map<Id, Opportunity> mapOpportunities = new Map<Id, Opportunity>([SELECT ID,
                                                                          (SELECT ID 
                                                                           FROM OpportunityLineItems) 
                                                                          FROM Opportunity 
                                                                          WHERE ID IN :opportunityIds]);
        // opp counter of new records
        Map<Id, Integer> mapOppCounter = new Map<Id, Integer>();
        for(OpportunityLineItem oli : listOppLineItems)
        {
            if(mapOppCounter.containsKey(oli.OpportunityId))
            {
                mapOppCounter.put(oli.OpportunityId, mapOppCounter.get(oli.OpportunityId)+1);
            }
            else
            {
                mapOppCounter.put(oli.OpportunityId, 1);
            }
        }
        //loop to add error if condition violated
        for(OpportunityLineItem olitems : listOppLineItems)
        {
            if(mapOpportunities.get(olitems.OpportunityId).OpportunityLineItems.size()+mapOppCounter.get(olitems.OpportunityId)>1 || olitems.Quantity > 1)
            {
                olitems.addError('Ce client peut seulement loué un seul véhicule.');
            }
        }
    }
}

The following is my PAD class:

public class PAD 
{
    public static String bypassTrigger; //List of bypassed triggers

        public static final User user;

        static {
            user = [Select BypassApex__c
                   from User 
                   where Id=:UserInfo.getUserId()];
            bypassTrigger = ';'+ user.BypassApex__c+ ';';                                                                                                                                   
            System.debug('>>>>> PAD constructor : END <<<<<'+bypassTrigger);
        }

        /**
        * Method used for the class PAD
        * @param c object of type JonctionServicePrestation__c
        * @return boolean
        */
         public static boolean canTrigger(string Name){
            return (bypassTrigger.indexof(';' + Name + ';') == -1);

         }



}

And the following is my Trigger:

trigger OpportunityLineItemBeforeInsert on OpportunityLineItem (before insert) {
    if(PAD.canTrigger('AP03_OpportunityLineItem')){
        AP03_OpportunityLineItem.preventmultipleOpportunityLineItems(Trigger.new);
    }
}
1

There are 1 best solutions below

0
Gareth Jordan On

You would need to loop through your opportunitiesproducts and build a list of opportunity Id, then query the Opportunity whose Accounts in the list with the record type you want to match, and build a set of the ids that match the specified record type then check if the set contains the accountId of the opportunity being process to know if the skip or process it.

Set<Id> recordTypeOpp = new Set<ID>();
SET<Id> opportunityIds = new Set<Id>();
Id recordTypeIdYouWant = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();

for(OpportunityLineItem  item : listOppLineItems){
    opportunityIds.add(item.OpportunityId);
}

for(Opportunity item : [SELECT Id FROM Opportunity WHERE opportunityIds IN :opportunityIds and Account.RecordTypeId = :recordTypeIdYouWant]){
    recordTypeOpp.add(item.Id);
}

 for(OpportunityLineItem olitems : listOppLineItems)
 {
    if(recordTypeOpp.contains(olitems.OpportunityId)){
      //do processing
    }
     else {
       continue;
    }
 }