0

I am trying to update a date field on the Account object when two roll up summaries (Total_Opportunities_Won__c & Active_Opportunities__c) enter 0. Currently our process is that we have two roll up summaries that feed from the Opportunity object and then we have 3 scheduled apex classes to update the status. What I am needing to accomplish is a way to update a date field on the Account object when both the roll up summaries equal 0. Then if the roll up summary for Active_Opportunities__c increases to 1 or greater to put todays date. Was wondering if someone might be able to help the newbie with this?

Prospect

global class DailyAccountProcessorProspect implements Schedulable {

global void execute(SchedulableContext ctx) {
    List<Account> myList = [SELECT Active_Opportunities__c,Id,Total_Opportunities_Won__c FROM Account WHERE Total_Opportunities_Won__c = 0 AND Active_Opportunities__c = 0];

    if(!myList.isEmpty()) {
        for(Account l: myList) {
            l.Customer_Type__c = 'Prospect';
        }
        update myList;
    }
}
}

Former:

global class DailyAccountProcessorFormer implements Schedulable {

global void execute(SchedulableContext ctx) {
    List<Account> myList = [SELECT Active_Opportunities__c,Id,Total_Opportunities_Won__c FROM Account WHERE Total_Opportunities_Won__c >= 1 AND Active_Opportunities__c = 0];

    if(!myList.isEmpty()) {
        for(Account l: myList) {
            l.Customer_Type__c = 'Former Customer';
        }
        update myList;
    }
}
}

Current: global class DailyAccountProcessorCurrent implements Schedulable {

global void execute(SchedulableContext ctx) {
    List<Account> myList = [SELECT Active_Opportunities__c,Id,Total_Opportunities_Won__c FROM Account WHERE Total_Opportunities_Won__c >= 1 AND Active_Opportunities__c = 1];

    if(!myList.isEmpty()) {
        for(Account l: myList) {
            l.Customer_Type__c = 'Current Client';
        }
        update myList;
    }
}
}
0

You can use the process builder to evaluate the fields each time the record is updated or changed, reference the object type enter image description here

and then you can define the conditions for the fields that are modified: enter image description here

and finally update the record using a formula field that evaluates to Today() enter image description here

this is easily maintainable, here you can find salesforce's documentation regarding the process builder

| improve this answer | |
  • With everything I read I thought you couldn't use process builder for a rollup summary change? – SoleHack Apr 13 '17 at 19:10
0

I was able to get it working this way.

trigger AccountTrigger on Account (before update)
 {

  if(checkRecursive.runOnce())
    {
list<Account> account_Updatelist= new list<Account>();
for(Account acc : Trigger.new)
  {
     if(acc.Total_Opportunities_Won__c>=1 && acc.Active_Opportunities__c == 0 && Trigger.oldMap.get(acc.id).Account_Clearing_Date__c!=null && acc.Account_Clearing_Date__c == Trigger.oldMap.get(acc.id).Account_Clearing_Date__c + 1)
        acc.Customer_Type__c = 'Former Client';


      else if(acc.Total_Opportunities_Won__c>=1 && acc.Active_Opportunities__c >= 1)
         acc.Customer_Type__c = 'Current Client';

      else if(acc.Total_Opportunities_Won__c==0 && acc.Active_Opportunities__c==0)
         acc.Customer_Type__c = 'Prospect';

      if(acc.Active_Opportunities__c==0)
         acc.Account_Clearing_Date__c = system.today();

      else if(acc.Active_Opportunities__c>=1)
        acc.Account_Clearing_Date__c = null;

      //  account_Updatelist.add(acc);

  }

//  if(account_Updatelist!=null && account_Updatelist.size()>0)
    //  update account_Updatelist;

  }

  }

With this class as well

public Class checkRecursive{
private static boolean run = true;
public static boolean runOnce(){
if(run){
 run=false;
 return true;
}else{
    return run;
}
}
}
| improve this answer | |
  • A workflow rule ought to handle this easily. You can include logic in a workflow field update that can evaluate the rollups and set the date appropriately. – gorav Apr 19 '17 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.