function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Austin Lewis 12Austin Lewis 12 

Before Update trigger not firing

I am entirely new to APEX and a novice to coding in general. I have a apex trigger on the ContentDocumentLink object that calls a flow, but I am unable to get the trigger to fire on udpates as well as inserts. Here is my trigger below


trigger SaveNote on ContentDocumentLink (before insert, before update, after insert) {
 IF(Trigger.isBefore){
    IF(Trigger.isInsert){
      ContentDocumentLink [] newnote=Trigger.new;
      mySaveNote.SaveNote(newnote);
      }
    IF(Trigger.isUpdate){
      ContentDocumentLink [] newnote=Trigger.new;
      mySaveNote.SaveNote(newnote);
    }
   }
}

Here is my class

public class mySaveNote {
public static void SaveNote (ContentDocumentLink [] newnote){
    for(ContentDocumentLink n: newnote){
    Map<String, Object> params = new Map<String, Object>();
    params.put('ContentDocumentLinkID',n.Id);
    
    Flow.Interview.CCOF_Note_Create_and_Udpate CCOF_Note_Create_and_UdpateFlow = new Flow.Interview.CCOF_Note_Create_and_Udpate(params);
    CCOF_Note_Create_and_UdpateFlow.start();
    }
}
}

 
VamsiVamsi
Hi,

Can you please make use of ​below code and give it a try 
public class mySaveNote
 {
  public static void SaveNote (ContentDocumentLink [] newnote)
 {
Map<String, Object> params = new Map<String, Object>();
    for(ContentDocumentLink n: newnote)
{
    
    params.put('ContentDocumentLinkID',n.Id);
  }  
  
  Flow.Interview.CCOF_Note_Create_and_Udpate CCOF_Note_Create_and_UdpateFlow  = new Flow.Interview.CCOF_Note_Create_and_Udpate(params);
    CCOF_Note_Create_and_UdpateFlow.start();
    }
}
}


 
Austin Lewis 12Austin Lewis 12
Hi Vamshi, Thank you for your reply! Sadly the updated class you posted did not trigger the trigger when updating the record. Thank you though. The trigger still only fires upon insert. 
VamsiVamsi
Hi Austin,

Could you please verify that flow is working as expected on updates manually..!!
VamsiVamsi
Else Try the below trigger 
 
​trigger SaveNote on ContentDocumentLink (before insert, before update) 
{
    IF(Trigger.isInsert)
    {
      ContentDocumentLink [] newnote = Trigger.new;
      mySaveNote.SaveNote(newnote);
   }else IF(Trigger.isUpdate)
   {
      ContentDocumentLink [] newnote =Trigger.new;
      mySaveNote.SaveNote(newnote);
    }
   
}

 
Austin Lewis 12Austin Lewis 12
HI Vamshi,

I am confused by your latest comment. The trigger calls a flow that runs whenever a ContentDocumentLink is created (and is supposed to work when the record is also updated), which then automatically runs the flow which updates a specific account (which I am using to check to see if the flow has run). Currently my flow runs when i manually create a ContentNote which creates a ContentDocumentLink (by SF default), but it seems that my flow is not triggered when i update a ContentNote. All inserts or updates of notes are manual
Austin Lewis 12Austin Lewis 12
Hi Vamshi, 
Sadly updating the trigger to ELSE IF (Trigger.isUpdate)  instead of IF(Trigger.isUpdate) did not trigger my flow upon a record update.
Is there a chance that the test class could be causing these errors? Odd that it fires on insert
VamsiVamsi
Hi,

From your comment noticed these line " Currently my flow runs when i manually create a ContentNote which creates a ContentDocumentLink (by SF default), but it seems that my flow is not triggered when i update a ContentNote. All inserts or updates of notes are manual"

you should perform an update on ContentDocumentLink that causes the trigger to fire and in turn initiates the flow to update account 
VamsiVamsi
Please perform an update on ContentDocumentLink and then watch out the result 
Austin Lewis 12Austin Lewis 12
HI Vamshi, 

Thank you! For some reason this helped something click in my head. I was wondering, do you know / is it possible to reference another object in a trigger other than the object that the trigger is on?
Austin Lewis 12Austin Lewis 12
I think im actually going to be transferring the trigger to a new object. I beleive the problem is that the ContentDocumentLink does not update with new versions of the linked content document -- it simply serves as a link between the related Object andthe content document. Ill see!
 
VamsiVamsi
Yes we can reference other objects
Austin Lewis 12Austin Lewis 12
Hi vamshi, could you help me understand how to access the content of the most recent contentnote using this trigger?
VamsiVamsi
Hi,

You can get the old values usning Trigger.old context variable