Crmgo Blog

Xdto, export to another document

Xdto, export to another document

Now let's try to answer a practical question: how to use the XDTO mechanism to transfer data from one configuration to another, where the configurations are different. It is important to understand that the schema "http://v8.1c.ru/8.1/data/enterprise/current-config" is suitable for exchanging data between identical configurations and allows performing conversion between ObjectXDTO and 1C Object. It is possible…

Xdto, export to another document

Now let's try to answer a practical question: how to use the XDTO mechanism to transfer data from one configuration to another, where the configurations are different.

It is important to understand that the schema "http://v8.1c.ru/8.1/data/enterprise/current-config" is suitable for data exchange between identical configurations and allows performing conversion between ObjectXDTO<->1C Object. You can use this!

Next, since we have different configurations, during the transfer we need to perform data transformation at some stage (export/import). This is usually done during the export stage - let's not deviate from tradition, moreover, this usually has several advantages.

1. To generate export data, we need the schema of the receiving configuration. Let's export it to a file: in the receiving configuration, right-click on the "XDTO Packages" node in the configuration tree / Export XML configuration data schema...

2. Let's start exporting. To generate export data, we create a factory based on the schema, specifying the path to the receiving configuration's schema file:

Code
XMLWriter = New XMLWriter; XMLWriter.OpenFile(FileName); Paths = New Array(); Paths.Add("C:\DestinatonConf.xsd"); Factory = CreateXDTOFactory(Paths);

3. Let's create a TypeXDTO type to hold data from the source configuration:

Code
ReceiptInvoiceType = Factory.Type("http://v8.1c.ru/8.1/data/enterprise/current-config", "DocumentObject.ReceiptInvoice");

Note that this TypeXDTO object maps the type of the receiving configuration, because our factory object instance works according to the schema loaded from the file, not according to the data of the current configuration.

4. Let's create an ObjectXDTO based on the type, and a 1C object from the source configuration:

Code
ReceiptInvoiceObject = Factory.Create(ReceiptInvoiceType); GoodsReceiptObject = GoodsReceipt.GetObject();

5. Fill the ObjectXDTO by performing necessary data transformations

Code
ReceiptInvoiceObject.Ref = GoodsReceiptObject.Link.UniqueIdentifier(); ReceiptInvoiceObject.DeletionMark = GoodsReceiptObject.MarkForDeletion; ReceiptInvoiceObject.Date = GoodsReceiptObject.Date; ReceiptInvoiceObject.Number = GoodsReceiptObject.Number; ReceiptInvoiceObject.Posted = GoodsReceiptObject.Posted; ReceiptInvoiceObject.Comment = GoodsReceiptObject.Comment; ReceiptInvoiceObject.ShortDocumentComposition = GoodsReceiptObject.ShortDocumentComposition; ReceiptInvoiceObject.Responsible = GoodsReceiptObject.Responsible.UniqueIdentifier(); ....

6. Write the object to a file:

Code
Factory.WriteXML(XMLWriter, ReceiptInvoiceObject); XMLWriter.Close();

Export is complete.

In the receiving configuration, the import code turns out to be quite simple:

Code
ReadingXML = New ReadXML; ReadingXML.OpenFile(FileName); IncomingInvoiceObject = XDTOSerializer.ReadXML(ReadingXML); IncomingInvoiceObject.DataExchange.Loading = True; IncomingInvoiceObject.Write(DocumentWriteMode.Write); ReadingXML.Close();

Moreover, if a 1C object with such an identifier already exists in the database at the time it is read from the file, the object returned by the ReadXML method will not be new (IsNew()=False), but it will contain the data obtained from the file; if there is no object with such an identifier, the object will be new (IsNew()=True), it will contain data from the file, and a reference to the new object will already be assigned to it.

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.

Categories: 1C