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 365 | Integration, Dataverse...
Suggested Answer

Index out of range error when running custom work flow

(0) ShareShare
ReportReport
Posted on by 10

Hi Everyone,

Getting an index out of range error when i run a custom workflow.

I've debugged and it breaks on the line highlighted in red. Any ideas why its breaking? do i need to specify the amount of elements in my entity collection?

try
{
  DynamicUrlParser dup = new DynamicUrlParser(dynamicRecordURL);
  string EntityLogicalName = dup.GetEntityLogicalName(service);
  Entity targetEntity = service.Retrieve(EntityLogicalName, dup.Id, new ColumnSet(true));
  bool changed = false;
  if (stringValue != null)
{
 string fetchXMLOptionSet = @"<fetch>
<entity name='stringmap' >
<attribute name='value' />
<attribute name='attributevalue' />
<filter>
<condition attribute='objecttypecodename' operator='eq' value='{0}' />
<condition attribute='attributename' operator='eq' value='{1}' />
<condition attribute='value' operator='eq' value='{2}' />
</filter>
</entity>
</fetch>";
fetchXMLOptionSet = String.Format(fetchXMLOptionSet, EntityLogicalName, fieldLogicalName, stringValue);
var query = new FetchExpression(fetchXMLOptionSet);
EntityCollection OptionsetValues = service.RetrieveMultiple(query);
Entity OSV = OptionsetValues[0];
targetEntity[fieldLogicalName] = new OptionSetValue(OSV.GetAttributeValue<int>("attributevalue"));
changed = true;

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Most of my c# for a returned entity collection goes like this with the results:

    OptionsetValues.Entities[0]

    Maybe start there, or else you could grab the string value of your fetchxml and see whats returning inside the XRMtoolbox FetchXML tester tool to get a better feel for whats happening.

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    1. Make sure you are target and source option set are syc in term of the indexes, this error occurs when you are trying to set some index which is not present in the optionset

    2. Before assigning value check if this is available or not like below

    if(OSV.Contains(attributevalue))

    {

    //assignment

    }

  • erhan.keskin Profile Picture
    2,253 on at

    Hi,

    It looks that query doesn't retrieve any data, so OptionsetValues doesn't contain any data, so OptionsetValue[0] throws the error.

    - you can check if the query returns any data

    - you can add a null control before reading OptionsetValues[0]

    - I don't see the definition of fieldLogicalName or value assignment, make sure that variable has a value

    regards,

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 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 74

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans