• Corey Ritter
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Here is the code and a link to the page for my org.  https://bodydesignos.my.salesforce.com/01q50000000Gue5


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
trigger EventTrigger on Event (after insert, after update, after delete, before delete) 

    if((trigger.isUpdate||trigger.isInsert)&&trigger.isAfter) EventActions.SetAccountUsedSessions(trigger.new); 
    if((trigger.isDelete)&&trigger.isAfter) EventActions.SetAccountUsedSessions(trigger.old);
    
    Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name='Corporate' LIMIT 1]; 
    if(UserInfo.getProfileId()!=sysAdminProfile.Id)
    {
        if(trigger.isBefore && trigger.isDelete)
        {
            for(Event event : trigger.old)
            { 
                Datetime endDate = event.EndDateTime;
                if(event.RecurrenceEndDateOnly!=null) endDate = event.RecurrenceEndDateOnly;
                if(endDate != null && endDate.addHours(-5) < system.today().toStartOfWeek())
                {
                    event.addError('Events completed before this week cannot be deleted or edited.');
                }
            }
        }
        if(trigger.isAfter && trigger.isUpdate)
        {
            for(Event event : trigger.new)
            {
                Datetime endDate = trigger.oldMap.get(event.Id).EndDateTime;
                if(event.RecurrenceEndDateOnly!=null) endDate = event.RecurrenceEndDateOnly;
                if(endDate!=null && endDate.addHours(-5) < system.today().toStartOfWeek())
                {
                    event.addError('Events completed before this week cannot be deleted or edited.');
                }
            }
        }
    }
}
The code has stopped totallingall the used sessions (events lableled PT Session) in the field "Used Sessions" on account.



Please let me know if you can figure out why. It's working for some accounts but not all. I also need the "system admin" profile to be added on this line"

#6     Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name='Corporate' LIMIT 1]; 


Thanks for your help!
 
How do I allow all users to use this apex? It's currently restricted to my community users.

public without sharing class EventActions 
{
  public static void SetAccountUsedSessions(List<Event> events)
  {
    Set<Id> accountIds = new Set<Id>();
    for(Event event : events)
    {
      if(event.AccountId!=null && !accountIds.contains(event.AccountId)) accountIds.add(event.AccountId);
    }
    if(accountIds!=null&&accountIds.size()>0)
    {
      Map<Id, Account> accountsToUpdate = new Map<Id, Account>();
      AggregateResult[] countResults = [SELECT AccountId, count(Id)cnt FROM Event WHERE AccountId IN :accountIds  AND IsRecurrence=false AND Subject LIKE 'PT%' AND ActivityDate <= TODAY AND EVENT_STATUS1__C <> 'Cancelled WITH 24hr Notice' GROUP BY AccountId];
      if(countResults!=null&&countResults.size()>0)
      {
        for (AggregateResult ar : countResults)  {
          String accountId = String.ValueOf(ar.get('AccountId'));
          String usedSessions = String.ValueOf(ar.get('cnt'));
          if(accountId!=null&&accountId!=''&&usedSessions!=null&&usedSessions!='')
          {
            Account account = new Account(Id=accountId);
            account.Used_Sessions__c = integer.valueOf(usedSessions);
            accountsToUpdate.put(account.Id, account);
          }
        }    
      }
      for(Id accountId : accountIds)
      {
        if(!accountsToUpdate.containsKey(accountId))
        {
          Account account = new Account(Id=accountId);
          account.Used_Sessions__c = 0;
          accountsToUpdate.put(accountId, account);
        }
      }
      if(accountsToUpdate!=null&&accountsToUpdate.values().size()>0) update accountsToUpdate.values();
    }
  }
  
  public static testMethod void Test()
  {
    List<Event> events = [SELECT Id, WhatId, AccountId FROM Event WHERE AccountId<>null 
      AND IsRecurrence=true AND Subject LIKE 'PT%' LIMIT 10];
    Event events2 = [SELECT Id, WhatId, AccountId FROM Event WHERE AccountId<>null 
      AND IsRecurrence=false AND EndDateTime > :system.today().toStartOfWeek() LIMIT 1];
    
    Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name='Corporate' LIMIT 1];
    
    User u = [select Id from User where IsActive = true and UserType = 'Standard' 
      and ProfileId != :sysAdminProfile.Id limit 1];
    System.runAs(u) {
    
      SetAccountUsedSessions(events); 
      update events2;
      delete events2;
    }
    
  }
}
How do I allow all users to use this apex? It's currently restricted to my community users.

public without sharing class EventActions 
{
  public static void SetAccountUsedSessions(List<Event> events)
  {
    Set<Id> accountIds = new Set<Id>();
    for(Event event : events)
    {
      if(event.AccountId!=null && !accountIds.contains(event.AccountId)) accountIds.add(event.AccountId);
    }
    if(accountIds!=null&&accountIds.size()>0)
    {
      Map<Id, Account> accountsToUpdate = new Map<Id, Account>();
      AggregateResult[] countResults = [SELECT AccountId, count(Id)cnt FROM Event WHERE AccountId IN :accountIds  AND IsRecurrence=false AND Subject LIKE 'PT%' AND ActivityDate <= TODAY AND EVENT_STATUS1__C <> 'Cancelled WITH 24hr Notice' GROUP BY AccountId];
      if(countResults!=null&&countResults.size()>0)
      {
        for (AggregateResult ar : countResults)  {
          String accountId = String.ValueOf(ar.get('AccountId'));
          String usedSessions = String.ValueOf(ar.get('cnt'));
          if(accountId!=null&&accountId!=''&&usedSessions!=null&&usedSessions!='')
          {
            Account account = new Account(Id=accountId);
            account.Used_Sessions__c = integer.valueOf(usedSessions);
            accountsToUpdate.put(account.Id, account);
          }
        }    
      }
      for(Id accountId : accountIds)
      {
        if(!accountsToUpdate.containsKey(accountId))
        {
          Account account = new Account(Id=accountId);
          account.Used_Sessions__c = 0;
          accountsToUpdate.put(accountId, account);
        }
      }
      if(accountsToUpdate!=null&&accountsToUpdate.values().size()>0) update accountsToUpdate.values();
    }
  }
  
  public static testMethod void Test()
  {
    List<Event> events = [SELECT Id, WhatId, AccountId FROM Event WHERE AccountId<>null 
      AND IsRecurrence=true AND Subject LIKE 'PT%' LIMIT 10];
    Event events2 = [SELECT Id, WhatId, AccountId FROM Event WHERE AccountId<>null 
      AND IsRecurrence=false AND EndDateTime > :system.today().toStartOfWeek() LIMIT 1];
    
    Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name='Corporate' LIMIT 1];
    
    User u = [select Id from User where IsActive = true and UserType = 'Standard' 
      and ProfileId != :sysAdminProfile.Id limit 1];
    System.runAs(u) {
    
      SetAccountUsedSessions(events); 
      update events2;
      delete events2;
    }
    
  }
}
I would like to make sure all phone numbers are updated to conform to this format:

(xxx) xxx-xxxx

I tried to write a workflow rule to address this as follows:

NOT (
   REGEX( HomePhone , "([0-9]{3})[0-9]{3}-[0-9]{3}")
)

However, I get the following syntax error: "Error: Function REGEX may not be used in this type of formula"
Is there a way to use REGEX here?  Is there a better way to go about this?

I