• Cindy GUILLET
  • NEWBIE
  • 25 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Hi,
I have this trigger which create an event in the approver calendar when a leave request is validated.
I need to delete this event when the leave request is cancelled.
Can you please help ? I am really new with apex.
Thank you for your help !


trigger ApexTrigger_LeaveRequest on LeaveRequest__c (after update) {


    If(AvoidRecursion.isFirstRun()){
        If(!AdminSettings__c.getInstance().DisableTg__c){ 
        
        For (LeaveRequest__c leaveRequest : Trigger.new) {
        
        // Access the "old" record by its Id in Trigger.oldMap
        LeaveRequest__c oldLeaveRequest = Trigger.oldMap.get(leaveRequest.Id);
        
        // Trigger.new record is conveniently the event creation
        Boolean oldLeaveRequestAccepted = oldLeaveRequest.Status__c.equals('30 Accepted');
        Boolean leaveRequestAccepted = leaveRequest.Status__c.equals('30 Accepted');
        
        // Check that the field was changed to the correct value
        If (leaveRequest.Approver__c == '00524000000z6txAAA' && !oldLeaveRequestAccepted) {
        Event ev = new Event 
        (
            OwnerId = leaveRequest.Approver__c,
            StartDateTime = leaveRequest.StartDate__c,
            EndDateTime = leaveRequest.EndDate__c,
            Subject = (leaveRequest.ParameterLabel__c + ' - ' + leaveRequest.EmployeeName__c),
            Activite__c = 'Other',
            IsAllDayEvent = false,
            IsPrivate = true,
            recordTypeId = '0121o00000113r4AAA',
            ShowAs = 'Free',
            Type = 'Leave'
        );
        Insert ev ; 
        }else {
            Event ev = new Event();        }
       }
      }
     }
    }
Hello,

I have a flow which add informations from children records of cases.
In the text field I obtain : 1.5.663,1.5.458,1.5.663,1.4.985
I need to remove duplicates in order to display : 1.5.663,1.5.458,1.4.985

I wrote a trigger based on what I found on many forums but I'm new on Apex and I can't figure it out.

trigger Trg_Case on Case (after update) {
    For(Case myCase: Trigger.new){
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            string reports = myCase.ChildrenVersions__c;
            List<String> l = new List<String>(new Set<String>(reports.split(',')));
            String uniqueReports = String.join(l, ',');
        }
    }
}

Thanks for your answers
Hello,
I'm a beginner with triggers and test classes.
I can't manage to cover the trigger below.

Thanks for your help.

