Script to only fire off Once

Before I start to write this script, is there a way to only allow a script to fire off once?  Background of the script I will create:  Need to populate the "SO DETAILS PURCHASEORDERNO" if blank with a "C" for all customers except 3 or 4, and only on lines with vendorno = "0001226".  However, I don't want the script to fire off every time an update is made to the SO, or if we delete the "C" in the POno field.  Basically, do not want the "C" to reappear if we delete it or for a legitimate POno to get overwritten by the "C".

Parents
  • Why couldn't you use a Column-Post Validate event on the ItemCode field to fill the PO No. field for your conditions. It would only fire once per ItemCode and not when otherwise changing the line. 

  • in reply to connex

    Column Post Validate on ItemCode should work if on a version that allows selecting it (older versions didn't but I think 2020+ does in a detail script).

    A table Post Read event filtered by oBusObj.EditState = 2 also works and once the record is saved, the EditState will be equal to 1.

    Another event could be the Set Default Values one, I haven't used it much but it is supposed to run when default values are being set by the business object on new records.

  • in reply to jland47

    Remove the column trigger, and just leave the line pre-write.  They'd have to add the value before moving off the line for the first time, but that is the best you can do in a line script.

    David,

    I always recommend use of pre-totals for header scripts instead of pre-write, since $ changes in a header pre-write script cause serious problems.

  • in reply to Kevin M

    I thought about that but if the user is using the create PO button on the Totals tab then it will fire before the user has a chance to create a PO.  If the user manually creates the PO and plugs the PO # in the line then this won't be an issue.  I do agree that if changes to totals are involved, the pre-totals event is best.

  • in reply to Kevin M

    I have not had any experience on my own with looping so I am assuming I have the script wrong, tried to take examples from previous post.  I get this error when running this script on a SO header pre-write and pre-totals.

    If oSession.CompanyCode = "ROI" then
    
    	If oBusObj.EditState = 2 then
    	
    	nCustomerNo = ""
    	sCCheckBox = ""
    	nVendorno = ""
    	nPOno = ""
    
    	rVal = oHeaderObj.GetValue("CustomerNo$", nCustomerNo)
    	rVal = oBusObj.GetValue("UDF_CCHECKBOX$", sCCheckBox)
    	rVal = oBusObj.GetValue("VendorNo$", nVendorNo)
    	rVal = oBusObj.GetValue("PurchaseOrderNo$", nPOno)
    
    		If nCustomerNo <> "2861000" and nCustomerNo <> "2303006" then
    
    		Set oLines = oSession.AsObject(oBusObj.Lines)
    		rVal = oLines.MoveFirst()
    		Do Until cBool(oLines.EOF) 'Keep looping until record pointer is on last line
    			
    			If sCCheckbox = "N" then
    
    				If nVendorno = "0001226" then		
    
    					If nPOno = "" then			
    					rVal = oBusObj.SetValue("PurchaseOrderNo$", "C")
    					rVal = oBusObj.SetValue("UDF_CCHECKBOX$", "Y")
    						
    					End if
    				End if
    			End if
    		rVal = oLines.MoveNext()
    		Loop	
    		End if
    	End if
    End if

  • in reply to jland47

    If you have attached this script to pre-totals or pre-write of SO Header, then you cannot use oHeaderObj this object handle is used when your scope is in the detail object.  You should be able to replace the oHeaderObj.  with oBusObj. assuming all the other values are in header object as well (ie UDF_CCHECKBOX, VendorNo, PurchaseOrderNo, etc.)

  • in reply to jland47

    oHeaderObj is only valid in a line level script.  In a header level script, oBusObj is for the header and the oBusObj.Lines property is how you access the lines.  You need to move all of the GetValue lines against the lines so they are within the line loop and change the object from oBusObj to oLines.

  • in reply to jland47

    As David and Elliott explain, changing from a detail trigger to a header trigger requires structural changes to your entire script logic.

  • in reply to Kevin M

    Changed script to account for the header trigger.  Any orders imported in through Insynch the script fires off correctly, however if I manually type a SO the script doesn't fire?

     

    If oSession.CompanyCode = "ROI" then
    
    	If oBusObj.EditState = 2 then
    	
    	nCustomerNo = ""
    
    	rVal = oBusObj.GetValue("CustomerNo$", nCustomerNo)
    	
    		If nCustomerNo <> "2861000" and nCustomerNo <> "2303006" then
    
    		Set oLines = oSession.AsObject(oBusObj.Lines)
    		rVal = oLines.MoveFirst()
    		Do Until cBool(oLines.EOF) 'Keep looping until record pointer is on last line
    
    			sCCheckBox = ""
    			nVendorno = ""
    			nPOno = ""
    
    			rVal = oLines.GetValue("UDF_CCHECKBOX$", sCCheckBox)
    			rVal = oLines.GetValue("VendorNo$", nVendorNo)
    			rVal = oLines.GetValue("PurchaseOrderNo$", nPOno)
    
    			
    			If sCCheckbox = "N" then
    
    				If nVendorno = "0001226" then		
    
    					If nPOno = "" then			
    					rVal = oLines.SetValue("PurchaseOrderNo$", "C")
    					rVal = oLines.SetValue("UDF_CCHECKBOX$", "Y")
    						
    					End if
    				End if
    			End if
    		rVal = oLines.MoveNext()
    		Loop	
    		End if
    	End if
    End if

  • in reply to Kevin M

    I put message boxes after each GetValue and each message box pops up but displays "nCustomerNo:" for example, does this mean it is not actually getting the value or do I have the message script wrong?

    rVal = oSession.AsObject(oSession.UI).MessageBox("nCustomerNo")

    Confused on why an imported order would work and not a manually entered order, they have to go through the same tables first to create the header then the lines correct?

  • in reply to jland47

    rVal = oSession.AsObject(oSession.UI).MessageBox("nCustomerNo = " & nCustomerNo)

    Wrapping something in quotes means print that text.  Without the quotes means print the variable's value. 

    (And CustomerNo is a string field, not numeric... convention is to prefix string variables with "s" instead of "n"... sCustomerNo... but it technically doesn't matter in this kind of script).

  • in reply to Kevin M

    All GetValues return the correct variables in the message box, the script would've stopped if the SO wasn't in EditState<>2, the loop is correct because if I enter 2 lines I get double the check boxes.  The script just doesn't preform the SetValue, it's on the table pre-write?  The orders that do get imported do not have the message box pop up, might be a setting ROI put in to allow orders to import?

Reply
  • in reply to Kevin M

    All GetValues return the correct variables in the message box, the script would've stopped if the SO wasn't in EditState<>2, the loop is correct because if I enter 2 lines I get double the check boxes.  The script just doesn't preform the SetValue, it's on the table pre-write?  The orders that do get imported do not have the message box pop up, might be a setting ROI put in to allow orders to import?

Children
  • in reply to jland47

    Think I figured it out, needed a rVal = oLines.Write() below the SetValues.  Could someone explain why this is needed when a order would be manually entered and not imported?  Waiting for orders to import to make sure they fire off correctly.

  • in reply to jland47

    You need to save (write) the line field changes as you go, but nothing is final until the header is saved.  When you are entering data in the Sage screen, a line Write() happens each time you move off a new line (or after a line edit).  That is just the way things work.  If you don't write, SetValue changes are lost the moment you MoveNext.