Skip to main content

I was given the apex trigger below that would call a flow to send a notification after a file is uploaded to a case.  It currently works but only because the record id is hard-coded, which is then passed to the flow.  I'm not a developer, so I was wondering how to change this trigger so it doesn't hard-code the record id and instead triggers off of the file upload (assuming that's possible).  Appreciate any guidance or code-correction or even if this solution will never work.

 

trigger ContentVersionTrigger on ContentVersion (before insert, after insert) {

 

  switch on Trigger.operationType {

     when AFTER_INSERT {

         System.debug('After Insert');

         //Create parameter

         Map<String, Object> Params = new Map<String, Object>();

         Params.put('recordId','068030000031hnHAAQ');

         //create instance of Interview

         Flow.Interview flow = new Flow.Interview.Apex_Called_Flow_File_Upload_Notification(Params);           flow.start();

     }

  }

}

13 risposte
  1. Andrew Russo (BACA Systems) Forum Ambassador
    14 mar 2023, 14:04

    if it were me i would jsut have the trigger sent it to the flow for all files on cases regardless of record type and put the record type logic in the flow so its easier to update in the future. 

     

    Also, I would do it based on the content document link.

     

    try this and let me know. i just modified it off of something I have for a similar use case in my org so this might need some tweaking.

     

    trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {

    List<Id> caseIds = new List<Id>();

    for (ContentDocumentLink cdl : Trigger.new) {

    if (cdl.LinkedEntityId.getSObjectType() == Case.SObjectType) {

    caseIds.add(cdl.LinkedEntityId);

    }

    }

    if (!caseIds.isEmpty()) {

    Map<String, Object> params = new Map<String, Object>();

    params.put('recordId', caseIds[0]);

    Flow.Interview.Apex_Called_Flow_File_Upload_Notification flow = new Flow.Interview.Apex_Called_Flow_File_Upload_Notification(params);

    flow.start();

    }

    }

0/9000