Isaac
asked on
JavaScript Scope issue
Hi All,
Please see an excerpt of my code below. I am using SharePoint 2010 and JSOM to retrieve data from my list.
My getID() is returning 'undefined'. I'm assuming it's a scope issue but not sure how to fix it. Any ideas would be great.
Thanks!
Please see an excerpt of my code below. I am using SharePoint 2010 and JSOM to retrieve data from my list.
My getID() is returning 'undefined'. I'm assuming it's a scope issue but not sure how to fix it. Any ideas would be great.
// JavaScript source code
$(document).ready(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
$("#procPlan").on("click", function () {
ppConstruction_addListItem();
//Empty fields after submit
$("#origDate").val('');
$("#ppcSetAside").val('');
});
});
var getQueryString = function (field, url) {
var href = url ? url : window.location.href;
var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
var string = reg.exec(href);
return string ? string[1] : null;
};
itemProjectNumber = getQueryString('projNumber');
function retrieveData() {
getListData("Project Module", "queryError");
getListData("Project Projections", "Procurement Plan (Design)");
getListData("Project Projections", "Design Award");
getListData("Project Projections", "Procurement Plan (Construction)");
getListData("Project Projections", "Construction Award");
var itemID = getID(itemProjectNumber, 'Procurement Plan (Design)');
alert("itemID: " + itemID); //<-- Returns '[b]itemID:undefined[/b]'
}
function getID(pid, category) {
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getByTitle('Project Projections');
if (itemProjectNumber) {
var prjQry = "<View><Query><ViewFields>" +
"<FieldRef Name='ID' />" +
"<FieldRef Name='PlanningCategory' />" +
"<FieldRef Name='ProjectNumber' />" +
"</ViewFields>" +
"<Where>" +
"<And>" +
"<Eq>" +
"<FieldRef Name='PlanningCategory' />" +
"<Value Type='Choice'>" + category + "</Value>" +
"</Eq>" +
"<Eq>" +
"<FieldRef Name='ProjectNumber' />" +
"<Value Type='Text'>" + pid + "</Value>" +
"</Eq>" +
"</And>" +
"</Where></ViewFields></View>";
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(prjQry);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.OnQuerySucceeded),
Function.createDelegate(this, this.queryError)
);
}
}
function OnQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var id = oListItem.get_item('ID');
alert("In loop: " + id); //<-- Returns '[b]In loop: 36[/b]'
}
alert("Outside loop: " + id); //<-- Returns '[b]outside loop: 36[/b]'
return id; //<-- Should return 36 but the calling function alerts 'undefine' WHY?
}
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now that makes a lot of sense. Looks like I need to understand scope a lot better.
I will modify my code to reflect suggested changes.
Thanks!
I will modify my code to reflect suggested changes.
Thanks!
You are welcome.
You need to do a return of the required value from the function in order for the assignment to mean anything - otherwise how is the code supposed to know what value you want to assign to itemID?