-1

I am having values in result[15452,33616] when using for loop ,the async function taking only last array value (e.g-33616) and fetching the data for that pushing to another array and final push into grid.for array[0] (e.g -15452)values data is not fetching . & it is repeating the same row three times for 33616.Please reply as soon as earlier.Need of urgent.

        for (var i = 0; i < result.length; i++)
        {
            var isFirst = 1;
            var context = new SP.ClientContext.get_current();
            var lists = context.get_web().get_lists();
            var associatedProjectsList = lists.getByTitle('Master Project Data');
            var camlQueryAssociatedProjects = new SP.CamlQuery();
            camlQueryAssociatedProjects.set_viewXml('<View><Query><Where><Eq><FieldRef Name="ID"/><Value Type="Number">'
            + result[i]["ESI_ProjectSeqNoLookupId"] + '</Value></Eq></Where></Query></View>');
            associatedProjectsItems = associatedProjectsList.getItems(camlQueryAssociatedProjects);
            context.load(associatedProjectsItems);
            context.executeQueryAsync(function () {
                MPDS = MasterProjectDataSuccess();
                if (isFirst == 1) {
                    rfaarr = MPDS;
                    isFirst = 0;
                }
                else {

                    rfaarr = $.extend(rfaarr, MPDS);
                    inputoJGrid.push(rfaarr);
                    self.PushToGrid(inputoJGrid);
                }
            }, MasterProjectDataFailure);


            var lists1 = context.get_web().get_lists();
            var associatedProjectsList1 = lists1.getByTitle('Project Users');
            var camlQueryAssociatedProjects1 = new SP.CamlQuery();
            camlQueryAssociatedProjects1.set_viewXml('<View><Query><Where><Eq><FieldRef Name="ID"/><Value Type="Number">'
            + result[i]["ESI_ProjectSeqNoLookupId"] + '</Value></Eq></Where></Query></View>');
            associatedProjectsItems1 = associatedProjectsList1.getItems(camlQueryAssociatedProjects1);
            context.load(associatedProjectsItems1);
            context.executeQueryAsync(function () {
                PUS = ProjectUsersSuccess();
                if (isFirst == 1) {
                    rfaarr = PUS;
                    isFirst = 0;
                }
                else {
                    rfaarr = $.extend(rfaarr, PUS);
                    inputoJGrid.push(rfaarr);
                    self.PushToGrid(inputoJGrid);
                }
            }, ProjectUsersFailure);
        }
    });
};
function MasterProjectDataSuccess() {
    var listItemEnumerator = associatedProjectsItems.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var cur = listItemEnumerator.get_current();
        rfalistfetch["ProjectTitle"] = cur.get_item("ESI_ProjectName");
        rfalistfetch["SequenceNo"] = cur.get_item("ESI_ProjectSequenceNo");
        rfalistfetch["Division"] = cur.get_item("ESI_Division").get_lookupValue();
        rfalistfetch["SubDivision"] = cur.get_item("ESI_SubDivision").get_lookupValue();
        rfalistfetch["Status"] = cur.get_item("ESI_Status").get_lookupValue();
        rfalistfetch["ProjectType"] = cur.get_item("ESI_ProjectCategory").get_lookupValue();
        var Budget = cur.get_item("ESI_BudgetYear");
        var dutyDateObj = new Date(Budget);
        var day = "" + dutyDateObj.getDate();
        if (day.length == 1) {
        day = "0" + day;
        }
        var month = "" + (dutyDateObj.getMonth() + 1);
        if (month.length == 1) {
        month = "0" + month;
        }
        var year = "" + (dutyDateObj.getYear() + 1);
        if (year.length == 1) {
        year = "0" + year;
        }
        rfalistfetch["BudgetYear"] = dutyDateObj.getFullYear();
        rfalistfetch["BMP"] = cur.get_item("ESI_BeginningMP");
        rfalistfetch["EMP"] = cur.get_item("ESI_EndingMP");
        rfalistfetch["State"] = cur.get_item("ESI_State").get_lookupValue();
        rfalistfetch["Station"] = cur.get_item("ESI_Station");
        rfalistfetch["SubCategory"] = cur.get_item("ESI_SubCategory").get_lookupValue();
        rfalistfetch["LineSegment"] = cur.get_item("ESI_LineSegment");
        rfalistfetch["DOT"] = cur.get_item("ESI_DOTNumber");
        rfalistfetch["BriefScope"] = cur.get_item("ESI_BriefScope");
        rfalistfetch["CPAR"] = cur.get_item("ESI_CPAR");
        rfalistfetch["ProjectID"] = cur.get_item("ID").toString();
        return rfalistfetch;
    }
};

function ProjectUsersSuccess() {
    var listItemEnumerator1 = associatedProjectsItems1.getEnumerator();
    while (listItemEnumerator1.moveNext()) {       
        var curr = listItemEnumerator1.get_current();
        rfalistfetch["ESDirector"] = curr.get_item("ESI_Director").get_lookupValue();
        return rfalistfetch;
    }
};
1
  • Asking your question a second time will not help with your credit rate Commented May 4, 2016 at 8:10

2 Answers 2

0

I have answered on this question: Files move from one library to another library using jsom

  1. Here i have set array globally.
  2. Push and load the item one by one in array with Looping of itemcollection
  3. Get array result in executeQueryAsync-success function
2
  • 3
    If I get your paycheck I can do your work Commented May 3, 2016 at 12:41
  • I can't update your code. I can give the solution or suggestion. Try to understand the code with here i have written steps. Commented May 3, 2016 at 13:02
0

Since you are using executeQueryAsync method of SharePoint it will process the request aysnchronously. Have a look at jquery Deferred variable to control your aynchronous operations.

Sample code:

var deferred = $.Deferred();
for(var i = 0; i < result.length; i++){
queryList(i).done(function(resultArr)
{
MasterProjectDataSuccess();
}) 
.fail(function(){
MasterProjectDataFailure();
})
}
5
  • where i have to use this deferred in my code/? Commented May 3, 2016 at 13:05
  • No need to include confusing deferred here. Not to mention he has to include jQuery as well. JavaScript has native promises now which can be ployfilled easily
    – Akhoy
    Commented May 3, 2016 at 13:08
  • can you explain how to do? Commented May 3, 2016 at 13:16
  • @Akhoy thanks for the info. Look into the following link for usage of javascript promise html5rocks.com/en/tutorials/es6/promises
    – Karvannan
    Commented May 3, 2016 at 13:45
  • Thanks for the info,But this how i implement in my code ,I am using for loop how it will works Commented May 4, 2016 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.