You need to sign in to do that
Don't have an account?
Tanmay 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:
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!
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:
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!