                                      metaOnly: false,
                                      objectType: MSObjectType.paymentin,
                                      collectionName: "payments")
-        dict["invoicesOut"] = serialize(entities: invoicesOut.flatMap { $0 as? MSDocument }.map { MSEntity<MSDocument>.entity($0) },
+        dict["invoicesOut"] = serialize(entities: invoicesOut.compactMap { $0 as? MSDocument }.map { MSEntity<MSDocument>.entity($0) },
                                         parent: self,
                                         metaOnly: true,
                                         objectType: MSObjectType.invoiceout,

        dict["shared"] = shared
        dict["applicable"] = applicable
        dict["state"] = serialize(entity: state, metaOnly: true)
        dict["attributes"] = attributes?.flatMap { $0.value() }.map { $0.dictionary(metaOnly: false) }
        if let agentAccount = agentAccount {
            dict["agentAccount"] = serialize(entity: agentAccount, metaOnly: true)
        }

                                           metaOnly: false,
                                           objectType: MSObjectType.purchaseorder,
                                           collectionName: "purchaseOrders")
        dict["demands"] = serialize(entities: demands.compactMap { $0 as? MSDocument }.map { MSEntity<MSDocument>.entity($0) },
                                    parent: self,
                                    metaOnly: true,
                                    objectType: MSObjectType.demand,
