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
Tanmay SahaiTanmay Sahai 

Help with Trigger - Change account ownership on the basis of Last Activity Date and Last Ownership change Date

Hi Developers,

I am looking to display the Last Modified Date when the Account Ownership changed (as displayed under Account History) on a field to use it in my trigger to calculate the difference between LastActivityDate and LastOwnershipChangedDate.

Here is my requirement:
I have a custom formula field: User Last Activity which calculates the number of days since last activity on the Account (this is calculated using the System field :LastActivityDate). Similarly, I want to calculate the number of days since last time the Account ownership changed. Below screenshot shows the Date that I want to capture and display on a field and then use this date to calculate the days on another formula field:

Scree shot showing the Date of Owner Ship change

Lets take User Last Activity as u and OwnershipLastChanged as v. Now, I want to change the ownership when:

If u>=v, then u else if u<v, then v. So, basically the account ownership should change only when either the Last User Activity >30 or Last Ownership Change <30 and if u>v then change on the basis of u else on the basis iof v.

Here is my trigger:

trigger Account_After_Update on Account (after update) {

map<id,id> accountOldOwnerid = new map<id,id>();

   list<account> updateaccount = new list<account>();

   

   if(trigger.isAfter && trigger.isUpdate){

for(account a :trigger.new){

    string oldownerid  = trigger.oldmap.get(a.id).OwnerId;

    string newownerid  = a.ownerid;

    if(oldownerid != newownerid){

accountOldOwnerid.put(a.id,oldownerid );

    }

}

map<id,User> oldowners = new map<id,User> ( [Select u.id ,u.IsActive From User u where u.id in : accountOldOwnerid.values()
                                             and u.isActive = true and u.id!= '00536000001hfleAAA']);

list<Account> accs = [ select OwnerId, ( select Id, CreatedDate, Field, CreatedBy.Name, OldValue, NewValue from AccountHistory where AccountId = '001W000000ZVss1IAD'  ) from account where id=:accountOldOwnerid.keyset()];

  for( Account acc : accs)
  {
     
  Id oldOwnerId =   accountOldOwnerid.get(acc.id);

  //if(oldowners.containsKey(oldOwnerId))  
  //{

    //for( Account ac :acc.tasks){

            //accs.OwnerId = oldOwnerId;
            //tasks.Id = oldOwnerId}

  }

    if(updateaccount.size()>0)
    {
     updateaccount.add(accs);
    }

     update updateaccount();

   }
}

Below is my class:
global class DailyAccountProcessor implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
            List<Account>listofAccount =
            [SELECT Id from Account where OwnerId != '00536000001hfleAAA' AND User_Last_Activity__c >30 AND of_Days_since_Last_Modification__c >30 LIMIT 200];
        
        List<Account>listofUpdatedAccount = new List<Account>();
        if(!listofAccount.isEmpty())
        {
            for(Account acc : listofAccount)
            {
                acc.OwnerId = '00536000001hfleAAA';
                listofUpdatedAccount.add(acc);
            }
                
                UPDATE listofUpdatedAccount;
        }
    }
}

Note: of_Days_since_Last_Modification__c calculates the number of days since last modified date (TODAY() - LastModifiedDate)

Any quick help/suggestions will be appreciated. Any other workarounds are also welcome.

Await your response guys.

Thanks!