How do I copy CustomFields from one company to another?

lock
push_pin
done
Besvarat
2

Hello everyone. 

I want to copy all custom fields from one company to another, but I'm having a hard time understanding how the behind the scenes-logic work.

I'm looking for someone who can help me understand it :)

 

The following code doesn't copy anything. Why?

#setLanguageLevel 4;

NSContactAgent ca;
Integer origId = 8590;
Integer newId = 8603;

NSContactEntity origComp = ca.GetContactEntity(origId);
NSContactEntity newComp = ca.GetContactEntity(newId);

Map origUdef = origComp.GetCustomFields();

newComp.SetCustomFields(origUdef);
ca.SaveContactEntity(newComp);

 

Adding the following line updates 'SuperOffice:18', but only 'SuperOffice:18'

Map origUdef = origComp.GetCustomFields();
origUdef.insert("SuperOffice:18", "261"); //THIS LINE

newComp.SetCustomFields(origUdef);
ca.SaveContactEntity(newComp);

..but not if I write it like this:

Map origUdef = origComp.GetCustomFields();
origUdef.insert("SuperOffice:18", "[I:261]");

 

And here comes the kicker. If I alter the previous code-snippet to the following, suddently '[I:261]' works again.

Map origUdef; 
origUdef.insert("SuperOffice:18", "[I:261]");

 

I tried something like this, but this doesn't work either:

Map origUdef = origComp.GetCustomFields();
Map newUdef;
for(origUdef.first(); !origUdef.eof(); origUdef.next()) {
    newUdef.insert(origUdef.getKey(), decodeDBValue(origUdef.getVal()));
}

newComp.SetCustomFields(origUdef);
ca.SaveContactEntity(newComp);

 

 

 

 

 

28. feb. 2025 | 09:46 fm

Allt Svar (2)

This should work:

#setLanguageLevel 4;

NSContactAgent contactAgent;
NSUserDefinedFieldInfoAgent udfAgent;

Integer sourceContactId = 1;
Integer destinationContactId = 2;

NSContactEntity sourceContactEntity = contactAgent.GetContactEntity(sourceContactId);
Map sourceContactEntityCustomFields = sourceContactEntity.GetCustomFields();

NSContactEntity destinationContactEntity = contactAgent.GetContactEntity(destinationContactId);
Map destinationContactEntityCustomFields = destinationContactEntity.GetCustomFields();

foreach (NSFieldInfoBase contactCustomField in udfAgent.GetCustomFieldInfoList("contact", false))
{
    String contactCustomFieldProgId = contactCustomField.GetFieldName();

    String sourceContactEntityCustomFieldValue = decodeDBValue(sourceContactEntityCustomFields.get(contactCustomFieldProgId));
        
    destinationContactEntityCustomFields.insert(contactCustomFieldProgId, sourceContactEntityCustomFieldValue);          
}

destinationContactEntity.SetCustomFields(destinationContactEntityCustomFields);
destinationContactEntity = contactAgent.SaveContactEntity(destinationContactEntity);
28. feb. 2025 | 10:01 fm
That seems to work.
But I'm not still sure why my original code-snippet doesn't work.
28. feb. 2025 | 10:23 fm

Lägg till svar