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

Can we have Triggers on fields rather than objects.

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 ?
You can check to see if a field has changed......

for(Opportunity opp :{
     //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){


Thanks,... I some how manage to get the code using and trigger.old.. with the solution that you gave me just wondering.. when does one use Map when Are there any guidelines as such ?
Thanks for you reply..
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:

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

A map of IDs to the old versions of the sObject records.
Note that this map is only available in update and delete triggers.
Thanks for your reply.. will try to find out some how..
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<; i++)
        if( ( Trigger.old[i].Owner !=[i].Owner)) // That means if the Owner name has been changed
    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.
        update updatedOppurtunites;



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