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
Pranav ChitransPranav Chitrans 

trigger isUpdate

I am having scenario some thing like this.. I wanrt to fire trigger on insert if salary less than 1000 and in update salary must be greater than 1300...BUT..BUT BUT.... main thing... during update i dont want to fire the update trigger record when the salary amount does not chnages.. it only get fired when  during update the salary amount is being changed..During update if i changes or updates any other field, except salary the before update trigger should not fire ... I done some something like this.. before insert is correct but i am having problem in before update... that when i changes the salary then only it shoul fire.. if old value != new value then fire the trigger
trigger salaryCheck on Teacher__c (before Insert, before Update) 
{
 for(Teacher__c tch : trigger.New)
 {
  if(trigger.isInsert && tch.Salary__c < 1000)
  {
   tch.addError('Must be greater than 1000');
  }
 }
 
 List<Teacher__c> oldlstTch = new List<Teacher__c>(trigger.old);
 for(Teacher__c newLstTch : trigger.new)
 {
  if(trigger.isUpdate &&  newLstTch.Salary__c != oldlstTch.Salary__c  )
  {
    if(newLstTch.Salary__c == null ||newLstTch.Salary__c < 1300)
      {
       newLstTch.Salary__c.addError('can not be null and greater than 1300');
      }
  }
 }
}
Error : Compile Error: Initial term of field expression must be a concrete SObject: List<Teacher__c> at line 14 column 50
 
Best Answer chosen by Pranav Chitrans
Abhishek BansalAbhishek Bansal
Hi Pranav,

Please try with the below code :
trigger salaryCheck on Teacher__c (before Insert, before Update) 
{
 for(Teacher__c tch : trigger.New)
 {
  if(trigger.isInsert && tch.Salary__c < 1000)
  {
   tch.addError('Must be greater than 1000');
  }
 }
 
 List<Teacher__c> oldlstTch = new List<Teacher__c>(trigger.old);
 for(Teacher__c newLstTch : trigger.new)
 {
  if(trigger.isUpdate &&  newLstTch.Salary__c != trigger.oldMap.get(newLstTch.id).Salary__c  )
  {
    if(newLstTch.Salary__c == null ||newLstTch.Salary__c < 1300)
      {
       newLstTch.Salary__c.addError('can not be null and greater than 1300');
      }
  }
 }
}

Let me know if you have any issue.

Thanks,
Abhishek

All Answers

SupriyaHSupriyaH
1. You do not necessarily have to create oldlstTch, infact Trigger.old has everything that you need.
2. At 14, again, you have to iterate through individual Sobject records to be able to compare the values of individual fields. But you are using, List.FieldValue and thus the error. 
3. The dot notation that you use to access elements off of list is List[index].field__c. 
4. It is recommended to write the logic in an Apex class and call that class in your trigger. 
5. Now try something like this
for(Teacher__c tNew:triggerNew){
      // Get new and old Teacher Instances
      Teacher__c newT = newMap.get(tNew.ID);
      Teacher__c oldT = oldMap.get(tNew.ID);
       if(newT.Salary__c>1300 
         && oldT.Salary__c!=newT.Salary__c  
         && newT.Salary__c !=NULL){
                // whaterver you want to update goes here
         }
   }

 
Abhishek BansalAbhishek Bansal
Hi Pranav,

Please try with the below code :
trigger salaryCheck on Teacher__c (before Insert, before Update) 
{
 for(Teacher__c tch : trigger.New)
 {
  if(trigger.isInsert && tch.Salary__c < 1000)
  {
   tch.addError('Must be greater than 1000');
  }
 }
 
 List<Teacher__c> oldlstTch = new List<Teacher__c>(trigger.old);
 for(Teacher__c newLstTch : trigger.new)
 {
  if(trigger.isUpdate &&  newLstTch.Salary__c != trigger.oldMap.get(newLstTch.id).Salary__c  )
  {
    if(newLstTch.Salary__c == null ||newLstTch.Salary__c < 1300)
      {
       newLstTch.Salary__c.addError('can not be null and greater than 1300');
      }
  }
 }
}

Let me know if you have any issue.

Thanks,
Abhishek
This was selected as the best answer