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;

  • erhan.keskin Profile Picture
    erhan.keskin 2,253 on at
    RE: Index out of range error when running custom work flow

    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,

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Index out of range error when running custom work flow

    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

    }

  • Suggested answer
    RE: Index out of range error when running custom work flow

    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.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans