how to update variable inside callback function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 06:28 AM
HI, I have defined variable outside and i want to update the value inside callback . How can i do it.
I have tried to explain my scenario using an example.
var name='abc';
if(user!=' '){
var x = g_form.getReference('user', callFunction);
function callFunction(x){
var managerName=x.manager;
name=name+"_" + managerName;
}
}
alert(name); // I'm not getting the updated name it still shows abc
//The goal I want to achieve is the variable to update the value and use it outside callFunction
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 11:53 PM
Hi,
then why not send the variable value to the ajax function; perform the logic there and return the final value along with other values to be returned.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:07 AM
Yea this was the last option i was looking for
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 12:29 AM
Hi,
I did find few OOB client scripts where the variable declared outside is accessible inside the callback but it was using GlideAjax
This is one example:
Name: Calc duration from Approved End Date
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var APPROVED_START_DATE = "approved_start_date",
DURATION = "duration",
APPROVED_END_DATE = "approved_end_date";
// TODO: This file mimics the behavior of 'Calculate Duration From End Date' need to move the
// common code to a single client script.
calculateDurationOnChangeOfApprovedEndDate();
function calculateDurationOnChangeOfApprovedEndDate() {
if (isLoading || newValue === '' || g_scratchpad.flag || !g_form.isNewRecord() || oldValue == newValue || g_scratchpad.prevApprovedEndDate == newValue) {
g_scratchpad.flag = false;
return;
}
g_scratchpad.prevApprovedEndDate = newValue;
var apprStartDate = g_form.getValue(APPROVED_START_DATE);
if (!apprStartDate) {
g_form.showFieldMsg(APPROVED_START_DATE, getMessage('Approved start date shouldn\'t be nil'), 'error');
return;
}
var pmClientDateAndDurationHandler = new PmClientDateAndDurationHandler(g_form);
if (!pmClientDateAndDurationHandler.isValidClientDate(APPROVED_START_DATE))
return false;
if (!pmClientDateAndDurationHandler.isValidClientDate(APPROVED_END_DATE))
return false;
calculateDuration(apprStartDate);
}
function calculateDuration(apprStartDate) {
var ga = new GlideAjax('AjaxProjectTaskUtil');
ga.addParam('sysparm_start_date', apprStartDate);
ga.addParam('sysparm_end_date', g_form.getValue(APPROVED_END_DATE));
if (g_scratchpad.nested) {
ga.addParam('sysparm_name', 'getDuration');
ga.addParam('sysparm_sys_id', g_scratchpad.projectTaskSysId);
} else {
var schedule;
if (g_form.hasField('schedule'))
schedule = g_form.getValue('schedule');
else
schedule = g_scratchpad.schedule;
ga.addParam('sysparm_name', 'getDurationPerSchedule');
ga.addParam('sysparm_sys_id', schedule);
}
ga.getXML(handleResponse);
}
function handleResponse(response) {
if (response && response.responseXML) {
var result = response.responseXML.getElementsByTagName("result");
if (result) {
result = result[0];
var status = result.getAttribute("status");
var answer = result.getAttribute("answer");
var message = result.getAttribute('message');
if (status == 'error') {
pmClientDateAndDurationHandler.showErrorMessage(APPROVED_END_DATE, message);
} else {
g_scratchpad.flag = true; //This flag is also used in other client scripts to avoid re-triggering
g_form.setValue(DURATION, answer, answer); // this comes from the above variable
}
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 04:44 AM
It is accessible inside but the problem is if we update the value inside callback and then use that variable outside it will not show updated value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2022 09:22 PM
Hi,
I believe that's expected since you are doing the calculation inside the callback.
Try to search any OOB script for this
But why to use that variable outside the callback when the entire calculation can be done within callback method?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader