Update UDT from CI_Item

SOLVED

After reading a few post about updating UDT's from scripts I am wondering if my events are wrong or do I need to SetKey before SetValue?  In the debug I couldn't get any return values, so I am guessing my events are wrong, I've tried a PRE-WRITE and a POST-READ on the CI_Item. 

If oSession.CompanyCode = "ZZZ" then

sIC = ""
sUK = ""
sVENDOR=""
oUDT=0

retVal = oBusObj.GetValue("ItemCode$", sIC)
r=oScript.DebugPrint("IC: " & sIC)
retVal = oBusObj.GetValue("UDF_UNIQUEKEY$", sUK)
r=oScript.DebugPrint("UK: " & sUK)
retVal = oBusObj.GetValue("PrimaryVendorNo$", sVENDOR)
r=oScript.DebugPrint("VENDOR " & sVENDOR)

IF sVENDOR = "0001904" then

Set oUDT = oSession.AsObject(oSession.GetObject("CM_UDTMaint_bus","PO_UDT_ROG_IMPORT_ITEM_MAPPING"))

retVal = oUDT.SetValue("UDF_SANMAR_ITEMCODE$",sUK)
retVal = oUDT.SetValue("UDF_SAGE_ITEMCODE$",sIC)
retVal = oUDT.Write()

End If
End IfIf oSession.CompanyCode = "ZZZ" then

sIC = ""
sUK = ""
sVENDOR=""
oUDT=0

retVal = oBusObj.GetValue("ItemCode$", sIC)
r=oScript.DebugPrint("IC: " & sIC)
retVal = oBusObj.GetValue("UDF_UNIQUEKEY$", sUK)
r=oScript.DebugPrint("UK: " & sUK)
retVal = oBusObj.GetValue("PrimaryVendorNo$", sVENDOR)
r=oScript.DebugPrint("VENDOR " & sVENDOR)

IF sVENDOR = "0001904" then

Set oUDT = oSession.AsObject(oSession.GetObject("CM_UDTMaint_bus","PO_UDT_ROG_IMPORT_ITEM_MAPPING"))

retVal = oUDT.SetValue("UDF_SANMAR_ITEMCODE$",sUK)
retVal = oUDT.SetValue("UDF_SAGE_ITEMCODE$",sIC)
retVal = oUDT.Write()

