Hi guys!
I have a code where I download a file from the Content Server to the File System in Oscript in a Workflow step.
In our case, we have an attachment in the workflow that is an Alias to a document in the Content Server, so we first get the ID of the original node. This code works for most cases, but, I don't know why, I'm getting an error in a couple workflows instances.
The error is "Erro ao buscar a versão de \'Fatura_REQ 5/94_2014 9 9\'" something like "Error fetching the version of \'Fatura_REQ 5/94_2014 9 9\'" in portuguese.
The error is ocurring in the line
checkVal = llNode.NodeFetchVersion( original, Undefined, fetchInfo )
I debugged the code, checked the ID of the original file that is determined from the alias and everything is ok. The file exists, I can check the versions, etc.
Does anybody know what can be the problem here?
The code is below
function Dynamic DownloadInvoice( \
Object prgCtx, \
WAPIWORK work, \
Integer workID, \
Integer subWorkID, \
Integer taskID, \
Integer returnSubWorkID, \
Integer returnTaskID, \
List info, \
Dynamic extraData = Undefined )
// locals
DAPINODE node
DAPINODE attachFolder
Assoc fetchInfo
Object llNode
Assoc checkVal
Integer index
String filename
String path = 'c:\SAP\'
Object dapiCtx = prgCtx.DSession()
Record r
Boolean found = False
Object obj = $WFMain.WFPackageSubsystem.GetItemByName( 'DAPIAttachments' )
RecArray array = $WFMain.WAPIPkg.LoadWorkData( prgCtx, work )
//
// off to get the object
//
if ( IsDefined( obj ) )
for r in array
if ( { r.TYPE, r.SUBTYPE } == { obj.fType, obj.fSubType } )
found = True
break
end
end
attachFolder = DAPI.GetNodeByID( prgCtx.DapiSess(), DAPI.BY_DATAID, r.USERDATA, TRUE )
if( !IsError( attachFolder ) )
//node = DAPI.GetNodeByID( dapiCtx.fSession, nodeID)
//if ( IsNotError( node ) )
Dynamic myList
myList=ListSubNodes(attachFolder)
if( length(myList) == 0 )
checkVal.OK = False
checkVal.ApiError = undefined
checkVal.ErrMsg = "Não existem documentos anexados ao fluxo de trabalho!"
else
Integer i
DAPINODE att
for att in myList
if ( IsDefined( Str.Locate(att.pName, "Fatura_") ) /*&& IsDefined( Str.Locate(att.pName, "- PO_") )*/ ) //MUDAR NO ES O NOME DO FICHEIRO CRIADO PARA SER IGUAL A PROD!
//
// O Objecto existe
//
DAPINODE original = DAPI.GetNodeByID( dapiCtx.fSession, att.pOriginalID )
filename = original.pName
if ( File.Exists( path + filename ) )
//
// Adicionar inteiros sucessivos ao nome até não existir um ficheiro com o mesmo nome
//
for index = 1 to 1000
checkVal = $WebDoc.WebDocUtils.GetFileNameParts( att.pName )
if ( Length( checkVal.extension ) > 0 )
//
// O ficheiro tem extensão
//
filename = Str.Format( "%1[%2].%3", checkVal.basename, index, checkVal.extension )
else
filename = Str.Format( "%1[%2]", checkVal.basename, index )
end
breakif ( !File.Exists( path + filename ) )
end
end
//
// O FilePath tem que incluir o nome
//
fetchInfo.FilePath = path + Str.ReplaceAll(filename, ' ', '_') + ".pdf"
llNode = $LLIAPI.LLNodeSubsystem.GetItem ( original.pSubtype )
checkVal = llNode.NodeFetchVersion( original, Undefined, fetchInfo )
if ( checkVal.ok == TRUE )
//
// Success in the download. Carry on
//
//echo( "Successful: ", fetchInfo.FilePath )
checkVal.OK = true
checkVal.Path = Str.ReplaceAll(filename, ' ', '_') + ".pdf"
else
echo( "Error: ", checkVal.errMsg )
end
end
end
end
//else
// echo( "Error in retrieving the object: ", nodeID )
//end
else
checkVal.OK = False
checkVal.ApiError = attachFolder
checkVal.ErrMsg = [WFMain_ErrMsg.CouldNotLocateTheAttachmentsFolder]
end
end
return checkVal
end
Function List ListSubNodes(\
DAPINODE node,\
Boolean autoExpand = False )
List retList
DAPINODE tmpNode
DAPI.RefreshNode( node )
if ( DAPI.IsAliasNode( node ) || DAPI.IsGenerationNode( node ) )
tmpNode = DAPI.GetOriginalNode( node )
else
tmpNode = node
end
if ( !IsError( tmpNode ) )
retList = DAPI.ListSubNodes( tmpNode, '', '', '', autoExpand, 0 )
if ( IsError( retList ) )
retList = {}
end
end
return( retList )
end