Import contacts - truncate too long fields

Tomasz Czado asked on May 25, 2017 10:14

Hi,

I'm using Contact Managment to import contacts from csv file. The problem is that Job title allows text not longer then 50 characters and Business phone allows text not longet then 26 characters. Module is not customizable so I can't update fields length. So I decided to use global events to truncate some texts. Code is below. It works when adding new contact via CMS, but ContactInfo.TYPEINFO.Events.Insert.Before event doesn't work when I use import from csv. Which event should I use to catch it?

using CMS.Base;
using CMS.ContactManagement;
using CMS.DataEngine;

[BlueModusCustomEventHandlers]
public partial class CMSModuleLoader
{
    private class BlueModusCustomEventHandlers : CMSLoaderAttribute
    {
        public override void Init()
        {
            ContactInfo.TYPEINFO.Events.Insert.Before += ContactInfoInsertOnBefore;
            ContactInfo.TYPEINFO.Events.Update.Before += ContactInfoUpdateOnBefore;
        }

        private void ContactInfoInsertOnBefore(object sender, ObjectEventArgs objectEventArgs)
        {
            if (objectEventArgs.Object != null)
            {
                ContactInfo contactInfo = (ContactInfo)objectEventArgs.Object;
                if (contactInfo != null)
                {
                    UpdateContactDetails(contactInfo);
                }
            }
        }

        private void ContactInfoUpdateOnBefore(object sender, ObjectEventArgs objectEventArgs)
        {
            if (objectEventArgs.Object != null)
            {
                ContactInfo contactInfo = (ContactInfo)objectEventArgs.Object;
                if (contactInfo != null)
                {
                    UpdateContactDetails(contactInfo);
                }
            }
        }

        private void UpdateContactDetails(ContactInfo contactInfo)
        {
            if (contactInfo.ContactBusinessPhone.Length > 50)
            {
                contactInfo.ContactBusinessPhone = contactInfo.ContactBusinessPhone.Substring(0, 50);
            }

            if (contactInfo.ContactJobTitle.Length > 26)
            {
                contactInfo.ContactJobTitle = contactInfo.ContactJobTitle.Substring(0, 26);
            }
        }
    }
}

Recent Answers


Prashant Verma answered on May 25, 2017 12:18

Hi Tomasz,

You can check with global events.

1.ImportExportEvents

Namespace: CMS.CMSImportExport

Events: ImportObject, ImportObjects Description document

2.ObjectEvents

Namespace: CMS.DataEngine

Events: Insert , BulkInsert Description document

Thanks

Happy to help you

1 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on May 25, 2017 13:33 (last edited on May 25, 2017 13:34)

Thank you for your replay. Unfortunatelly none of below global events works.

ContactInfo.TYPEINFO.Events.Insert.Before += ContactInfoInsertOnBefore;
ContactInfo.TYPEINFO.Events.Update.Before += ContactInfoUpdateOnBefore;
ImportExportEvents.Import.Before += ImportOnBefore;
ImportExportEvents.ImportObject.Before += ImportObjectOnBefore;
ImportExportEvents.ImportObjects.Before += ImportObjectsOnBefore;
ObjectEvents.Insert.Before += InsertOnBefore;
ObjectEvents.BulkInsert.Before += BulkInsertOnBefore;
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.