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
AnicheAniche 

Can we have Triggers on fields rather than objects.

Hi,
 
I need to find out if the owner of account is changed during the batch processing.
Not sure if i can put a trigger on the field itself.
 
If i cannot the other option is to compare old and new values but i dont have a clue on how to do that.
Can somebody help ?
 
Thanks
TehNrdTehNrd
You can check to see if a field has changed......

Code:
for(Opportunity opp : Trigger.new){
     //Create an old and new map so that we can compare values
     Opportunity oldOpp = Trigger.oldMap.get(opp.ID); 
     Opportunity newOpp = Trigger.newMap.get(opp.ID);
     
     //Retrieved the old and new Owner
     ID oldOwner = oldOpp.OwnerID;
     ID newOwner = newOpp.OwnerID;
          
     //If the owner has changed, add the Opp ID to a Set for querying or do what ever logic you want
     if(oldOwner != newOwner){
      oppIDs.add(opp.id); 
     }
}

 

AnicheAniche
Thanks,... I some how manage to get the code using trigger.new and trigger.old.. with the solution that you gave me just wondering.. when does one use Map when trigger.new.. Are there any guidelines as such ?
 
Thanks for you reply..
TehNrdTehNrd
I honestly can't quite remember where I read about using newMap and oldMap but I  know they should be used when you want to compare changes to records.

Also this note taken from the documentation:

newMap:
A map of IDs to the new versions of the sObject records.
Note that this map is only available in before update, after insert, and after update
triggers.

oldMap:
A map of IDs to the old versions of the sObject records.
Note that this map is only available in update and delete triggers.
AnicheAniche
Thanks for your reply.. will try to find out some how..
sumpritsumprit
Hi there,

I tried something, gives no errors however the logic somehow does not work. What am I missing, please advice?

=================================================================================

trigger Update_OwnerNames on Account (after update) {
   
    // MAP allows us to keep track of the accounts which have changed owner names.
   
    Map<ID,Account> AccountwithChangedOwners = new Map<ID, Account> ();
   
    for (Integer i=0; i<Trigger.new.size(); i++)
    {
        if( ( Trigger.old[i].Owner != Trigger.new[i].Owner)) // That means if the Owner name has been changed
        {
            AccountwithChangedOwners.put(Trigger.old[i].id, Trigger.new[i]);
        }
       
    }
   
    List<Oppurtunity> UpdateOppurOwnername = new List<Oppurtunity>();
   
    for (Oppurtunity opp: [SELECT ID, ACCOUNTID,Owner.Name FROM ACCOUNT WHERE ACCOUNTID IN :AccountwithChangedOwners.keySet()])
    {
        Account parentAccount = AccountwithChangedOwners.get(opp.ID);
       
        opp.Owner = parentAccount.Owner;  // This states that Oppurtunity Owner names should match the Account Owner name.
       
        updatedOppurtunities.add(opp);
   
    }
   
        update updatedOppurtunites;

}
       
   
   
   
   
   
   
   
   
   

 


Message Edited by sumprit on 01-31-2008 04:59 PM