The following chart shows the interaction between 1C:Enterprise script objects used for operations with catalogs (fig. 29.3).
Fig. 29.3. 1C:Enterprise script objects used for operations with catalogs
Note. The yellow box indicates the data manipulation object. The object method from which the arrow originates is marked with the respective number in the listing (for example, number 3 stands for the FindByCode() method of the CatalogManager.<name> object), while the target object of the arrow is the type of returned object (for example, CatalogRef.<name>).
Learn more! For details on major types of 1C:Enterprise script objects, see section 1C:Enterprise script objects used for operations with applied data.
For examples of operations with catalogs that involve 1C:Enterprise script objects, see listing 29.5.
Listing 29.5. Object usage examples
1. // Global context // Catalogs // Example: displaying all types of references to catalog items available in the configuration. Array = Catalogs.AllRefsType().Types(); For Each NextType In Array Do Message(NextType); EndDo; 2. // CatalogsManager object // .<catalog name> // [<catalog name>] // For Each … In … Do … EndDo; // Example: creating a folder in the MaterialsAndServices catalog. NewFolder = Catalogs.MaterialsAndServices.CreateFolder(); NewFolder.Description = "My new folder"; NewFolder.Write(); // Example: getting a reference to the MaterialsAndServices catalog. Catalogs["MaterialsAndServices"].GetRef(); 3. // CatalogsManager.<catalog name> object // FindByCode() // FindByDescription() // FindByAttribute() // EmptyRef() // GetRef() // .<predefined catalog item name> // Example: checking whether a MaterialsAndServices catalog item with code 13 // is marked for deletion. If Catalogs.MaterialsAndServices.FindByCode(13).DeletionMark Then Message("Item with code 13 is marked for deletion."); EndIf; // Example: checking whether a MaterialsAndServices catalog item named "Services" // is a folder. If Catalogs.MaterialsAndServices.FindByDescription("Services", True).IsFolder Then Message("The item named Services is a folder."); EndIf; // Example: checking whether all catalog items have a type (material or service). EmptyEnumRef = Enums.MaterialServiceTypes.EmptyRef(); If Not Catalogs.MaterialsAndServices.FindByAttribute("ProductType", EmptyEnumRef).IsEmpty() Then Message("Items with no type assigned are found."); EndIf; // Example: passing an empty reference to a method parameter. Selection = Catalogs.MaterialsAndServices.Select(Catalogs.MaterialsAndServices.EmptyRef()); 4. // CatalogManager.<catalog name> object // Select() // SelectHierarchically() // Example: displaying the list of items located in the root folder of the catalog. Selection = Catalogs.MaterialsAndServices.Select(Catalogs.MaterialsAndServices.EmptyRef()); While Selection.Next() Do If Not Selection.IsFolder Then Message(Selection); EndIf; EndDo; // Example: deleting all items from a hierarchical catalog. Selection = Catalogs.MaterialsAndServices.SelectHierarchically(); While Selection.Next() Do Selection.Delete(); EndDo; 5. // CatalogManager.<catalog name> object // CreateFolder() // CreateItem() // Example: creating an item in the Employees catalog. NewItem = Catalogs.Employees.CreateItem(); NewItem.Description = "Andrew A. Smith"; // Filling the PastEmployment tabular section. NewTableRow = NewItem.PastEmployment.Add(); NewTableRow.Company = "NTC Corp."; NewTableRow.StartDate = Date(2013,05,01); NewTableRow.EndDate = Date(2013,12,31); NewTableRow.JobTitle = "Programmer"; NewItem.Write(); 6. // CatalogObject.<catalog name> and CatalogRef.<catalog name> objects // Owner // Parent // Ref // Example: prohibiting changes to subordinate items in the catalog item form module // if the ProhibitChanges property of the owner item is True. Procedure BeforeWrite(Cancel) If Owner.ProhibitChanges Then Cancel = True; EndIf; EndProcedure 7. // CatalogRef.<catalog name> // GetObject() // CatalogObject.<catalog name> // Copy() // Example: changing catalog item name. Item = Catalogs.MaterialsAndServices.FindByCode(10).GetObject(); Item.Description = "My new description"; Item.Write(); // Example: filling a catalog with test data. Item = Catalogs.MaterialsAndServices.CreateItem(); Item.Description = "Test item"; Item.Write(); For Z = 1 To 1000 Do NewItem = Item.Copy(); NewItem.Write(); EndDo; 8. // CatalogSelection.<catalog name> object // Ref // Example: filling the tabular section of a GoodsReceipt document // with all the items from the specified MaterialsAndServices catalog folder. Selection = Catalogs.MaterialsAndServices.SelectHierarchically(TextBox1); While Selection.Next() Do MaterialOrServiceReference = Selection.Ref; If MaterialOrServiceReference.IsFolder Then Continue; EndIf; NewRow = Materials.Add(); NewRow.Material = MaterialOrServiceReference; EndDo; 9. // CatalogSelection.<catalog name> object // GetObject() // Example: marking all items of a nonhierarchical catalog for deletion. Selection = Catalogs.Customers.Select(); While Selection.Next() Do Selection.GetObject().SetDeletionMark(True); EndDo; Next page: Event sequence for writing catalog items from item forms (save and close)