Hi
I am trying to get the async query to fill up a dictionary object. The dictionary is a global variable.
Basically, I am trying to get the onQuerySucceeded function to return the value. My code is attached.
I understand I have to use callback, but I just do not know how to go about doing it. The function only has to be triggered on textbox keydown.
var dict = {};
dict = {
Title: {},
Name: {},
Email: null,
AccountName: null,
Unit: {},
EmploymentCategory: {},
Status: {},
DateJoin: new Date({}),
DateExit: null,
ResignDate: null,
Country: {},
Department: {},
'Factory-NonFactory': {},
'Department-HOD': {},
CostCentre: {},
UnitSupervisorAccName: {},
HODAccountName: {},
Section: {}
};
var empidjstxtbox = document.getElementById(empidjs);
var empnametxt = document.getElementById(empnamejs);
empidjstxtbox.addEventListener('keydown', function(event){
// alert(event.key);
// If the user presses the "Enter" key on the keyboard
if (event.key === 'Enter') {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
//alert(empidjstxtbox.value);
//SearchStaffRecords(siteUrl, '1204');
// SearchStaffRecords(siteUrl, '1204'),
alert(dict);
console.log("Whats this" + dict.Name);
// console.log(dictval.Name);
}
});
function SearchStaffRecords(siteUrlparam, staffIDval) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('StaffRecords');
var camlQuery = new SP.CamlQuery();
//camlQuery.set_viewXml('<View></View>');
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + staffIDval + "</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Id,Title,Name,Email,AccountName,Unit,EmploymentCategory,Status,DateJoin,DateExit,Department,Department_x002d_HOD,Country,UnitSupervisorAccName,ResignDate,Factory_x002d_NonFactory,CostCentre,Section)');
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded(sender, args) {
//var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
var listCountry;
var listSupervisor;
//var listName;
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
// listCountry = oListItem.get_item('Country').get_lookupValue();
dict.Title = oListItem.get_item('Title');
dict.Name = oListItem.get_item('Name');
dict.Unit = oListItem.get_item('Unit').get_lookupValue();
dict.EmploymentCategory = oListItem.get_item('EmploymentCategory').get_lookupValue();
dict.Status = oListItem.get_item('Status');
dict.DateJoin = oListItem.get_item('DateJoin').toLocaleDateString();
dict.DateExit = oListItem.get_item('DateExit');
dict.ResignDate = oListItem.get_item('ResignDate');
dict.Country = oListItem.get_item('Country').get_lookupValue();
dict.Department = oListItem.get_item('Department');
dict['Factory-NonFactory'] = oListItem.get_item('Factory_x002d_NonFactory');
dict['Department-HOD'] = oListItem.get_item('Department_x002d_HOD').get_lookupValue();
dict.CostCentre = oListItem.get_item('CostCentre').get_lookupValue();
dict.UnitSupervisorAccName = oListItem.get_item('UnitSupervisorAccName').get_lookupValue();
dict.Section = oListItem.get_item('Section').get_lookupValue();
dict.Email = oListItem.get_item('Email').get_lookupValue();
dict.AccountName = oListItem.get_item('AccountName').get_lookupValue();
}
alert(dict.AccountName);
// getLeavelist(listCountry);
// var ins = new NF.PeoplePickerApi('#' + lvl1approverjs);
// //search for jon doe and add the first result to the people picker
// ins.search(listSupervisor).done(function (data) {
// // alert('Found');
// ins.add(data[0]);
// });
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}