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
KRISH9KRISH9 

Getting Error while inserting record but i want to achieve the insert and update both

trigger TriggerOnEstimate on Estimate__c (before insert,before update) {
 if(Trigger.isInsert)
 {
  Date d = Date.today();  
  for ( Estimate__c e : Trigger.new)
  {
   List<Estimate__c> est = [select Most_Recent_Estimate__c,Estimate_Name__c from Estimate__c ];
   System.debug('******All Estimates****'+est+'***Size***'+est.size());
   
   
   for (Integer i =0 ;i<est.size() ; i++)
   {
    est[i].Most_Recent_Estimate__c  = 'FALSE';
    update est[i];
   }
  // update est;
   
   e.Most_Recent_Estimate__c = 'TRUE';   
   
  }  
 } 
 for(Estimate__c e: Trigger.new)
 
   {
    if(Trigger.isUpdate && e.CreatedDate == Date.today())
    {
    
    }
    if(Trigger.isUpdate && e.CreatedDate < Date.today() )
    {
     e.addError('***********Donot edit the estimate***********');
    }
   }
}

Coco_SdyneyCoco_Sdyney

1st of all, never query inside a for loop.

vishal@forcevishal@force

Yes, you shouldn't be querying inside a for loop.

 

Also, what I see is you want to set the Latest Inserted Estimate as Most_Recent_Estimate. So at a time, only one Estimate will be Most_Recent_Estimate ideally.

 

So for update, what business goal do you have? On every modification, that record should become the Most_Recent_Estimate?