SO Invoice Line Addition

SOLVED

I have a good idea of how I picture this script working, never dealt with UDT's but from previous post it looks like the same rules.  I am just not positive on how to "loop" through the SO Invoice lines to find the value on the UDT in order to enter a value from the UDT to the SO Invoice lines.  Situation; we have a UDT_ArtCharges with columns SO#, Customer Name, ART_TOTAL.  When a SO is invoiced through "SO Invoice Data Entry" I want to loop through the lines to see if any of the lines contain a miscellaneous item code of "/ART" (user should add this in SO entry but sometimes doesn't), if no lines contain "/ART" I want to pull up the UDT and find that SO number being invoiced in the UDT and add a line to the invoice with the corresponding "ART_TOTAL" for that SO number.  The other feature I would like is to add a "yes" or "no" option before adding the line to the invoice, something else I haven't played with yet.  I've complied a mess of code from past post, which part should I focus on first to clean it up.


sCompany=oSession.CompanyCode
If oSession.Updating > 0 or oSession.StartProgram <> "SO_INVOICE_UI" THEN 

Exit Sub

	if sCompany = "ROI" Then

	retVal = 0
	invoiceType  = ""
	retVal = oHeaderObj.GetValue("InvoiceType$", invoiceType)

		If invoiceType = "IN" Then

		strTemp = ""
		Set oLines = oBusObj.AsObject(oBusObj.Lines)

		retVal = oLines.MoveFirst()

		Do While Not(cBool(oLines.EOF))
    		retVal = oLines.GetValue("ItemCode$", strTemp)
    		retVal = oSession.AsObject(oSession.UI).MessageBox(strTemp)
   		retVal = oLines.MoveNext()
		Loop

			If strTemp <> "/ART" then

			Set oMyObj = oSession.AsObject(oSession.GetObject("CM_UDTMaint_bus","CI_UDT_ArtCharges"))

			retVal = oMyObj.Find(SalesOrder)

			retVal = 0
			order = ""

			retVal = oBusObJ.GetValue("SalesOrderNo$", order)

				If retVal = order then

				retVal =0
				oArt = ""

				oArt = oSession.GetObject("CM_UDTMaint_bus", "CI_UDT_ArtCharges")
				retVal = oBusObj.AddLine()
				retVal = oBusObj.SetValue("ItemCode$", "/ART")
				retVal = oBusObj.SetValue("ItemType$", "5")
				retVal = oBusObj.SetValue("Ordered$", "1")
				retVal = oBusObj.SetValue("UnitPrice$", "UDT_ART_TOTAL")
				retVal = oBusObj.Write()
				r = oScript.ActivateProcedure("*ALL*")
					If retVal<>0 Then
					oScript.LinesAdded = 1

    					End If
    				End If
    			End If
    		End If
    	End If

  • 0 in reply to jland47

    If you had debug mode enabled and were viewing the trace log, you would probably see that the line write is failing because of the missing GL accounts.  So make sure they are assigned in Miscellaneous Item Maintenance. 

    As for the UDT's module, yes, it makes sense to create the UDT under the module where it is used.

  • +1 in reply to David Speck
    verified answer

    Just tested every possible example and all worked accordingly, probably will enable debug mode to learn more about it.  Also took out the extra lines since item was just a charge as you mentioned.  Final code below, greatly appreciated!

    aValidCompanies = Array("ROI")
    aValidStartPrograms = Array("SO_Invoice_UI")
    aValidInvoiceTypes = Array("IN")
    sItemCodeToFind = "/ART"
    If oSession.CompanyCode <> "" And InStr(UCase(Join(aValidCompanies, C h r(138))), UCase(oSession.CompanyCode)) <> 0 Then
    	If oSession.Updating = 0 Then
    		If oSession.StartProgram <> "" And InStr(UCase(Join(aValidStartPrograms, C h r(138))), UCase(oSession.StartProgram)) <> 0 Then
    			sInvoiceType = "" : oBusObj.GetValue "InvoiceType$", sInvoiceType
    			If sInvoiceType <> "" And InStr(UCase(Join(aValidInvoiceTypes, C h r(138))), UCase(sInvoiceType)) <> 0 Then
    				sSalesOrderNo = "" : oBusObj.GetValue "SalesOrderNo$", sSalesOrderNo
    				If sSalesOrderNo <> "" Then
    					Set oLines = oSession.AsObject(oBusObj.Lines)
    					oLines.MoveFirst
    					bItemCodeToFindLineExists = False
    					Do Until CBool(oLines.Eof)
    						If Not(bItemCodeToFindLineExists) Then
    							sItemCode = "" : oLines.GetValue "ItemCode$", sItemCode
    							If UCase(sItemCode) = UCase(sItemCodeToFind) Then
    								bItemCodeToFindLineExists = True
    							End If
    						End If
    						oLines.MoveNext
    					Loop
    					If Not(bItemCodeToFindLineExists) Then
    						If InStr(UCase(oBusObj.GetDataSources()), UCase("SalesOrderNo_ARTCHARGES")) = 0 Then
    							nSO_UDT_ARTCHARGES_Svc = 0 : nSO_UDT_ARTCHARGES_Svc = oSession.GetObject("CM_UDT_Svc", "SO_UDT_ARTCHARGES")
    							If nSO_UDT_ARTCHARGES_Svc <> 0 Then
    								oSession.DropObject nSO_UDT_ARTCHARGES_Svc
    								nSO_UDT_ARTCHARGES_Svc_ChildCollection = 0 : nSO_UDT_ARTCHARGES_Svc_ChildCollection = oBusObj.InitChildCollection("SalesOrderNo_ARTCHARGES", "CM_UDT_Svc", "SalesOrderNo$",	"kPrimary", "SO_UDT_ARTCHARGES")
    							End If
    						End If
    						If InStr(UCase(oBusObj.GetDataSources()), UCase("SalesOrderNo_ARTCHARGES")) > 0 Then
    							nSalesOrderNo_ARTCHARGES = 0 : nSalesOrderNo_ARTCHARGES = oBusObj.GetChildHandle("SalesOrderNo_ARTCHARGES")
    							If nSalesOrderNo_ARTCHARGES <> 0 Then
    								Set oSalesOrderNo_ARTCHARGES = oSession.AsObject(nSalesOrderNo_ARTCHARGES)
    								nRetVal = 0 : nRetVal = oBusObj.ReadAdditional("SalesOrderNo_ARTCHARGES")
    								If nRetVal <> 0 Then
    									sUDF_Art_OrderNo = "" : oSalesOrderNo_ARTCHARGES.GetValue "UDF_ART_ORDERNO$", sUDF_Art_OrderNo
    									If bArgDebugPrint Then oScript.DebugPrint "sUDF_Art_OrderNo: " & sUDF_Art_OrderNo
    									If UCase(sSalesOrderNo) = UCase(sUDF_Art_OrderNo) Then
    										bAddLine = False
    										If oSession.UI = 0 Then
    											bAddLine = True
    										Else
    											sRetVal = "" : sRetVal = oSession.AsObject(oSession.UI).MessageBox("", "The """ & sItemCodeToFind & """ line does not exist." & vbCrLf & vbCrLf & "Do you want to add it?", "Style=YesNo, Icon=?")
    											If UCase(sRetVal) = UCase("Yes") Then
    												bAddLine = True
    											End If
    										End If
    										If bAddLine Then
    											nUDF_Art_Charge = 0 : oSalesOrderNo_ARTCHARGES.GetValue "UDF_ART_CHARGE", nUDF_Art_Charge
    											If bArgDebugPrint Then oScript.DebugPrint "nUDF_Art_Charge: " & nUDF_Art_Charge
    											nRetVal = 0 : nRetVal = oLines.AddLine()
    											If bArgDebugPrint Then oScript.DebugPrint "oLines.AddLine(): " & nRetVal
    											nRetVal = 0 : nRetVal = oLines.SetValue("ItemCode$", sItemCodeToFind)
    											oLines.SetValue "ExtensionAmt", nUDF_Art_Charge
    											nRetVal = 0 : nRetVal = oLines.Write()
    											If bArgDebugPrint Then oScript.DebugPrint "oLines.Write(): " & nRetVal
    										End If
    									End If
    								End If
    								Set oSalesOrderNo_ARTCHARGES = Nothing
    							End If
    						End If
    					End If
    				End If
    			End If
    		End If
    	End If
    End If