Skip to main content

Notifications

Announcements

No record found.

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,249 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
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans