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
Eugene RitenbandEugene Ritenband 

Need help with an Apex Trigger to Update a Checkbox field after a Document is uploaded

Hi All, 

I am very new to writing triggers and need some help on this one. On the Opportunity object, there is a custom checkbox field called "Invoice Attached". When an Invoice is uploaded to that Opp, I would like the checkbox will be marked. I have attached my Trigger and Test Class. Any help would be greatly appreciated. User-added imageUser-added image
Best Answer chosen by Eugene Ritenband
Rick HaagRick Haag
Hi Eugene,

Take a look at this post as it might be helpful.
Updating a checkbox field if a File is uploaded through ContentDocumentLink trigger (https://developer.salesforce.com/forums/?id=9060G0000005Me4QAE)

You might consider using a Lightning Flow Screen to guide the user in the file upload process and include an update for the "Invoice_Attached__c" field. Otherwise, it may not be certain to know if the file is an invoice or some errant file.

All Answers

Rick HaagRick Haag
Hi Eugene,

Take a look at this post as it might be helpful.
Updating a checkbox field if a File is uploaded through ContentDocumentLink trigger (https://developer.salesforce.com/forums/?id=9060G0000005Me4QAE)

You might consider using a Lightning Flow Screen to guide the user in the file upload process and include an update for the "Invoice_Attached__c" field. Otherwise, it may not be certain to know if the file is an invoice or some errant file.
This was selected as the best answer
Eugene RitenbandEugene Ritenband
Hi Rick, 

Thanks for sending over the link. I will take a look at it. Thank you! 

-Eugene
Eugene RitenbandEugene Ritenband
Hey Rick, 

I am hoping you can help me with this. I got the trigger to fire and the checkbox is now checked, but in order to have the checkbox checked, I need to refresh the page. Is there a way to have the checkbox checked without having to refresh the page? Thank you! 
User-added image
Rick HaagRick Haag
Eugene,
The data is committed to the database, even if the UI isn't updated.  I presume this is an issue for you in the Lightning UI as in my experience this works as expected and updates the screen in Classic when adding a file.

To trigger a refresh in Lightning for this scenario it appears a bit more is required as the trigger will also need to publish a platform event that can be received by a component to run a "force:refreshView". I haven't looked into this much further, so this may not even be possible.

Here is an alternative to using the Apex trigger:
  1. Create a flow screen with the upload file component that will create the ContentDocumentLink to the Related Opportunity as well as update the Opportunity.Invoiced_Attached__c=TRUE.
  2. Create an Action labeled "Upload Invoice", select Flow and the Flow Name.  When the action completes, the page should refresh. If it doesn't you can use the "Update Screen" Flow Action Component found on unofficialsf.com (https://unofficialsf.com/the-update-screen-flow-action-component/). I have used this method previously and it works well.

 
Eugene RitenbandEugene Ritenband
Thank you, Rick. I will look into the force:refreshview further. Appreciate the help!