Trigger EventBeforeUpdate on Event (before insert, before update){
   
    If(AvoidRecursion.isFirstRun()){
        If(!AdminSettings__c.getInstance().DisableTg__c){
            For (Event myEvent: trigger.new){
                If(Trigger.isInsert) {
                }
       
                If(Trigger.isUpdate){
                    If(UserInfo.getUserId()!= myEvent.OwnerId && UserInfo.getUserId()!= '00524000001WRNAAA4'&& myEvent.RecordTypeId == '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
                    {
                    myEvent.adderror('ERROR1');
                    }
                    If(UserInfo.getUserId()!= myEvent.OwnerId && myEvent.RecordTypeId != '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
                    {
                    myEvent.adderror('ERROR2.');
                    }
                }
            }
        }
    }
}


My Test :

@isTest
public class EventBeforeUpdateTest{
   
    private static User makeStandardUser(String p_firstName, String p_lastName) {
       
        User standardUser = new User();
       
        standardUser.LastName = p_lastName;
        standardUser.FirstName = p_firstName;
        standardUser.Alias = 'JPJ';
        standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
        standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
        standardUser.Email = 'dev@dev.com';
        standardUser.TimeZoneSidKey = 'America/Los_Angeles';
        standardUser.LocaleSidKey = 'en_US';
        standardUser.EmailEncodingKey = 'UTF-8';
        standardUser.LanguageLocaleKey = 'en_US';
        standardUser.profileId = '00e24000000tqX0';
       
        return standardUser;
    }
   
    static testMethod void createAndUpdateEventTest() {
       
        User User1 = makeStandardUser('JUL', 'JAV');
        User User2 = makeStandardUser('CGL', 'GUI');
       
        Insert User1;
        Insert User2;
       
        AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
        setting.DisablePb__c = True;
       
        Insert setting;
       
        Event myEvent1 = new Event();
        myEvent1.RecordTypeId = '0121o00000113r4';
        myEvent1.Subject = 'New Event Standard';
        myEvent1.StartDateTime = system.Now();
        myEvent1.EndDateTime = system.Now();
        myEvent1.OwnerId = User1.Id;
       
        Insert myEvent1;
          system.runAs(User2){
            try{
                Update myEvent1;
                }catch(Exception ex){
          }
        }
       
        system.runAs(User1){
            try{
                Update myEvent1;
            }catch(Exception ex){     
            }
        }
    }
}
 
Hello, I managed to send a notification email to all event Invitees using Visual Workflow.
I'm now searching a way to include the link to the event in this email. But, the ID is different :
Example :
id of the event for the owner : 00U6E0000021pVY
id of the event for the first invitee : 00U6E0000021pVZ
id of the event for the second invitee : 00U6E0000021pVa
And I only managed to get the first one.
Does anyone has an idea ?
Hi,
I have this trigger which create an event in the approver calendar when a leave request is validated.
I need to delete this event when the leave request is cancelled.
Can you please help ? I am really new with apex.
Thank you for your help !


trigger ApexTrigger_LeaveRequest on LeaveRequest__c (after update) {


    If(AvoidRecursion.isFirstRun()){
        If(!AdminSettings__c.getInstance().DisableTg__c){ 
        
        For (LeaveRequest__c leaveRequest : Trigger.new) {
        
        // Access the "old" record by its Id in Trigger.oldMap
        LeaveRequest__c oldLeaveRequest = Trigger.oldMap.get(leaveRequest.Id);
        
        // Trigger.new record is conveniently the event creation
        Boolean oldLeaveRequestAccepted = oldLeaveRequest.Status__c.equals('30 Accepted');
        Boolean leaveRequestAccepted = leaveRequest.Status__c.equals('30 Accepted');
        
        // Check that the field was changed to the correct value
        If (leaveRequest.Approver__c == '00524000000z6txAAA' && !oldLeaveRequestAccepted) {
        Event ev = new Event 
        (
            OwnerId = leaveRequest.Approver__c,
            StartDateTime = leaveRequest.StartDate__c,
            EndDateTime = leaveRequest.EndDate__c,
            Subject = (leaveRequest.ParameterLabel__c + ' - ' + leaveRequest.EmployeeName__c),
            Activite__c = 'Other',
            IsAllDayEvent = false,
            IsPrivate = true,
            recordTypeId = '0121o00000113r4AAA',
            ShowAs = 'Free',
            Type = 'Leave'
        );
        Insert ev ; 
        }else {
            Event ev = new Event();        }
       }
      }
     }
    }
Hello,

I have a flow which add informations from children records of cases.
In the text field I obtain : 1.5.663,1.5.458,1.5.663,1.4.985
I need to remove duplicates in order to display : 1.5.663,1.5.458,1.4.985

I wrote a trigger based on what I found on many forums but I'm new on Apex and I can't figure it out.

trigger Trg_Case on Case (after update) {
    For(Case myCase: Trigger.new){
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            string reports = myCase.ChildrenVersions__c;
            List<String> l = new List<String>(new Set<String>(reports.split(',')));
            String uniqueReports = String.join(l, ',');
        }
    }
}

Thanks for your answers
Hello,
I'm a beginner with triggers and test classes.
I can't manage to cover the trigger below.

Thanks for your help.

Trigger EventBeforeUpdate on Event (before insert, before update){
   
    If(AvoidRecursion.isFirstRun()){
        If(!AdminSettings__c.getInstance().DisableTg__c){
            For (Event myEvent: trigger.new){
                If(Trigger.isInsert) {
                }
       
                If(Trigger.isUpdate){
                    If(UserInfo.getUserId()!= myEvent.OwnerId && UserInfo.getUserId()!= '00524000001WRNAAA4'&& myEvent.RecordTypeId == '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
                    {
                    myEvent.adderror('ERROR1');
                    }
                    If(UserInfo.getUserId()!= myEvent.OwnerId && myEvent.RecordTypeId != '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
                    {
                    myEvent.adderror('ERROR2.');
                    }
                }
            }
        }
    }
}


My Test :

@isTest
public class EventBeforeUpdateTest{
   
    private static User makeStandardUser(String p_firstName, String p_lastName) {
       
        User standardUser = new User();
       
        standardUser.LastName = p_lastName;
        standardUser.FirstName = p_firstName;
        standardUser.Alias = 'JPJ';
        standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
        standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
        standardUser.Email = 'dev@dev.com';
        standardUser.TimeZoneSidKey = 'America/Los_Angeles';
        standardUser.LocaleSidKey = 'en_US';
        standardUser.EmailEncodingKey = 'UTF-8';
        standardUser.LanguageLocaleKey = 'en_US';
        standardUser.profileId = '00e24000000tqX0';
       
        return standardUser;
    }
   
    static testMethod void createAndUpdateEventTest() {
       
        User User1 = makeStandardUser('JUL', 'JAV');
        User User2 = makeStandardUser('CGL', 'GUI');
       
        Insert User1;
        Insert User2;
       
        AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
        setting.DisablePb__c = True;
       
        Insert setting;
       
        Event myEvent1 = new Event();
        myEvent1.RecordTypeId = '0121o00000113r4';
        myEvent1.Subject = 'New Event Standard';
        myEvent1.StartDateTime = system.Now();
        myEvent1.EndDateTime = system.Now();
        myEvent1.OwnerId = User1.Id;
       
        Insert myEvent1;
          system.runAs(User2){
            try{
                Update myEvent1;
                }catch(Exception ex){
          }
        }
       
        system.runAs(User1){
            try{
                Update myEvent1;
            }catch(Exception ex){     
            }
        }
    }
}