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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 174

#2
ManoVerse Profile Picture

ManoVerse 58 Super User 2026 Season 1

#3
Niki Patel Profile Picture

Niki Patel 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans