Why doesn't this automation work?

I have written an automation in Velocity to remind users when they have reached their quota for images and redirect them to a page, but it doesn't work. The event that I have selected to trigger the automation is Media.BeforeCreate. Here's the code:

#set($user = false)
#set($user = $core_v2_user.Accessing)

#set($imageQuota = $core_v2_utility.ParseInt($user.ExtendedAttributes.get_Item("ImageQuota").Value))

#set($imageList = $core_v2_media.List("%{ AuthorId=$user.Id, IncludeUnpublished='True' }"))
#set($imageCount = $imageList.Count)

#if ($imageCount >= $imageQuota)
 {"redirectUrl":"$core_v2_encoding.JavascriptEncode($core_v2_urls.Custom('Select_Plan'))","message":"$core_v2_encoding.JavascriptEncode($core_v2_language.FormatString($core_v2_language.GetResource('ImageQuotaReached'), $core_v2_language.FormatNumber($imageCount), $core_v2_language.FormatNumber($imageQuota)))"}
#end



Edited
[edited by: IbrahimK at 12:29 PM (GMT 0) on Wed, Jun 24 2020]
  • Automations are not run in-line during a request, so they won't be able to redirect the user to a different page. It sounds like this logic would be better handled by making a customization to the "Media Gallery - Upload/Edit File Form" widget.

    Beyond that, a couple tips on the logic itself:

    • It's possible the image count is not updated until after the media is created - so double check the edge case to make sure it's returning the numbers you expect.
    • Once you create an Automation type via the studio, make sure to create and configure a configured Automation via the "Automations" page, and select the type you just created. This is required to actually run your automation with any selected events and required configuration provided.