Copy one document to another and modifying copied Document is getting updated in Original document also

I have to copy one document to another document in a flow service and modify only the copied document, but after modifying the copied document the changes are getting reflected in original docuemnt as well. Please let me know how to sort this out?

Mapping from one string to another string in flow is copy-by-value, but mapping from one document to another is copy-by-reference. You will end up with 2 documents that look at the same memory space for their contents.

The pub.utils:deepClone service will make a complete copy-by-value of a document and all its sub-fields.

1 Like

Hi Vignesh,

can you share an outline of your Flow service please?
Specially the step where the mapping occurs.

Hopefully you have a DocType for these documents which can be used to create DocReferences in the pipeline.
If so, create a reference for the input and a reference for the (temporary?) output.
You can then map between these two references.
When needed, you can then map the temporary document back to the original name.


1 Like

pub.utils:deepClone service worked for me.I will try out the document reference option as well.
I will be dealing with Large document in this, could you please suggest which option would perform better?

Be aware of the warning that is in deepClone – it indicates that it is “expensive.” What that really means in terms of memory, CPU usage, etc. is not clear.

I’m curious about the need to copy a document in its entirety. Can you share a bit about the process you’re implementing?

1 Like

Creating Doc Type reference and updating the reference is not working out, It’s still updating the copied reference and the original doc. Please find snapshot below.
I have copied input doc to 2 reference doc types and updating one of the reference doctype. It’s still updating all three docs

There are two technical solutions as suggested in the above replies.

  1. Deep clone
  2. Map between the documents field by field

I don’t think any other technical solution available. So, we have to avoid copying whole documents by designing the flow logic. We need to understand your requirement for designing this solution. Let us know the why you have to copy the original document to another two different documents.

@reamon Do you have any suggestion, out of two suggested options, which is less expensive?

1 Like

I had initially mentioned this in my draft reply but then removed it since the copy-by-reference and copy-by-value item had been brought up by @erossing.26610 and covers why. When doc A is mapped to doc B, they both reference the same object. When string X is mapped to string Y, a new object is created which is a copy of the object at X and Y refers to the new copy.

The creation of a doc type and using document references does not impact this at all. A doc type and doc reference are design-time aspects only – they define what fields are expected and provide the way to map known fields. They have no impact at all at run-time.

Indeed, you can take some random document variable obtained in some way, map it to a variable that has a doc reference – and the variable will look like the source doc, not the doc type. And the run-time will not complain. This is the nature of weak-typing, which is a pro (and a con for some things) of FLOW development.

With the info at this point (want to copy a doc) the 2 options of mapping at the field level or using clone are the only options I can think of. If additional information about what the higher level task is that is prompting the thought that copying the doc is needed, perhaps another option will come to mind.

Do not worry too much about “expensive” – I noted it only because the SAG docs mention it. And that if testing/time measurement indicates the speed of the solution is slow enough that it matters, that might be a place to start looking for speed gains. But do not optimize prematurely. Only if testing shows the speed matters, and what specific steps are where the time is spent, that optimization should be done. Otherwise, choose the option that addresses the “-ilities” – readability, maintainability, etc.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.