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
steve456steve456 

After Insert Trigger on Same object ???

Can we write an after insert trigger on the same object 

 

 

My scenario is this way

 

 

I have three objects Sales , Order ,Inventory

 

 

Whenever a Sales Record is created an Order is created automatically

 

inventory is a field on Order which is a lookup type 

 

 

*****

I have a scenario where I have a before insert , before update trigger which fires based on Inventory field on Order 

 

The update part of the trigger is working fine ,but on an Insert the system doesnot have any value in Inventory field on Order  as it gets updated after the record is saved.Is my interoretation right or is there is something that I am missing as my code is not working on insert

 

The code is 

 

trigger UpdateAdditionalRevenueBasedOnInventory on Order__c (before insert , before update) {

Set<ID> inventoryIds=new Set<Id>();


 for(Order__c order : Trigger.new) {
         
       
         
                inventoryIds.add(order.Inventory__c);
             
        
 }
 
     
     
     List<Inventory__c> inventoryList = [Select Id, Name, Product__c, Product__r.Name from Inventory__c where Id IN: inventoryIds];
     Map<Id,String> inventoryMap=new Map<Id,String>();
     for(Inventory__c inv :inventoryList){
        if(inv.Product__c!=null)
        
        inventoryMap.put(inv.Id,inv.Product__r.Name);
     }  
         
               
     
  
 String recordtypeid=[Select Id from RecordType where Name='P18VG' and SobjectType='Order__c'].Id;    

   
    if(inventoryMap.size()>0 && inventoryMap!=null) {
        for(Order__c order:Trigger.new)
        {
               if(Trigger.isInsert||Trigger.oldMap.get(order.Id).Inventory__c != Trigger.newMap.get(order.Id).Inventory__c) {
              
                     if(order.RecordTypeId==recordtypeid ){
                   
                         Date myDate = date.newinstance(2013, 3, 11);
                         
                         if(inventoryMap!=null && 
                            order.Id!=null && 
                            order.Inventory__c != null && 
                            inventoryMap.get(order.Inventory__c) != null && 
                            inventoryMap.get(order.Inventory__c).length()>=8 && 
                            inventoryMap.get(order.Inventory__c).substring(0,8)=='V-Sample'&&                         
                            order.Date__c >=myDate
                            ){
        
                         
                             order.Revenue__c=15;
                            }
                            else{
                             
                                 order.Revenue__c=0;
                            }
                             
        
                     }
              }
                        
                        
              
                 
        }
      
    }
}