+ Start a Discussion
Bertrand DBBertrand DB 

Trigger: run if parent record type id is a specific one

Hello,

I have a trigger that is updating a value on a parent record.
The trigger run on the custom object "Cost_line_item__c" and updates value on the parent opportunity.

I would like this trigger to run only for a specific record type id of the opportunity (so, the parent object).

My trigger (working) is:
Trigger UpdateOpportunityBV on Cost_line_item__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> opportunityIds = New List<ID>();
	List<ID> mylistIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Cost_line_item__c oli: Trigger.New){
            if(oli.Opportunity__c  != null){
                opportunityIds.add(oli.Opportunity__c);
				mylistIds.add(oli.Id);
            }  
        }
    }
    If(Trigger.IsDelete){
        For(Cost_line_item__c con: Trigger.Old){
            opportunityIds.add(con.Opportunity__c);
			mylistIds.add(con.Id);
        }
    }
     
    List<opportunity> opportunityListToUpdate = New List<opportunity>();
    Decimal RollupAmount = 1;
    For(Opportunity opt: [Select Id, Number_of_beneficiaries__c, (Select ID, Value__c FROM Cost_line_items__r WHERE Used_for_BV_calculation__c=true) FROM Opportunity WHERE ID = :opportunityIds]){
		for(Cost_line_item__c con:opt.Cost_line_items__r){
			RollupAmount = RollupAmount * con.Value__c;
		}
		opt.tech_cost_line_calculation__c = RollupAmount;
        opt.Amount = RollupAmount * opt.Number_of_beneficiaries__c;
		opportunityListToUpdate.add(opt);
    }
     
     
    try{
        Update opportunityListToUpdate;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}
I tried to replace the line 
if(oli.Opportunity__c  != null){
By
if(oli.Opportunity__c  != null && oli.Opportunity__r.RecordTypeid=='0121r0000003Qzk')
But it doesn't work (nothing happens).

Any suggestions?
Best Answer chosen by Bertrand DB
Manohar kumarManohar kumar

Hi  Bertrand, 
Have you tried adding RecordTypeid in the query ? I think it will work. 

In the line 23 you can addd extra condition. 

For(Opportunity opt: [Select Id, Number_of_beneficiaries__c, (Select ID, Value__c FROMCost_line_items__r WHERE Used_for_BV_calculation__c=true) FROM Opportunity WHERE ID =:opportunityIds and RecordTypeid=='0121r0000003Qzk')])  

pls let me know if this works

All Answers

Manohar kumarManohar kumar

Hi  Bertrand, 
Have you tried adding RecordTypeid in the query ? I think it will work. 

In the line 23 you can addd extra condition. 

For(Opportunity opt: [Select Id, Number_of_beneficiaries__c, (Select ID, Value__c FROMCost_line_items__r WHERE Used_for_BV_calculation__c=true) FROM Opportunity WHERE ID =:opportunityIds and RecordTypeid=='0121r0000003Qzk')])  

pls let me know if this works

This was selected as the best answer
Bertrand DBBertrand DB
Thank you Manohar,
It works well! I just had to correct small things in your suggestion (extra '=' and extra ' ( ' ).
It is:
FROM Opportunity WHERE ID = :opportunityIds and RecordTypeid='0121r0000003Qzk'