Workflow rule or script to email sites with safety stock when product status changes

SOLVED

Good afternoon!

I am looking to set up a new workflow rule which will automatically email all of the affected product-sites when a product status is changed. 

So far, I have managed to set up a rule that will detect when the custom status field we have is changed from "OK" to "DS". It detects modification in the ITM event code, and looks like this:

At the end of the transaction, if the status is either of those codes, DM or DS, it will trigger a mail and works as expected.

However, we have over 300,000 products and 20 product sites, and not all product sites have all products. So what I'd like to have in the the recipient tab, is for more conditions to be set where, if a product site of the product that was modified has a safety stock that is "greater than 0", a mail will be triggered and sent to the affected site. Here's an example of how that should look, based on this logic:

However, the problem is that the product-site (event code ITF) is not related to the product (event code ITM). In my test email without those conditions, I put those calls in the message box to see if they would output. [F:ITF]STOFCY was returned blank and [F:ITF]SAFSTO was returned as 0. So it would seem that there is no communication between ITM and ITF in this context.

I am wondering if it is possible to set something like this up by other means, such as triggering an action/script that will perform where/if clauses to check the edited product in question from from the workflow. Like taking [F:ITM]ITMREF and injecting it into an if statement such that it can be called into ITF from ITM, and checked in ITF for the safety stock value to trigger emails from there, etc, if that makes sense.

Or maybe this is entirely possible from a workflow standpoint.

Any help is appreciated :)

