Is My Record Temporary?

Very often when developing we have logic we would like to execute on records, but only when the table is actually a real table and not when used as a temporary record. This could be things like inserting, modifying or deleting attached records in another table which would make no sense in most cases if the table you are currently in is temporary.

Now when we use a RecordRef we have a command that will return true or false based on the RecordRef in question is temporary or not. The command is called

RecordRef.ISTEMPORARY

However this command sadly doesn’t exist for Records.

So how can we get around that issue?

I have created a RecordRefLibrary codeunit that contains the following 2 functions among others

IsVariantTemporary(PassedVariant : Variant) : Boolean

Variant2RecRef(PassedVariant,RecRef);

EXIT(RecRef.ISTEMPORARY);

Variant2RecRef(PassedVariant : Variant;VAR RecRef : RecordRef)

CASE TRUE OF

PassedVariant.ISRECORD :

RecRef.GETTABLE(PassedVariant);

PassedVariant.ISRECORDREF :

RecRef := PassedVariant;

ELSE

ERROR(UnsupportedTypeErr);

END;

Now in each table where I need it I add the following function

IsTemporary() : Boolean

EXIT(RecordRefLibrary.IsVariantTemporary(Rec));

Now I can write

Record.IsTemporary

anywhere I use the Record and it will return if the record I am currently in is temporary or not.

I could of course also just call directly to the codeunit if I would like not to make changes to the Table, however it would be good practice in my view to expose the functionality on each table where needed, so everybody knows where to find it.

Hopefully this was helpful to some of you if you don’t have a solution for this already.

One thought on “Is My Record Temporary?

  1. Pingback: Is My Record Temporary (Part 2)? | Thinking NAV – Thinking Differently

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s