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".

  • in reply to David_Speck

    Column Post-Validate is in every version since script events were introduced in 2010 with version 4.40. I think it is your safest bet because any event that precedes setting the ItemCode will cause the PO number field to be cleared.

  • in reply to connex

    The event existed since 4.4 but in certain tables, certain columns were not selectable on column events.  I seem to recall at one point, the ItemCode column could not be selected on detail tables.  I can't recall what version was the last version I saw this on though, I did just check my 2018 install and it can be selected so it may have been corrected in 2018 or maybe 2017.

  • in reply to David_Speck

    Column-Post Validate and table pre-write both work good in every aspect except the script puts the "C" value in the PO field as soon as the item code is entered, I would like it to wait until the user decides if they want to generate a PO first.  Also tried the table post read with the "editstate" filter, couldn't get the script to fire at all.  Seems like I am close with the first two events maybe just a logic sequence problem?  Here's the corrected script .

    If oSession.CompanyCode = "ROI" then
    
    sCCheckBox = ""
    nCustomerNo = ""
    nVendorno = ""
    nPOno = ""
    
    rVal = oBusObj.GetValue("UDF_CCHECKBOX$", sCCheckBox)
    rVal = oHeaderObj.GetValue("CustomerNo$", nCustomerNo)
    rVal = oBusObj.GetValue("VendorNo$", nVendorNo)
    rVal = oBusObj.GetValue("PurchaseOrderNo$", nPOno)
    
    	If sCCheckbox = "N" then
    
    		If nCustomerNo <> "2861000" and nCustomerNo <> "2303006" 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
    	End if
    End if

  • in reply to jland47

    If you want to give the user an opportunity to create a PO first, then a line level script is not the way to do this.  You'll want to switch to the pre-write event on the header table.  Grab the customer number and if appropriate, loop through each line using the same criteria against the checkbox and purchase order number and for each line that meets your criteria, set the purchase order number.  You can restrict this to new records only by checking if oBusObj.EditState = 2 so it doesn't run on existing orders.

  • 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.