End If
End If

  • 0

    I always wrap every new object in a permission check, to ensure it actually opens.

    oSequenceTable = oSession.GetObject("CM_UDTMaint_bus", "SO_UDT_SEQUENCE")
    if oSequenceTable <> 0 then
    	Set oSequenceTable = oSession.AsObject(oSequenceTable)
    else	
    	retVal = oSession.AsObject(oSession.UI).MessageBox("", "Access to SO_UDT_SEQUENCE is required for the ??? script to work.")
    	exit sub
    end if

  • 0

    Ok, so I am not getting to the UDT, I tried a POST-READ and POST-WRITE.  On the trace screen my last message was the sVENDOR value.  

    r=oScript.DebugPrint("VENDOR " & sVENDOR)
    
    	IF sVENDOR = "0001904" then
    
    	oUDT = oSession.GetObject("CM_UDTMaint_bus","PO_UDT_ROG_IMPORT_ITEM_MAPPING")
    	r=oScript.DebugPrint("After GetObject oUDT ")
    	
    		If oUDT <> 0 then
    		
    		Set oUDT = oSession.AsObject(oUDT)
    		else
    		
    		retVal = oSession.AsObject(oSession.UI).MessageBox("", "Access to PO_UDT_ROG_IMPORT_ITEM_MAPPING is required for the ??? script to work.")
    		r=oScript.DebugPrint("After AsObject oUDT ")		
    
    		exit sub

  • 0 in reply to jland47

    So you are getting the "Access to..." error when you try?

    If you add the UDT to the menu, the effective permission is in Role Maintenance... with an old bug related to UDT permissions in scripts, where the first UDT listed in Role Maintenance is checked (instead of the correct table).

  • 0

     

    Like all Sage 100 business objects you need to prepare the record for editing whether that is a new record or an existing record.  This is done through SetKey() or SetKeyValue()/SetKey().  It appears that you are not setting any keys in the example above.

    So for your oUDT object you need to SetKey( entire_concatenated_key ).  The return result will be 0 - error, 1 - existing record and ready for editing, 2 - new record and ready for editing.  I'm not sure what you defined as your key for the UDT.

    Hope this helps.

  • 0

    Been trying several different ways but only getting this far in the trace window on a column post validate and a post write event.

    So I am not getting to the message box, I should have all rights and under Roles all the UDT's are checked off.  Would the way a UDT is setup not allow for access through scripting?

    oUDT = oSession.GetObject("CM_UDTMaint_bus","PO_UDT_ROG_IMPORT_ITEM_MAPPING")
        r=oScript.DebugPrint("After GetObject oUDT ")
        
            If oUDT <> 0 then
            
            Set oUDT = oSession.AsObject(oUDT)
            else
            
            retVal = oSession.AsObject(oSession.UI).MessageBox("", "Access to PO_UDT_ROG_IMPORT_ITEM_MAPPING is required for the ??? script to work.")
            r=oScript.DebugPrint("After AsObject oUDT ")        
    
            exit sub
    
            retVal =oUDT.SetKeyValue("UDF_SANMAR_ITEMCODE$", sUK)
            r=oScript.DebugPrint(retVal)
    
                If retVal = 2 then
        
                'retVal = oUDT.SetValue("UDF_SANMAR_ITEMCODE$",sUK)
                retVal = oUDT.SetValue("UDF_SAGE_ITEMCODE$",sIC)
                retVal = oUDT.SetKey()

    Would I be better off just trying to update the fields in SQL instead of through BOI?

  • 0 in reply to jland47

    Where is your end if?  You only want to exit sub when you don't have UDT access. 

    (And change the MessageBox to a DebugPrint, in case it's causing an invisible pop-up on the server).

    Direct SQL edits are strongly discouraged for a few reasons.

  • 0 in reply to jland47

    The code should look like this:

    retVal = oUDT.SetKeyValue("UDF_SANMAR_ITEMCODE$", sUK) 
    retVal = oUDT.SetKey()

    If retVal = 2 then
           retVal = oUDT.SetValue("UDF_SAGE_ITEMCODE$",sIC) 

           retVal = oUDT.Write()

    End if

  • 0 in reply to Kevin M

    This is where I am at now, it was getting to the SetSession but thats as far as I got, I dont think this version is getting there though.  I keep on trying to place the exit sub in correct spot or remove it and nothing is correcting the issue.

    If oSession.CompanyCode = "ZZZ" then
    
    sIC = ""
    sUK = ""
    sVENDOR=""
    oUDT=0
    
    retVal = oBusObj.GetValue("ItemCode$", sIC)
    r=oScript.DebugPrint("IC: " & sIC)
    retVal = oBusObj.GetValue("UDF_UNIQUEKEY$", sUK)
    r=oScript.DebugPrint("UK: " & sUK)
    retVal = oBusObj.GetValue("PrimaryVendorNo$", sVENDOR)
    r=oScript.DebugPrint("VENDOR " & sVENDOR)
    
    	If sVENDOR = "0001904" then
    
    	oUDT = oSession.GetObject("CM_UDTMaint_bus","PO_UDT_ROG_IMPORT_ITEM_MAPPING")
    	r=oScript.DebugPrint("oUDT " & oUDT)
    
    	
    		If oUDT <> 0 then
    	
    		
    		Set oUDT = oSession.AsObject(oUDT)
    		r=oScript.DebugPrint("After SetSession oUDT ")
    		retVal = oUDT.SetKeyValue("UDF_SANMAR_ITEMCODE$", sUK)
    		retVal = oUDT.SetKey()
    
    			If retVal = 2 then
    				 retVal = oUDT.SetValue("UDF_SAGE_ITEMCODE$",sIC)
    
    				 retVal = oUDT.Write()
    
    			
    			End If
    		
    		End If
    		exit sub
    	End If
    End If

  • 0 in reply to jland47

    I set this 100% separate from all other script logic, at the top of a script, as a quick way to not waste time processing anything if the user doesn't have the required permissions.

    Add a debug print after this section of code "UDT Object created", so you know permissions are OK.  Don't try to embed object creation within other logic, which will just confuse matters.  I always do this at the top of scripts, (along with something like checking ItemType and exiting when not working with inventory items) before getting into the business logic.

  • 0 in reply to Kevin M

    Finally got something to work, after refering to a good UDT scripting post (https://communityhub.sage.com/us/sage100/f/business-object-interface/92592/scripting-with-user-defined-tables-udts/506751) and using the users on this post comments.