• Stephanie Zimmerman 7
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 4
    Replies
Hello all, 

Hoping this will be a quick and easy question. I currently have a batch class to send emails on Event. The batch class is referenced by a schedulable class; the schedulable class runs daily. For the portion of the code that queries the Event records, I am looking to make a small modification to exclude Events where Type = Rescheduled or Cancelled. 

Previously, this was the code and emails were sending:
for(Event evnt : eventLst){
      
      if(evnt.WhatId != null && String.valueOf(evnt.WhatId).startsWith(PREFIX_OPPORTUNITY) && evnt.WhoId != null && String.valueOf(evnt.WhoId).startsWith(PREFIX_CONTACT)){
        //Opportunity Id set
        oppIdSet.add(evnt.WhatId);
        //Contact Id set
        contIdSet.add(evnt.WhoId);
      }
    }


Now this is the updated code attempting to exclude Events where Type = Rescheduled or Cancelled

for(Event evnt : eventLst){
      
      if(evnt.Type != 'Rescheduled' && evnt.Type != 'Cancelled' && evnt.WhatId != null && String.valueOf(evnt.WhatId).startsWith(PREFIX_OPPORTUNITY) && evnt.WhoId != null && String.valueOf(evnt.WhoId).startsWith(PREFIX_CONTACT)){
        //Opportunity Id set
        oppIdSet.add(evnt.WhatId);
        //Contact Id set
        contIdSet.add(evnt.WhoId);
      }
    }


With this change, now no emails are sending, even when Type is null. I assume I'm referencing picklist values incorrectly but am not sure how to correct. Any ideas for the best way to write this code to exclude Events where Type = Rescheduled or Cancelled? 

Thanks!
Stephanie
Hi, I need what I thought would be a quick change to the below code. 
email.setReplyTo('marc@xyz.com');

Instead of setting the Reply To to one email address, I need to set to three email addresses, such as below:
email.setReplyTo('marc@xyz, stacy@xyz.com, jill@xyz.com');

I tried this but the code is now not working so I'm sure I am wrong with the syntax in some way. Could anyone please tell me how to correct the above line to include three email addresses? Thank you!
Hello, please see Event trigger below. It prevents the delete of an Event record if running user is not equal to created by user. I would like to update to add another condition to the IF statement ...and if Opportunity.Stage != 'Appt Canceled'. Perhaps something along the lines of if ((UserInfo.getUserId() != a.CreatedById)) && a.what.stage != 'Appt Cancelled')

However, I know that a.what.stage = 'Appt Cancelled' is not the right way to do this due to the polymorphic nature of the What relationship field. How can I write this in so that Salesforce knows I want to look only at Opportunity.Stage? 

Below is the full trigger. Thank you to anyone who has a few minutes to help. 

trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           
          system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
          if (UserInfo.getUserId() != a.CreatedById)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}
Hi, I have what I think should be a very simple trigger that is intended to prevent users from deleting Events unless they are the user that created it. 

This is the code:
trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           

          if (UserInfo.getUserId() != a.CreatedBy.Id)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}

The problem is that the code prevents all users from deleting Events, even if they are the CreatedBy user. So I assume the problem is with this line ( if (UserInfo.getUserId() != a.CreatedBy.Id) ), though I'm not sure what the problem with it is. 

Any advice is much appreciated. Thank you!
I'm starting to learn Apex and working on basic triggers so apologies in advance if the code is bad.

Architecture: Parent: Opportunity; relevant fields = Draw_4__c (checkbox), Project_Stage__c (text) Child: Referral__c; relevant fields = Send_Email__c (checkbox), Status__c (text)

Referral__c is a junction object between Opportunity records. The purpose is to link one opportunity to another and say that this opportunity referred that opportunity.

On update of Opportunity, the code needs to always set Status__c = Project_Stage__c and only set Send_Email__c = TRUE if Draw_4__c = TRUE.

Right now, Send_Email__c is getting appropriately set but Status__c is not. I can tell that the problem is how I am trying to refer to the parent field (opportunity.project_stage__c). If I set Status__c = some text, such as 'Active', the trigger works as needed but if I set status__c = opportunity.project_stage__c, the field remains blank. I'm wondering if this has something to do with the fact that Referral is junction between two opportunity records.

