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
kiran k 12kiran k 12 

Trigger problem is during inserting a new record

I have written a trigger.that logic is working fine for me . when i inserting a new record ,the trigger is not firing.but updating a same record trigger is working fine.the trigger logic is the discount is exceeded morethan fixed discount the checkbox 'Granted discount KO' is checked.i have tried tigger.isinsert and trigger.isnew context variables but same result.can you anybody help me.
Trigger:
trigger Rfleet_validDiscountpercent on Opportunity_car_set__c (before insert,before update) {
    decimal d;
    set<id>oppids=new set<id>();
    string conca;
    list<Opportunity> lsupdateopp=new list<Opportunity>();

        for(Opportunity_car_set__c oppcarset:[select Model__r.Name, Discount__c,Opportunity__r.Total_Fleet_Size__c,Model__c,Rfleet_Country_Code__c,CreatedDate from Opportunity_car_set__c  where id in:trigger.new]){
            integer dt=oppcarset.CreatedDate.Year();
            conca = oppcarset.Model__r.Name +oppcarset.Rfleet_Country_Code__c + string.valueof(oppcarset.Opportunity__r.Total_Fleet_Size__c)+string.valueof(dt);
            system.debug('conca----->'+conca);
       
        }
     
     list<Discount_Grid__c> listDisGrid=[select Rfleet_Status__c,Rfleet_Discountconca__c,Rfleet_Discount__c from Discount_Grid__c  where Rfleet_Status__c = 'Enabled' and Rfleet_Discountconca__c=:conca order by CreatedDate desc limit 1];
     system.debug('>>>>>>>>>>>>>>:'+listDisGrid);
       for(Opportunity_car_set__c opp:trigger.new){
            for(Discount_Grid__c disGrid:listDisGrid){
               if(opp.Discount__c >disGrid.Rfleet_Discount__c){
                    opp.Rfleet_Granted_discount_KO__c = true;
                    oppids.add(opp.Opportunity__c);
                }else{
                      oppids.add(opp.Opportunity__c);
                }
            } 
      }
    
     list<Opportunity>opplistupdate=[select Rfleet_Granted_discount_KO__c from Opportunity where id=:oppids];
       system.debug('>>>>>>>>>>>>>>:'+opplistupdate); 
       
        
         for(Opportunity_car_set__c oppCar:trigger.new){
         
             for(Opportunity updateopp:opplistupdate){
                    if(oppCar.Rfleet_Granted_discount_KO__c==true && opplistupdate.size()>0){
                         updateopp.Rfleet_Granted_discount_KO__c =true;
                         lsupdateopp.add(updateopp);
                     }else if(oppCar.Rfleet_Granted_discount_KO__c==false && opplistupdate.size()>0){
                         updateopp.Rfleet_Granted_discount_KO__c =false;
                         lsupdateopp.add(updateopp);
                     }
             }
             update lsupdateopp;
        }
        
        
}
John Aaron NelsonJohn Aaron Nelson

It appears that in your "before insert" trigger, you're referencing id's in "trigger.new", which would not be exist if they have not been inserted.  This logic would work fine when updating the record as they would have an Id.  

You also have DML inside two for loops.  You should bulkify your trigger so as to not execute DML inside of a loop.

Checkout this unit on Trailhead. https://developer.salesforce.com/trailhead/apex_triggers/apex_triggers_bulk

To fix your code, I would recommend changing your trigger to after insert and after update and bulkifying your update DML

kiran k 12kiran k 12
i have changed according your suggestions but i got error like:
User-added image
sandeep sankhlasandeep sankhla
Hi Kiran,

There are too many mistakes in your trigger, but if you can share the requirnmnets then may be I can help you to write a good trigger to meet those.

Share what exactly you need.

Thanks!