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)

what's wrong with this js?

(0) ShareShare
ReportReport
Posted on by

Hello, I have a js function that loops through and resolves selected cases. I'm trying to allocate 3 seconds for each case to resolve before attempting to resolve the next case. Here's the function I'm using:

function ResolveCases(statusCode)
{
var caseIds = SelectedControlSelectedItemIds;
alert('About to Resolve/Close ' + caseIds.length + ' case(s)');
for(var i=0; i<caseIds.length; i++)
{
var isLast = (i === (caseIds.length - 1));
setTimeout(function() {
debugger;
ResolveCase(statusCode, caseIds[i], isLast);
}, (3000 * i));
}
}

I'm new to using anonymous functions in this way in js. I tested the code above with 2 cases in js debug mode. However, when the debugger statement gets hit above, the "i" incrementer = 2. This results in an "undefined" caseId being sent to the ResolveCase function because there are only 2 cases in the list, so caseIds[2] is out of range.

This seems like a weird error. It seems to be straightforward that the "i" incrementer in setTimeout function should be iterated in a normal way. Is there something about the nature of using anonymous functions in for loops that I don't understand?

*This post is locked for comments

I have the same question (0)
  • jlattimer Profile Picture
    24,562 on at

    Are you sure caseIds only have 2 items? Could you possibly be generating an invalid value in whatever mechanism gets the selected ids?

    Can you add this code near the top?

    var caseIds = SelectedControlSelectedItemIds;

    alert(caseIds.length);

  • Verified answer
    Community Member Profile Picture
    on at

    Figured it out.  setTimeout with parameters in js requires a structure like this:

    function ResolveCases(statusCode)

    {

       var caseIds = SelectedControlSelectedItemIds;

       alert('About to Resolve/Close ' + caseIds.length + ' case(s)');

       for(var i=0; i<caseIds.length; i++)

       {

            var isLast = (i === (caseIds.length - 1));

            ResolveCaseOnTimer(statusCode, isLast, caseIds[i], i);

       }

    }

    function ResolveCaseOnTimer(statusCode, isLast, caseId, i)

    {

       setTimeout(function() {

           ResolveCase(statusCode, caseId, isLast);

       }, (5000 * i));

    }

    stackoverflow.com/.../settimeout-in-a-for-loop-and-pass-i-as-value

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