Trigger:

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
35
36
37
38
39
trigger UpdateReferral on Opportunity(after update) {

If(Trigger.isUpdate){

     Set<ID> ids = Trigger.newMap.keySet();
     list<Opportunity> updatedParents = [SELECT Id, 
                 (SELECT Id, Referred_Opportunity__c, Send_Email__c, Status__c 
                  from Referred_By__r ) FROM Opportunity
                 WHERE Id in :ids];
         List<Referral__c> childrenToUpdate = new List<Referral__c>();

         //Then loop through each parent object in updated parent
         for (Opportunity p : updatedParents) 
         { 
        if(p.Draw_4__c = TRUE){

                //and loop thru each kid in the child set}
               for(Referral__c kid : p.Referred_By__r) 
               { 
                         ///update logic from above
                             kid.Send_Email__c = True;
                             kid.Status__c = 'Active';
               childrenToUpdate.add(kid);
                }
                }
                else  {
           for(Referral__c kid2 : p.Referred_By__r) 
           {
               kid2.Status__c = 'Active';
               childrenToUpdate.add(kid2);
           }
{
        }}
       if( !childrenToUpdate.isEmpty())
       {
            update childrenToUpdate;
       }
}}
}

Thanks in advance for your help!
Hello, please see Event trigger below. It prevents the delete of an Event record if running user is not equal to created by user. I would like to update to add another condition to the IF statement ...and if Opportunity.Stage != 'Appt Canceled'. Perhaps something along the lines of if ((UserInfo.getUserId() != a.CreatedById)) && a.what.stage != 'Appt Cancelled')

However, I know that a.what.stage = 'Appt Cancelled' is not the right way to do this due to the polymorphic nature of the What relationship field. How can I write this in so that Salesforce knows I want to look only at Opportunity.Stage? 

Below is the full trigger. Thank you to anyone who has a few minutes to help. 

trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           
          system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
          if (UserInfo.getUserId() != a.CreatedById)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}
Hi, I have what I think should be a very simple trigger that is intended to prevent users from deleting Events unless they are the user that created it. 

This is the code:
trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           

          if (UserInfo.getUserId() != a.CreatedBy.Id)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}

The problem is that the code prevents all users from deleting Events, even if they are the CreatedBy user. So I assume the problem is with this line ( if (UserInfo.getUserId() != a.CreatedBy.Id) ), though I'm not sure what the problem with it is. 

Any advice is much appreciated. Thank you!
I'm starting to learn Apex and working on basic triggers so apologies in advance if the code is bad.

Architecture: Parent: Opportunity; relevant fields = Draw_4__c (checkbox), Project_Stage__c (text) Child: Referral__c; relevant fields = Send_Email__c (checkbox), Status__c (text)

Referral__c is a junction object between Opportunity records. The purpose is to link one opportunity to another and say that this opportunity referred that opportunity.

On update of Opportunity, the code needs to always set Status__c = Project_Stage__c and only set Send_Email__c = TRUE if Draw_4__c = TRUE.

Right now, Send_Email__c is getting appropriately set but Status__c is not. I can tell that the problem is how I am trying to refer to the parent field (opportunity.project_stage__c). If I set Status__c = some text, such as 'Active', the trigger works as needed but if I set status__c = opportunity.project_stage__c, the field remains blank. I'm wondering if this has something to do with the fact that Referral is junction between two opportunity records.

Trigger:

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
35
36
37
38
39
trigger UpdateReferral on Opportunity(after update) {

If(Trigger.isUpdate){

     Set<ID> ids = Trigger.newMap.keySet();
     list<Opportunity> updatedParents = [SELECT Id, 
                 (SELECT Id, Referred_Opportunity__c, Send_Email__c, Status__c 
                  from Referred_By__r ) FROM Opportunity
                 WHERE Id in :ids];
         List<Referral__c> childrenToUpdate = new List<Referral__c>();

         //Then loop through each parent object in updated parent
         for (Opportunity p : updatedParents) 
         { 
        if(p.Draw_4__c = TRUE){

                //and loop thru each kid in the child set}
               for(Referral__c kid : p.Referred_By__r) 
               { 
                         ///update logic from above
                             kid.Send_Email__c = True;
                             kid.Status__c = 'Active';
               childrenToUpdate.add(kid);
                }
                }
                else  {
           for(Referral__c kid2 : p.Referred_By__r) 
           {
               kid2.Status__c = 'Active';
               childrenToUpdate.add(kid2);
           }
{
        }}
       if( !childrenToUpdate.isEmpty())
       {
            update childrenToUpdate;
       }
}}
}

Thanks in advance for your help!