Folks, I have a custom button on SharePoint form with an onClick event that is as follows:
onclick="if (!itemDuplicated()){ddwrt:GenFireServerEvent('__commit;__redirectsource')}; window.alert('Item updated.') ; window.close();"
The script to handle the click event is as follows:
function itemDuplicated(){
var field1 = document.getElementById("ctl00_m_g_7d2eac47_1ef1_4ab4_96b7_38089fea54a1_ff21_ctl00_ctl00_TextField").value;
var siteUrl = 'http://myurl';
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle("RFC Field Grouping (kerry test of concept)");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name='field1'/><Value Type='Text'>' + field1 + '</Value></Eq></Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Title)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),Function.createDelegate(this, this.onQueryFailed));
} //end function
in the this.onListItemsLoadSuccess function I am easily able to get the value I need from the caml query but because it is running Async it never gets considered and the itemDuplicated function always returns false.
I have read a lot about this and how it's a difficult thing to accomplish, making asynchronous calls synchronous (not recommended) and using callbacks, etc
I have tried a bunch of different things to no avail. Is there any way to accomplish this?