Episode 1. Working with Metadata Objects
1C:Enterprise is an object-oriented platform, and it uses the object-oriented script language.
The 1C:Enterprise application is, basically, a huge bunch of objects involved in complex relationships and interactions with each other. Some of these objects are created by the Platform, others - by 1C:Enterprise developers like you and me.
The first thing that happens when you start the 1C:Enterprise application is that the Platform creates a single instance of an object called the Global Context. Let me show you around this vital part of the Platform. This object provides us with essential properties and methods we can use from anywhere in the source code. So let’s take a look at what’s in store for us.
First of all, the Global Context contains a set of Metadata Object managers. These are the proxies we use to work with all the Metadata Objects we create. All their properties, methods and events are listed right here - in the Syntax Assistant.
This is our Global Context and here are all the objects living in its properties. For example, this Catalogs guy here is the object we call when we need to access one of our catalogs.
Not all of these objects are included in the Metadata. Some of them are just predefined system objects we can use in the code but cannot modify in any way. For example, here we’ve got CollaborationSystem for messaging and stuff, CryptoTools for something sensitive, DataHistory to bring back old memories of the data long gone, and many many other fascinating things.
Right below these properties is a bunch of folders with all kinds of general-purpose methods the Global Context keeps handy for us. If I were you, I would make some time to browse through this list, for there is a lot of goodies here. We’ll be looking at some of them later in this course.
The last part of the Global Context lives all the way down here. It’s called system value sets and enumerations and contains predefined lists, like the list of fonts, or colors, or all possible document write modes and such.
So, this is the Global Context. What else do we have? To call the Global Context from the source code, we need a script language engine that understands and executes commands as long as they follow a specific syntax. It can do a lot of things for us, but for now, let’s just focus on how it works with the Metadata Objects.
The 1C:Enterprise script engine knows how to call the Metadata Object managers living inside of the Global Context. Despite being object-oriented, the 1C:Enterprise script has no class declaration syntax. So I cannot write anything like this. What I can do though is to create a new Metadata Object, which is exactly the same thing because Metadata Objects are nothing less than classes for data objects.
Now I can call this Global Context Catalogs property and see my new Catalog inside of it. This guy is called a Catalog Manager, and most of its methods are object factories, meaning that we use them to create or obtain specific data objects. Let me show you.
I’m adding a dot, and here is a whole bunch of methods. This one, for example, creates a new catalog item - data object belonging to this Catalog1 class. After it’s created, I can fill out its properties and write it down to the database just like this.
The same catalog manager can also read specific catalog items from the database for example, using this FindByCode method. The important thing to note here is that this kind of reading methods always return references rather than objects. So, if we need to change an item, we need to call this reference method, and it will read and return the entire object. Only after this we can change its properties and write it back to the database.
It’s pretty much the same deal with all the Metadata Objects. They all have Metadata Object managers that know how to spawn data objects belonging to this specific class. All these managers live in the Global Context. We have all the Documents, Catalogs, Registers, etc. right here in this list.
We use these Metadata Objects manager’s methods to create (or read from the database) the data objects. These data objects we create are not the part of the Global Context, of course. They live in the local context of the procedure they were created in.
Usually, there are several data object classes belonging to the same Metadata Object. They all are listed down here in the Applied objects section of the Syntax Assistant. This one, for example, is the catalog manager - the one we just used to find the catalog item in this code. It knows how to create other types of Catalog-related data objects.
For example, it can create a new empty catalog item for us to fill up and write down. It also knows how to read the catalog item reference knowing its code. This is the method we just used right here.
This method returns a CatalogRef class object - the guy living in the same Syntax Assistant neighborhood as you can see. This is what we have assigned to this variable right here. This catalog reference object knows how to retrieve the entire catalog item with this GetObject method.
The method returns the catalog object - another neighbor of ours. And we put this one into this variable here. This guy in its turn gives us full access to all catalog item’s attributes, so we can now fill out the object as we like.
It also has this Write method to save the catalog item to the database, which is exactly what we did in the next line.
So, this is how we work with the Metadata Objects in 1C:Enterprise script.