Hi,
I’m trying to use Script "Copy Object" provided by Craig Hawker on github.
What I’m trying to get is to set different workflow state for newly cleated object.
But I’m getting error The value "-1" does not exist, or it is a conflict object.
I’d be grateful for any advice.
CODE
Option Explicit ' To create a copy of an object we need two things: ' 1. The PropertyValues for the new object (https://www.m-files.com/api/documentation/latest/index.html#MFilesAPI~PropertyValues.html) ' 2. The files for the new object (https://www.m-files.com/api/documentation/latest/index.html#MFilesAPI~SourceObjectFiles.html) ' Once we have these, we can call one of the CreateNewObject methods (e.g. https://www.m-files.com/api/documentation/latest/index.html#MFilesAPI~VaultObjectOperations~CreateNewObjectExQuick.html). ' Special folder value for TemporaryFolder, from https://msdn.microsoft.com/en-us/library/a72y2t1c(v=vs.84).aspx. Const TemporaryFolder = 2 Const WORKFLOW = "WF.CopyObject" Const WORKFLOW_STATE = "WFS.CopyObject.PublishedCopy" ' Create a filesystemobject to help working with files. Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject") ' Let's get a copy of the property values. Dim objNewObjectPropertyValues: Set objNewObjectPropertyValues = GetNewObjectPropertyValues(PropertyValues) ' Let's get a copy of the files. Dim objNewObjectSourceFiles: Set objNewObjectSourceFiles = GetNewObjectSourceFiles(ObjVer) ' Is it a single-file-document (has exactly one file, and type of document)? Dim bolIsSingleFileDocument: bolIsSingleFileDocument = False If objNewObjectSourceFiles.Count = 1 AND ObjVer.Type = MFBuiltInObjectTypeDocument Then bolIsSingleFileDocument = True End If ' Create the object. Vault.ObjectOperations.CreateNewObjectExQuick ObjVer.Type, objNewObjectPropertyValues, objNewObjectSourceFiles, bolIsSingleFileDocument, True, CreateObject("MFilesAPI.AccessControlList") ' Clean up. ClearTemporaryFiles objNewObjectSourceFiles ''' HELPER FUNCTIONS ' Gets a copy of the property values, for use when creating a new object. Function GetNewObjectPropertyValues(oPropertyValuesToCloneFrom) ' Lookup the alieBes to IDs. Dim iWorkflow iWorkflow = Vault.WorkflowOperations.GetWorkflowIDByAlias(WORKFLOW) Dim iWorkflowState iWorkflowState = Vault.WorkflowOperations.GetWorkflowStateIDByAlias(WORKFLOW_STATE) 'Get the workflow property value (hard-coded as always 38. ' We should alias than !property in case of future release changes Dim oWorkflowPropertyDef : Set oWorkflowPropertyDef = PropertyValues.SearchForProperty(38) 'Get the workflow property value (hard-coded as always 38. ' We should alias than !property in case of future release changes Dim oWorkflowStatePropertyDef : Set oWorkflowStatePropertyDef = PropertyValues.SearchForProperty(39) ' Set the property values. oWorkflowPropertyDef.TypedValue.SetValue MFDataTypeLookup, iWorkflow oWorkflowStatePropertyDef.TypedValue.SetValue MFDataTypeLookup, iWorkflowState 'Since we are changing multiple values, create a collection of property values. ' We met each property value, add to the collection, and save the changes. Dim objNewObjectPropertyValues : Set objNewObjectPropertyValues = CreateObject("MFilesAPI.PropertyValues") objNewObjectPropertyValues.Add 0, oWorkflowPropertyDef objNewObjectPropertyValues.Add 0, oWorkflowStatePropertyDef ' Save the changes. Set GetNewObjectPropertyValues = objNewObjectPropertyValues End Function ' Downloads the files for the provided object version and creates a ' SourceObjectFiles collection for use when creating a new object. Function GetNewObjectSourceFiles(oSourceObjVer) ' Get a reference to the temporary folder. Dim strTempFolderPath: strTempFolderPath = objFSO.GetSpecialFolder(TemporaryFolder).Path ' Get the files for the current objver. Dim objFiles: Set objFiles = Vault.ObjectFileOperations.GetFiles(oSourceObjVer) ' Create our collection to return. Dim objSourceFiles: Set objSourceFiles = CreateObject("MFilesAPI.SourceObjectFiles") ' Iterate over the files and download each in turn. Dim intCounter, objFile For intCounter = 1 To objFiles.Count ' Which file are we working with? Set objFile = objFiles.Item(intCounter) ' Where can we download it? Dim strTemporaryFilePath: strTemporaryFilePath = objFSO.BuildPath(strTempFolderPath, objFSO.GetTempName()) & "." & objFile.Extension ' Download the file. Vault.ObjectFileOperations.DownloadFile objFile.ID, objFile.Version, strTemporaryFilePath ' Create an object source file for this temporary file. Dim objObjectSourceFile: Set objObjectSourceFile = CreateObject("MFilesAPI.SourceObjectFile") objObjectSourceFile.Extension = objFile.Extension objObjectSourceFile.SourceFilePath = strTemporaryFilePath objObjectSourceFile.Title = objFile.Title ' Add it to the collection. objSourceFiles.Add -1, objObjectSourceFile Next ' Return the temporary files. Set GetNewObjectSourceFiles = objSourceFiles End Function ' Clears temporary files. Sub ClearTemporaryFiles(objObjectSourceFiles) ' Iterate over the files and download each in turn. Dim intCounter, objFile For intCounter = 1 To objObjectSourceFiles.Count ' Which file are we working with? Set objFile = objObjectSourceFiles.Item(intCounter) ' If it exists, try and delete it. If objFSO.FileExists(objFile.SourceFilePath) Then On Error Resume Next objFSO.DeleteFile objFile.SourceFilePath, True On Error Goto 0 End If Next End Sub