web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

SSIS optionset field update

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,
I am trying to update columns from one CRM application to the other using ssis script component. The update was failing on an optionset field which is throwing this error:
"an item with the same key has already been added". Can somebody please look at the optionset and let me know how do I make changes.

public override void ContactInput_ProcessInputRow(ContactInputBuffer Row)
    {
        bool hasDulicate = false;

        Entity existingLAPInterpreter = new Entity("new_lapinterpreter");

    
        FilterExpression codeFilter = new FilterExpression();
        codeFilter.AddCondition("new_interpretercontactid", ConditionOperator.Equal, Row.newAutoContactID);

        QueryExpression query = new QueryExpression
        {
            EntityName = "new_lapinterpreter",
            ColumnSet = new ColumnSet("new_firstnameintlap", "new_lastname", "new_interpreterlanguage", "new_emailintlap", "new_primaryphoneintlap", "new_interpretertype", "new_interpreterstate"),
            Criteria = codeFilter
        };

        EntityCollection records = _orgService.RetrieveMultiple(query);
        int totalrecords = records.Entities.Count;

        foreach (Entity record in records.Entities)
        {
            if (record["new_firstnameintlap"] != null)
            {
                record["new_firstnameintlap"] = Row.FirstName;
            }

            if (record["new_lastname"] != null)
            {
                record["new_lastname"] = Row.LastName;
            }

            if (record["new_interpreterlanguage"] != null)
            {
                record["new_interpreterlanguage"] = Row.newprimarylanguage;
            }

            if (record["new_emailintlap"] != null)
            {
                record["new_emailintlap"] = Row.emailaddress1;
            }

            if (record["new_primaryphoneintlap"] != null)
            {
                record["new_primaryphoneintlap"] = Row.mobilephone;
            }

            if (record["new_interpreterstate"] != null)
            {
                record["new_interpreterstate"] = Row.address1stateorprovince;
            }

            OptionSetValue updateinterpreterType = new OptionSetValue();

            if (record["new_interpretertype"]!=null)
            {
                switch (Row.newinterpretertype)
                {
                    case 100000000:
                        updateinterpreterType.Value = 100000000;
                        break;
                    case 100000001:
                        updateinterpreterType.Value = 100000001;
                        break;
                    case 100000002:
                        updateinterpreterType.Value = 100000002;
                        break;
                    case 100000003:
                        updateinterpreterType.Value = 100000003;
                        break;
                    case 100000004:
                        updateinterpreterType.Value = 100000004;
                        break;
                    default:
                        updateinterpreterType.Value = 100000001;
                        break;
                }

                record.Attributes.Add("new_interpretertype", (OptionSetValue)updateinterpreterType);
         
            }

                _orgService.Update(existingLAPInterpreter);
                hasDulicate = true;
            }
}


Thanks for any help.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Royal King Profile Picture
    27,686 on at

    It looks like record entity object already contains attribute with name "new_interpretertype" in collection but you are trying to add it again that's the reason it was throwing an error .Change the below line of the code to fix this issue.

    From 

    record.Attributes.Add("new_interpretertype", (OptionSetValue)updateinterpreterType);

    To 

    record["new_interpretertype"] = updateinterpreterType

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Hi..

    Try removing Option set first from entity context and then add it back

    record.Attributes.Remove("new_interpretertype");

    record.Attributes.Add("new_interpretertype", (OptionSetValue)updateinterpreterType);

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Rox,

    You have defined more than 1 key for the new_interpretertype

    You can either remove first

    if(record.Attributes.Contains ("new_interpretertype"))

    {

      record.Attributes.Remove("new_interpretertype");

    }

    record.Attributes.Add("new_interpretertype", (OptionSetValue)updateinterpreterType);

    or just use

    record.Attributes["new_interpretertype"] = (OptionSetValue)updateinterpreterType;

    Thanks,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans