Import sales orders from a csv file

Hi Forum,

 

I've trying to have this script working but i guess i need to post it to get help from you guys....

What i am trying to do is import sales orders from a csv file.  some orders have more than 1 line, most of them are single line.

i need to get the Customer Number doing a find by customer Name. Once i get the customer number i build the S/O header and lines. My goal is to have one script... i have one script that creates new customers and new ship to codes.  The S/O code will be appended to the other script, but that's a different story.

i cannot make it loop if the order has more than 1 line.

 

Here is the code... what's left of the code, i've been adding and deleting lines...

 

'Create ProvideX COM Object
Set oScript = CreateObject ("ProvideX.Script")

'The Init method must be the first method called
' Please remember to correct the MAS90\Home path
oScript.Init("C:\Sage Software\MAS 90\VERSION4.4.6\Mas90\home")

'The Session object must be the first MAS 90 object created
Set oSS = oScript.NewObject("SY_Session")

mdate = oSession.sModuleDate
'sdate = oSession.sSystemDate
'ucode = oSession.sUserCode

' Set the user for the Session
r = oSS.nLogon()
If r=0 Then
	' Please remember to correct the username, password
	r = oss.nSetUser("im","metals")
End If

' Set the company, module date and module for the Session
r = oss.nsetcompany("FIT")
r = oSS.nSetDate("S/O",mdate)
r = oSS.nSetModule("S/O")

' Instantiate a Sales Order business object
oSEC = oSS.nSetProgram(oSS.nLookupTask("SO_SalesOrder_ui"))
Set o = oScript.NewObject("SO_SalesOrder_bus", oSS)

' Instantiate a AR_Customer business object
'Set oCust = oScript.NewObject("AR_Customer_bus, oSS)

'Open the CSV file

dim fs,objTextFile
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile("C:\Akatz\Fitness First\orders.csv")

Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,",")
strOrderNo = arrstr(0)
strName = arrStr(1)
strPhone = arrStr(2)
strAddress1 = arrStr(12)
strAddress2 = arrStr(13)
strAddress3 = arrStr(14)
'strCity = arrStr(15)
'strState = arrStr(16)
strZip = arrStr(17)
strCountryCode = arrStr(18)
strItem = arrStr(3)
strQty = arrStr(4)
strPrice = arrStr(6)
'strFreight = arrStr(8)
strFreight = strFreight + arrStr(8)

'Read AR_Customer to find if Customer exists

retval = 0
name = ""
custExist = 0
ARDivisionNo = "00"

retval = oCust.nSetIndex("KNAME")
retval = oCust.nFind(strName)   ' this will fail
retval = oCust.nMoveNext()           ' go to next record
if Not(cBool(oCust.nEOF)) then
		retval = oCust.nGetValue("CustomerName$", name)
		if UCase(strName) = UCase(name) then
				custExist = 1
		end if
end if

If custExist = 1 then

' Create a new header record using the next available order number
OrderNo = ""
r = o.nGetNextSalesOrderNo(OrderNo)
r = o.nSetKey(OrderNo)
r = o.nSetValue("ARDivisionNo$", "00")
r = o.nSetValue("CustomerNo$", "0179854")
r = o.nSetValue("CustomerPONo$", "BUSOBJTST")

' Create a new line for the item and write to memory file

r = o.oLines.nAddLine()
r = o.oLines.nSetValue("ItemCode$", strItem)
r = o.oLines.nSetValue("WareHouseCode$", "000")
r = o.oLines.nSetValue("QuantityOrdered", strQty)
r = o.oLines.nSetValue("UnitPrice", strPrice)
r = o.oLines.nWrite()


' Write header and lines to disk
r = o.nSetValue("FreightAmt", strFreight)
r = o.nGetValue("NonTaxableAmt", nontax)
r = o.nSetValue("FreightAmt", freight)
r = o.nSetValue("DepositAmt",nontax+freight)
r = o.nWrite()

strFreight = 0


end if

loop

 

Doing it this way creates and order per line read... there are orders with 2 o more lines, therefore i am creating 2 or 3 orders instead of 1 order with 2 or 3 lines...

 

Any suggestions?

 

Regard,

