GetListItems variable help!!!

Apr 19, 2013 at 11:44 PM
Folks,
I am trying to set a global variable using the result of a GetListItems function at document ready. I have tried using 'var' and without; both result in an error message saying the variable is not defined. Here is the portion of my code where Im having the issue.
                $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "Assigned Resource Progress Tracker",
                    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='stringTitle' /></ViewFields>",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='stringTitle' /><Value Type='text'>"+concatstringTitle+"</Value></Eq></Where></Query>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode("z:row").each(function() {
                            actualStringTitle = $(this).attr("ows_stringTitle");
                        });
                    }
                });

alert(actualStringTitle);

Essentially, the alert is to test if the data is pulling the appropriate list item correctly. The end result of the variable is to use it in an if/then statement to perform additional functions in my SharePoint form.

Any assistance as to why I cannot declare/set the variable 'actualStringTitle' is appreciated.
Apr 20, 2013 at 12:44 AM
Can you put the alert inside the completefunc function, right below the .each() and see if it triggers.

You have no checks below, see if the SPServices call was successful, so we should first check that you in fact get an item (or more) back.

_____
Paul

Sent from mobile device.
Coordinator
Apr 20, 2013 at 3:10 PM
It looks like a scoping issue. You need to declare the variable outside the SPServices call. Something like this:
var actualStringTitle;
                $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "Assigned Resource Progress Tracker",
                    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='stringTitle' /></ViewFields>",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='stringTitle' /><Value Type='text'>"+concatstringTitle+"</Value></Eq></Where></Query>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode("z:row").each(function() {
                            actualStringTitle = $(this).attr("ows_stringTitle");
                        });
                    }
                });

 alert(actualStringTitle); 
However, you're looping through the items, so this would alert the last value. It sounds like you want to do something more like:
                $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "Assigned Resource Progress Tracker",
                    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='stringTitle' /></ViewFields>",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='stringTitle' /><Value Type='text'>"+concatstringTitle+"</Value></Eq></Where></Query>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode("z:row").each(function() {
                            var actualStringTitle = $(this).attr("ows_stringTitle");
                            alert(actualStringTitle); 
                        });
                    }
                });
This will alert every actualStringTitle from the returned list items.

M.
Apr 22, 2013 at 5:04 PM

You are correct that I am looping through the list; however, the CAML Query will only result in one item if it was previously created and active in the list. My desired end result is to have the variable actualStringTitle set by the result of the GetListItems script; this will be matched against another variable in an if/then statement. The alert was my way of testing the result.

I found that whenever I close the GetListItems script with the required }); the result is not available, so I tried to set a global variable using the ‘return’ command, but still did not accomplish what I wanted. I will try the 1st option you provided and see how that works for me…thank you again for all of your help.

Thank you,

William J. Nevarez
AVP; Project Manager
Operational Performance & New BAC Execution

Bank of America
213.345.2133

"As iron sharpens iron, so one man sharpens another."
Prov 27:17

Apr 22, 2013 at 5:16 PM

Marc,

I was able to use the suggestion you provided, and it works as desired!

By declaring the actualStringTitle as a blank variable (var actualStringTitle = “”;) prior to the GetListItems script, declaring the variable within the GetListItems script overwrote the blank. Thank you again!!!

Thank you,

William J. Nevarez
AVP; Project Manager
Operational Performance & New BAC Execution

Bank of America
213.345.2133

"As iron sharpens iron, so one man sharpens another."
Prov 27:17