Skip to main content
Code formatting
Source Link
Surya Narayan
  • 1.5k
  • 10
  • 20
    public bool SaveContactData(string firstName, string lastName, string identifierSource, string email, string phoneNumber, string language, string userAgent)
        {
            try
            {
                var contactReference = GetIdentifiedContactReference(identifierSource, email);

                using (var client = SitecoreXConnectClientConfiguration.GetClient())
                {
                    var contact = client.Get(contactReference, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, PhoneNumberList.DefaultFacetKey));

                   //CONTACT IS COMING NULL 



                }
                return true;
            }
            catch (Exception ex)
            {
                Log.Error("Error saving data to profile", ex, this);
                return false;
            }
        }

 private IdentifiedContactReference GetIdentifiedContactReference(string identifierSource, string identifier)
        {
            if (Tracker.Enabled && !Tracker.Current.IsActive)
            {
                Tracker.StartTracking();
            }

            if (Tracker.Current?.Contact == null)
            {
                Log.Warn("Tracker.Current?.Contact == null", this);
                return null;
            }

            if (Tracker.Current.Contact.IsNew)
            {
                Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                _contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
                IdentifyContact(identifierSource, identifier);
                return new IdentifiedContactReference(identifierSource, identifier);

            }

            var id = Tracker.Current.Contact.Identifiers.FirstOrDefault();
            return Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0
                ? new IdentifiedContactReference(identifierSource, identifier)
                : new IdentifiedContactReference(id?.Source, id?.Identifier);
        }
    public bool SaveContactData(string firstName,string lastName, string identifierSource, string email,string phoneNumber, string language, string userAgent)
        {
            try
            {
                var contactReference = GetIdentifiedContactReference(identifierSource, email);

                using (var client = SitecoreXConnectClientConfiguration.GetClient())
                {
                    var contact = client.Get(contactReference, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, PhoneNumberList.DefaultFacetKey));

                   //CONTACT IS COMING NULL 



                }
                return true;
            }
            catch (Exception ex)
            {
                Log.Error("Error saving data to profile", ex, this);
                return false;
            }
        }

 private IdentifiedContactReference GetIdentifiedContactReference(string identifierSource, string identifier)
        {
            if (Tracker.Enabled && !Tracker.Current.IsActive)
            {
                Tracker.StartTracking();
            }

            if (Tracker.Current?.Contact == null)
            {
                Log.Warn("Tracker.Current?.Contact == null", this);
                return null;
            }

            if (Tracker.Current.Contact.IsNew)
            {
                Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                _contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
                IdentifyContact(identifierSource, identifier);
                return new IdentifiedContactReference(identifierSource, identifier);

            }

            var id = Tracker.Current.Contact.Identifiers.FirstOrDefault();
            return Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0
                ? new IdentifiedContactReference(identifierSource, identifier)
                : new IdentifiedContactReference(id?.Source, id?.Identifier);
        }
public bool SaveContactData(string firstName, string lastName, string identifierSource, string email, string phoneNumber, string language, string userAgent)
{
    try
    {
        var contactReference = GetIdentifiedContactReference(identifierSource, email);

        using (var client = SitecoreXConnectClientConfiguration.GetClient())
        {
            var contact = client.Get(contactReference, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, PhoneNumberList.DefaultFacetKey));

            //CONTACT IS COMING NULL
        }
        return true;
    }
    catch (Exception ex)
    {
        Log.Error("Error saving data to profile", ex, this);
        return false;
    }
}

private IdentifiedContactReference GetIdentifiedContactReference(string identifierSource, string identifier)
{
    if (Tracker.Enabled && !Tracker.Current.IsActive)
    {
        Tracker.StartTracking();
    }

    if (Tracker.Current?.Contact == null)
    {
        Log.Warn("Tracker.Current?.Contact == null", this);
        return null;
    }

    if (Tracker.Current.Contact.IsNew)
    {
        Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
        _contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
        IdentifyContact(identifierSource, identifier);
        return new IdentifiedContactReference(identifierSource, identifier);

    }

    var id = Tracker.Current.Contact.Identifiers.FirstOrDefault();
    return Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0
        ? new IdentifiedContactReference(identifierSource, identifier)
        : new IdentifiedContactReference(id?.Source, id?.Identifier);
}
edited title
Link
Mark Cassidy
  • 25.4k
  • 7
  • 59
  • 117

Sitecore 10.2: Contact is null

Source Link
Swati Gupta
  • 3.4k
  • 1
  • 17
  • 42

Sitecore 10.2: Contact is null

I am working on Sitecore 10.2 and trying to update facets in xDB. The below function is working fine on local but it is not working on VM. xDB is enabled on Sitecore instance.

    public bool SaveContactData(string firstName,string lastName, string identifierSource, string email,string phoneNumber, string language, string userAgent)
        {
            try
            {
                var contactReference = GetIdentifiedContactReference(identifierSource, email);

                using (var client = SitecoreXConnectClientConfiguration.GetClient())
                {
                    var contact = client.Get(contactReference, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, PhoneNumberList.DefaultFacetKey));

                   //CONTACT IS COMING NULL 



                }
                return true;
            }
            catch (Exception ex)
            {
                Log.Error("Error saving data to profile", ex, this);
                return false;
            }
        }

 private IdentifiedContactReference GetIdentifiedContactReference(string identifierSource, string identifier)
        {
            if (Tracker.Enabled && !Tracker.Current.IsActive)
            {
                Tracker.StartTracking();
            }

            if (Tracker.Current?.Contact == null)
            {
                Log.Warn("Tracker.Current?.Contact == null", this);
                return null;
            }

            if (Tracker.Current.Contact.IsNew)
            {
                Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                _contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
                IdentifyContact(identifierSource, identifier);
                return new IdentifiedContactReference(identifierSource, identifier);

            }

            var id = Tracker.Current.Contact.Identifiers.FirstOrDefault();
            return Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0
                ? new IdentifiedContactReference(identifierSource, identifier)
                : new IdentifiedContactReference(id?.Source, id?.Identifier);
        }