Manuel

  • Hi Forum,

     

    I got an example of an script to create SO... i am not seeing anything different compared to what i already have.

    one thing that i noticed it that...:

     

    r = oLines.nAddLine()

     

    the r value is '0' - zero.... it doesn't make any sense.... i am setting the oLines by doing the following..

     

    ' Instantiate a Sales Order business object
    oSEC = oSS.nSetProgram(oSS.nLookupTask("SO_SalesOrder_ui"))
    Set o = oScript.NewObject("SO_SalesOrder_bus", oSS)
    oLines = 0
    Set oLines = oScript.NewObject("SO_SalesOrderDetail_bus", oSS)
    
    ' Instantiate a AR_Customer business object
    oSEC = oSS.nSetProgram(oSS.nLookupTask("AR_Customer_ui"))
    Set oCust = oScript.NewObject("AR_Customer_bus", oSS)

     Am i missing something here???

     

    Regards,

    Manuel Roman

     

  • manuel,

     

    You cannot instantiate the detail lines object by itself, you need to use the object handle within the header object.  The error message you are probably getting is that you have an invalid header, because you are trying to use the lines object standalone.

     

    Set oLines = o.oLines

     

    instead of how you are defining the lines object.

     

    Hope this helps

     

    Elliott

  • We just tried it and still doesn't work...

    We changed the script to use the o.oLines and still getting the message Item doesn't exist when in fact it is in the master.

     

    Regards,

    Manuel Roman

  • one more thing we are trying it in Mas200...

  • OK,

     

    i can create the order in  Mas200 and Mas90.... but  i am getting an error setting the QuantityOrdered and ItemPrice.

    Here is the code:

     

    ' Create a new line for the item and write to memory file
    
    
    Do while NOT objTextFileLines.AtEndOfStream
    arrStrLines = split(objTextFileLines.ReadLine,",")
    LinesOrderNo = arrStrLines (0)
    strItem =trim( arrStrLines (7))
    strQty = arrStrLines (8)
    strPrice = arrStrLines (10)
    strFreight = arrStrLines (12)
    'strFreight = strFreight + arrStrLines (12)
    
    if LinesOrderNo = strHdrOrderNo then
    
    msgbox "order numbers: " & LinesOrderNo & " " & strHdrOrderNo
    
    r = oLines.nAddLine()
    
    If r = 0 then
    msgbox("Error setting AddLine: " & r & " ..." & o.sLastErrorMsg)
    End if
    
    r = oLines.nSetValue("ItemCode$", strItem)
    
    If r = 0 then
    msgbox("Error setting Item Code: " & strItem & " ..." & o.sLastErrorMsg)
    End if
    
    r = oLines.nSetValue("WarehouseCode$", "000")
    
    If r = 0 then
    msgbox("Error setting Warehouse Code: " & r & " ..." & o.sLastErrorMsg)
    End if
    
    r = oLines.nSetValue("QuantityOrdered$", strQty)
    
    If r = 0 then
    msgbox("Error setting Qty Ordered: " & strQty & " ..." & o.sLastErrorMsg)
    End if
    
    r = oLines.nSetValue("UnitPrice$", strPrice)
    
    If r = 0 then
    msgbox("Error setting Item Price: " & strPrice & " ..." & o.sLastErrorMsg)
    End if
    
    r = oLines.nWrite()
    
    
    

     The values are coming in correctly but i cannot set them...Using '$' in the variable gives me the error, without it i don't get the rror but i cannot get the value...

     

    Like:

     

    r = oLines.nSetValue("QuantityOrdered$", strQty)

    gives me the error ..."Error Setting Qty Ordered:..."

     

     

    Regards,

    Manuel Roman

  • I may be wrong, but the "$" denotes a string variable.  QtyOrdered and Price are not strings, that is why it doesn't fail without the "$".  Make sure the variable you are setting is also set to integer and not string, otherwise it will always return 0.

     

    I think anyway.

  • still doesn't work without the $....

    all fields on detail are giving me an error...Item, whse, qty and price...

     

    Regards,

    Manuel Roman

  • You didn't remove the "$" from all the fields did you?  It still has to remain on the string fields.  Also, you are setting the variables to something before assigning them right?  In my experience if you don't declare a variable as an integer variable, it is always considered a string.

     

    strqty = 0
    strQty = arrStrLines (8)
  • no i just took the $ from string variables..

    i already set numeric varables to '0' ... still doesn't work.

     

    Regards,

    Manuel Roman

  • Ok, found the problem.... Actuly Alnoor found the problem....

    But i am still not able to set te qty ordered and unitprice.... here is the part of the script

     

    r = oLines.nAddLine()
    
    
    
    If r = 0 then
    
    msgbox("Error setting AddLine: " & r & " ..." & oLines.sLastErrorMsg)
    
    End if
    
    
    
    r = oLines.nSetValue("ItemCode$", strItem)
    
    
    
    If r = 0 then
    
    msgbox("Error setting Item Code: " & strItem & " ...  " & oLines.sLastErrorMsg)
    
    End if
    
    
    
    r = oLines.nSetValue("QuantityOrdered", strQty)
    
    msgbox("Error setting Qty Ordered: " & r & " ...  " & oLines.sLastErrorMsg)
    
    If r = 0 then
    
    msgbox("Error setting Qty Ordered: " & strQty & " ...  " & oLines.sLastErrorMsg)
    
    End if
    
    
    
    r = oLines.nSetValue("UnitPrice", strPrice)
    
    msgbox("Error setting Item Price: " & r & " ...  " & oLines.sLastErrorMsg)
    
    If r = 0 then
    
    msgbox("Error setting Item Price: " & strPrice & " ...  " & oLines.sLastErrorMsg)
    
    End if
    
    
    
    r = oLines.nSetValue("WarehouseCode$", "000")
    
    
    
    If r = 0 then
    
    msgbox("Error setting Warehouse Code: " & r & " ...  " & oLines.sLastErrorMsg)
    
    End if
    
    
    
    r = oLines.nWrite()

     Now, can someone take a look at the code and tell me what's wrong with it?... because i cannot see anything wrong...

     r is equal to 1 on both, the qtyordered and unitprice nSetValue lines....

     

    Regards,

    Manuel Roman