2

I want to create a event after my record have been approved, I have a checkbox checked when my record is approved so I can create my event, but the record will be locked !!!!

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was:

"Apex trigger TRTimeOffAfterUpdate caused an unexpected exception, contact your administrator: TRTimeOffAfterUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.TRTimeOffAfterUpdate: line 4, column 1".

trigger TRTimeOffAfterUpdate on Time_Off__c (after update) {

    for (Time_Off__c toEvent : Trigger.new) {
        if(toEvent.Is_approved__c=true){
       // Event event = new Event
       Event ev= new Event 
       (
            OwnerId = toEvent.CreatedById,
            StartDateTime = toEvent.Start_date__c,
            EndDateTime = toEvent.End_date__c,
            Subject = toEvent.Pattern__c,
            Description =  toEvent.Comment__c,
            ShowAs = 'Out of Office'
        );
        insert ev ;             
    }
}
2
  • Do you have a multi-step approval process? Are you indicating via a field update that the record is approved during 'final approval actions'
    – cropredy
    Commented Apr 10, 2014 at 16:05
  • Yes I have a multistep approval, with a field update and set the field Is_approved__c checkbox to true once the record is approved! based to this I want to create a event on his calander but only the one who has the right to approve my records (in this situation my manager) can create an event on my calander
    – LoveLace
    Commented Apr 11, 2014 at 8:12

1 Answer 1

0

First, I made a small edit to your trigger. Instead of the single equal sign, you need a double equal sign. Also, I made it check if the old value was unchecked so you don't keep making records over and over.

trigger TRTimeOffAfterUpdate on Time_Off__c (after update) {

for (Time_Off__c toEvent : Trigger.new) {
    if(toEvent.Is_approved__c==true && Trigger.oldMap.get(toEvent.Id).Is_approved__c == false){
   // Event event = new Event
   Event ev= new Event 
   (
        OwnerId = toEvent.CreatedById,
        StartDateTime = toEvent.Start_date__c,
        EndDateTime = toEvent.End_date__c,
        Subject = toEvent.Pattern__c,
        Description =  toEvent.Comment__c,
        ShowAs = 'Out of Office'
    );
    insert ev ;             
}
}

Does this work?

2
  • I have here a management rule, is that only the manager who has the right to approve my record, can create an event on my calander after approving my record !!! I think I have to recover the manager ID and assign it to the OwnerId, I'm gonna try it now
    – LoveLace
    Commented Apr 11, 2014 at 8:15
  • Did my code let you create the event? If so, please mark the answer as the right one so we can close the question. Commented Apr 11, 2014 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .