Coming soon: Your brand new Help Center & Community! Get a sneak-peek here

VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

Hello,

I want to set a specific document template to uncompleted/status 1

Because there is no function for this, I want to solve it by script

Therefore I created one but it does not work and I do not know if this depends on the event OnCurrentDocumentCreated. Maybe here it is not possible to change propertys for the CurrentDocument?

 

Here is my code - Thank you in advance!:

 

Public Sub OnCurrentDocumentCreated	
	
	
	if CurrentDocument.template.text = "Checkliste Ausschreibung neu" then 
	
		CurrentDocument.Description = "Dies ist ein Testdokument"
		CurrentDocument.Completed = false
		CurrentDocument.status = 1
		CurrentDocument.Save
		
		SOMessageBox CurrentDocument.template.text	
	end if
end sub




RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

Yes, in  "Created" and "Saved" messages, you cannot change fields.
This is guarded against in all entities.

Try exploiting the *FieldChanged, *FieldsChanged and/or *BeforeSave events.
Never call Save inside messages on "self".

/Conrad

Af: Conrad Weyns 2. mar 2017

RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

TY, now it works

here my solution - maybe someone can benefit

Dim bolNewFile
bolNewFile = false


Public Sub OnCurrentDocumentCreated	
	bolNewFile = true	
end sub

Public Sub OnCurrentDocumentBeforeSave()
	
	'### Skript nur beim Dokument vom Typ XXX ausführen
	if CurrentDocument.template.text = "Checkliste Ausschreibungen" then 
		if bolNewFile = true then				
			CurrentDocument.status = 1			
			bolNewFile = false
		end if	
	end if		
end sub
 
Af: Martin Loreck 3. mar 2017

RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

Why do you save?

Af: Martin Andersen 3. mar 2017

RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

I heard that I have to save. I did not tried without. Is this a problem?

Af: Martin Loreck 3. mar 2017

RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

You are in an event "BeforeSave" so you know that when your sub is done the record is going to be saved. I think it is a mistake, and could lead to unforseen mess, to save here. You should even cause a never ending loop (but I guess Conrad's code will stop the loop).

Af: Martin Andersen 3. mar 2017

RE: VB Scriptquestion - editing CurrentDocument in OnCurrentDocumentCreated Event

it is not Conrads code it is mine :)

the bool makes sure that there is no loop

but deleted the save line - I understand your point

Af: Martin Loreck 3. mar 2017