Does File still save, if there is an error in an BeforeSave triggered rule?

Does File still save, if there is an error in an BeforeSave triggered rule?

d_eckel
Contributor Contributor
660 Views
8 Replies
Message 1 of 9

Does File still save, if there is an error in an BeforeSave triggered rule?

d_eckel
Contributor
Contributor

Hi,

I have a fast question.We have some issues with saving files in the last couple of weeks and I just want to make sure, none of the iLogic Rules is the problem.

Does a File (part, assembly, drawing) still save, if there is an error in an BeforeSave triggered rule?

 

Thanks

0 Likes
661 Views
8 Replies
Replies (8)
Message 2 of 9

Frederick_Law
Mentor
Mentor

It may not save.

The error could stop and error out before it get to save.

0 Likes
Message 3 of 9

Michael.Navara
Advisor
Advisor

In my opinion the document is NOT saved when before save rule raises exception.

You can easily test this with this simple rule.

Add this rule "before save" and look what happens 😀

Logger.Debug(ThisDoc.Document.FileSaveCounter)
If MsgBox("Raise exception?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
	Throw New ApplicationException("This is expected exception")
End If

 

0 Likes
Message 4 of 9

SebastienOuellet
Participant
Participant

it may not save but you can add a Try Catch between your rules....that way it will not stop the next action.

 

Try 
  'INSERT YOUR CODE HERE
Catch
  'INSERT THE WAY YOU WANT TO BE NOTIFIED OF THE ERROR..IF NEEDED
End Try

 

 



Logo MCMCAD
https://mcmcad.com/
0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

Hi @d_eckel.  In addition to what Michael said, keep in mind the rule running order.  If your other rule is already listed under the before save event, you would want his rule to be listed below that other rule under that event, so that it will be the second (or next) one to run when that event happens, after your regular rule.  When that event happens, it will run the rules listed under that event in the order they are listed in the Event Triggers dialog.  Not sure which one would run first if one rule is on the All Documents tab, and another rule is on the This Document tab, under the same event though.

Edit:  Actually, you might want that 'checker' rule to be under the 'after save' event.  That way, if it does not show anything, it did not save.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

Frederick_Law
Mentor
Mentor

The fun of using event trigger.

0 Likes
Message 7 of 9

Curtis_Waguespack
Consultant
Consultant

Hi @d_eckel 

 

You could add a save line to a Finally statement as shown here. 

 

Keep in mind, a Finally statement always runs.

 

In this example a parameter that does not exist called foo is being set, so an error is encountered.

 

The Finally statement checks to see if the save counter has incremented, and if it has not then it saves the document.

 

Sub main

	MsgBox(ThisDoc.Document.FileSaveCounter, , "iLogic")

	SAVEcount = ThisDoc.Document.FileSaveCounter

	Try
		Call My_iLogic_Code
	Catch
		Logger.Info("there was an error")
	Finally
		If SAVEcount = ThisDoc.Document.FileSaveCounter Then ThisDoc.Save
	End Try

	MsgBox(ThisDoc.Document.FileSaveCounter, , "iLogic")

End Sub

Sub My_iLogic_Code
	
	'here is all of your rule code:

	Parameter("foo") = "foo-who"

End Sub

 

EESignature

Message 8 of 9

Michael.Navara
Advisor
Advisor

Sorry @Curtis_Waguespack your code doesn't work. Avoid to call Document.Save from OnSave event handler.

 

Dim fileSaveCounter = ThisDoc.Document.FileSaveCounter

Try
	Logger.Debug(fileSaveCounter)
	Throw New ApplicationException("Expected exception")
Catch ex As Exception
	Logger.Error(ex.Message)
Finally
	If fileSaveCounter = ThisDoc.Document.FileSaveCounter Then
		ThisDoc.Save()
	End If
End Try

 

It ends with another unhandled exception in better case. In the worst case you get StackOverflowException when someone doesn't handle this recursive call.

Problems encountered while saving the document.
	Access is denied. 
	The database in  could not be saved

 

 

 

Message 9 of 9

Curtis_Waguespack
Consultant
Consultant

@Michael.Navara wrote:

Sorry @Curtis_Waguespack your code doesn't work. Avoid to call Document.Save from OnSave event handler.

 


Hi @Michael.Navara,

 

Oooops! You're exactly right, a save call will not work with the Before Save trigger.

 

I forgot to test this with the trigger when posting and didn't catch that the file can not be saved during that event (because the file is already being saved, thus the database conflict).

 

Sorry for the confusion, and thanks for catching this.

 

 

EESignature

0 Likes