Zoey

  • +1
    verified answer

    Hi Zoey,

    You can achieve your requirement by calling workflow from script.

    in item Master when user click on Save button $VERIF_MOD action please add below piece of code after workflow creation.

    If !Clalev([F:ZITF]) : Local File ITMFACILIT[F:ZITF] : Endif #Table opening
    Global Char GZZITM
    Global Char GZZFCY
    Global Decimal GZZSFSTK
    Filter[F:ZITF] Where[F:ZITF]ITMREF = [M:ITM0]ITMREF & [F:ZITF]SAFSTO >0
    For[F:ZITF]
    GZZITM = [F:ZITF]ITMREF
    GZZFCY = [F:ZITF]STOFCY
    GZZSFSTK = [F:ZITF]SAFSTO

    ##----------------- Signature Workflow -----------------#
    Local Integer TYPEVT : TYPEVT = 1 #Event type
    Local Char CODEVT : CODEVT = 'ZITF' #Event Code You can add new event code if required in 908 Misc. table
    Local Char OPERAT : OPERAT = ''
    Local Char CLEOBJ : CLEOBJ = [F:ZITF]ITMREF
    Call WORKFLOW(TYPEVT,CODEVT,OPERAT,CLEOBJ) From AWRK
    ##----------------- Signature Workflow -----------------#
    Raz GZZITM, GZZFCY, GZZSFSTK
    Next

    #use above global variables in workflow  Message tab.

    If you need any further help please reach me & happy to support you.

    Skype: venu.josh

    Wts app: 00966 583026960

    Regards,

    Venu Babu

  • 0 in reply to VeNu BaBu

    Thank you very much for you reply. I mostly understand how the code works, but do not have the knowledge to implement it. Could you possibly reply here with detailed instructions about that? I would really appreciate it :)

    Thank you,

    Zoey

  • +1 in reply to VeNu BaBu
    verified answer

     Thank you very much again for the information here, and sorry we couldn't connect on Saturday. I figured out and implemented a solution based on your script here, with some slight modifications.

     I noticed you checked that you have the same question, so I have tagged you to send a notification that this question is solved. Here is my resolution based on Venu's work above.

    Just a note before I begin here, I really wanted to avoid editing standard or specific scripts relating to the ITMMASTER window (SUBITM.src or SPEITM.src). I did not feel comfortable with this and instead employed the use of a custom script, action and workflow to achieve the same low-level effect. This way, you can test everything you want without having to worry about breaking something.

    Since we will be calling a workflow from script, you will need to first create a new event code. To do this, open the Miscellaneous Tables window (process FONADI), look for table 908 in the left hand side, called "Miscellaneous Event Type". At the bottom of the List of Values, create a new one. This event code will be unique and should only be used to call this specific workflow. I called mine "ZDS":

    The next step is to set up the script itself. You can do this by opening the script editor (process ADOTRT). Make sure the archive folder is set to LIVE, and the type SRC. The file naming convention we use at my job is to start all user-created objects with a Z, so I called my script ZCUSTOM. (I will use it for other script activities and other actions in the future, so I did not name it specific to our question here).

    The flow of the script is as follows:

    > Begin a sub program which we will call from an action. I called mine ZDISCOW (Discontinuation Workflow)

    > Open the Product-site table and set the class to a custom name (Venu said this avoids techno errors)

    > Set global variables for Item name, stock site and safety stock decimal

    > Filter the ITF (product-site) object with specific conditions to look for the product name in question, and where the safety stock unit is above 0.

    > Loop through the ITF objects based on these conditions

    > set the global variables to their respective values for use in the workflow later

    > call the workflow (requires a custom Misc. Event Code)

    > clear the global variables

    > Next site

    > End

    My code based on Venu's example looks like this:

    #--------SETUP--------#
    
    # Name program and open ITF table as a custom name
    Subprog ZDISCOW
    If !Clalev([F:ZITF]) : Local File ITMFACILIT[F:ZITF] : Endif
    
    # Set global variables #
    Global Char GZITM # Item name string
    Global Char GZFCY # Item stock site string
    Global Decimal GZSFS # Safety Stock decimal
    
    #--------Filter & Loop for existence of safety stock--------#
    Filter[F:ZITF] Where [F:ZITF]ITMREF = [F:ITM]ITMREF & [F:ZITF]SAFSTO > 0
    
    For[F:ZITF]
    
    # Set globsal variables to use in workflow message #
    GZITM = [F:ZITF]ITMREF # Set GZITM to product site product name
    GZFCY = [F:ZITF]STOFCY # Set GZFCY to product site stock site
    GZSFS = [F:ZITF]SAFSTO # Set GZSFS to safety stock number
    
    # Call workflow #
    Local Integer TYPEVT : TYPEVT = 1 # Miscellaneous type
    Local Char CODEVT : CODEVT = 'ZDS' # Custom event name for this script
    Local Char OPERAT : OPERAT = '' # No rules
    Local Char CLEOBJ : CLEOBJ = [F:ITM]ITMREF # Product object we will be calling classes from
    Call WORKFLOW(TYPEVT,CODEVT,OPERAT,CLEOBJ) From AWRK # Call the workflow with the above parameters
    
    #--------Clear Globals for next site--------#
    Raz GZITM, GZFCY, GZSFS
    
    #--------Next site--------#
    Next
    
    #--------End of Subprogram--------#
    End

    To save the script, click compile. You cannot run it from this context because it is expecting ITMMASTER classes when there are none (they will be opened from invoking the Product Master later)

    Next, let's set up our custom Workflow Rule for this script. This can be found in GESAWA. I called mine "ZDISCOD" for Discontinued Detail. This workflow will be our auto-mailer. The only conditions we need to set are the ones in the recipient tab, which will receive an email to notify then a product has been discontinued. The setup looks like this:

     

    No header conditions are required, as we will set those in another workflow. 

    The messages tab looks like this, where I have set the Stock Site global variable from our script to each stock site, and set up a specific email for that condition.

    Next, let's create the Action which will invoke our script in the first place. Head over to Action Dictionary (process GESACT), click new, and set it up as I have:

    Name your action and give it a description, set template and type to Outside Model and Miscellaneous, and then call your subprogram from your custom script under the Parameters field. My standard script name I mentioned before is ZCUSTOM, and my program name which calls my workflow is ZDISCOW. The parameters TAB can be left as-is. Click create! If you set it up properly, there will be no errors.

    Lastly, we need some way to invoke this action, which will come after we set a product status to discontinued. There are a few ways to do this, but the easiest one I found was.. to use a workflow! Huzzah! Back to Workflow Rules we go Slight smile

    The whole point of this mess was that we needed to find a way to detect when a product was changed to DS or DM, and ONLY NOTIFY the sites with safety stock. Workflow Rules are great for the first part, so let's set that up now.

    My workflow to detect the status change is called "ZDISCOW" so it's easy to remember what it does and is liked with the script in question. Since we need to DETECT when a product status is changed, we will check for event type OBJECT of code ITM. Set the Operation to M, since we only want to check when the object is modified.

    THE MOST IMPORTANT PART of this workflow will be our header conditions. We ONLY want it to activate at the END of the workflow, AFTER the new status has been saved. So, tick the box "End of transaction" and input your conditions for the status. We use two types, DS and DM, so I've set them to [F:ITM]TSICOD(0), which is the statistical group status field for product status. It may be different for you.

    Now, this time, instead of triggering a mail, once the conditions are met, we will trigger our previously created action! So tick "Trigger action" and head over to the action tab. (Please note, I have Trigger mail clicked here for debugging purposes).

    Put your custom action code in, and make sure triggering is set to Workflow end!

    Make sure both workflows are activated, and now whenever you change an item to your discontinued status, it will email all of the sites with a safety stock! (Don't forget to set your message in the first workflow). 

    !!!! PLEASE NOTE !!!!

    If you make a modification to ANY page with an aforementioned discontinued status, it will still send the email to any sites with a remaining safety stock set! That's why it is VERY IMPORTANT to remove the safety stock for all affected sites for that product, or they will be bombarded with emails every time you change anything in the product sheet so long as the status remains as discontinued.

    Feel free to ask questions if you have any! I'm off to ask more myself...