Dear Developer community,
I'm currently developing an Application for a client to interact with the GTM API trough a Front End.
I have run into an issue with the callback handlers withSuccessHandler and withFailureHandler both are returned before the google.script.run function is completed.
Within my .HTML I have defined the following function:
function runUpdate(tagId,tagIndex) {
var id = 'tagInfo'+tagId
console.log(id)
google.script.run
.withSuccessHandler(onSuccess(id))
.withFailureHandler(onFailure(id))
.updateTag(<?=acc?>,<?=con?>,<?=wsp?>, tagId,<?=gaawc?>)
console.log("Script.run completed")
}
function onSuccess(tagIndex){
console.log("Success Handler triggered")
document.getElementById(tagIndex).innerHTML = "Success"
}
function onFailure(tagIndex){
console.log("Failure Handler triggered")
document.getElementById(tagIndex).innerHTML = "ERROR"
}
runUpdate is triggered on a button click.
The runUpdate() function is located in the Code.gs file and looks similar to this:
function updateTag(acc, con, wsp, tagId, gaawc) {
var oldTag = TagManager.Accounts.Containers.Workspaces.Tags.get("accounts/" + acc + "/containers/" + con + "/workspaces/" + wsp + "/tags/" + tagId)
if (oldTag){
Logger.log(JSON.stringify(oldTag))
var response = TagManager.Accounts.Containers.Workspaces.Tags.create(TranslateTag(oldTag, gaawc), "accounts/" + acc + "/containers/" + con + "/workspaces/" + wsp)
}
}
As per the documentation I expected .withSuccessHandler() and withFailureHandler() be fired accordingly to the outcome of the server side function.
The outcome is slightly different as the Log shows, the Handlers are being fired immediately after the runUpdate function is started.
userCodeAppPanel:2 function started
userCodeAppPanel:5 Failure Handler triggered
userCodeAppPanel:5 Success Handler triggered
userCodeAppPanel:25 Script.run completed
445144638-warden_bin_i18n_warden__en_gb.js:99 Net state changed from IDLE to BUSY
4078442995-mae_html_user_bin_i18n_mae_html_user__de.js:128 dropping postMessage.. deserialize threw error.
445144638-warden_bin_i18n_warden__en_gb.js:99 Net state changed from BUSY to IDLE
4078442995-mae_html_user_bin_i18n_mae_html_user__de.js:58 Uncaught at updateTag (translateTag:12)
The result is unfortunately that the last defined Handler would be overwriting the returned message Success/Failure.
Looking for any advice.