Not Get response From Server Side Script Include

payalpatel26
Tera Contributor
var FetchPromptStrctureBasedOnChangeServiceType = Class.create();
FetchPromptStrctureBasedOnChangeServiceType.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    // Function to fetch questions based on the selected type
    getQuestionsBasedOnType: function (selectedType) {
        var selectedType = this.getParameter('sysparm_selectedType');
gs.addInfoMessage("serverSideType"+selectedType);
 
        // Query the 'ServiceType' table and retrieve questions
        var serviceTypeGR = new GlideRecord('ServiceType');
serviceTypeGR.addQuery('type', selectedType);
serviceTypeGR.query();
 
        if (serviceTypeGR.next()) {
            var promptStructure = serviceTypeGR.getValue('promptstructure');
            return promptStructure ; // Return an empty string if PromptStructure is null
        }
 
      
    },
 
    type: 'FetchPromptStrctureBasedOnChangeServiceType'
});

 

1 ACCEPTED SOLUTION

Please also take a screenshot of the other field - promptstructure.

 

But even from this screenshot a problem can be seen: type is of type String in which you are looking for a sys_id.

That is theoretically possible but very NOT likely.

So the problem could be line

serviceTypeGR.addQuery('type', selectedType);

in the Script Include.

It is possible and likely that what you mean is:

serviceTypeGR.addQuery('sys_id', selectedType);

which can be short-handed to:

...
// Query the 'ServiceType' table and retrieve questions
var serviceTypeGR = new GlideRecord('x_1296451_legaladv_servicetype');

if (serviceTypeGR.get(selectedType)) {
	gs.addInfoMessage('serviceoog' + serviceTypeGR);
	promptStructure = serviceTypeGR.getValue('promptstructure');
}

gs.addInfoMessage('promptStructure ' + promptStructure);
...

View solution in original post

22 REPLIES 22

var serviceTypeGR = new GlideRecord('x_1296451_legaladv_servicetype');

I try this one still not get get response please suggest if i am wrong

 

Narsing1
Mega Sage

Never saw a Table in Servicenow of this type.  The table should be in all small letters. Please check the table name

Thanks,

Narsing

thank you for your responses as per your suggestion i try 

var serviceTypeGR = new GlideRecord('x_1296451_legaladv_servicetype');

still not get response  let me know still you see wrong

Can you please post screen shots of both Script include & Client script.  Now, I suspect these

  • ACL on Script include - this should be given to snc_internal
  • Is the parameter getting the value and is the value exists in the given table
  • Have you tried using the Javascript executer and pass a hardcoded value for "sysparm_selectedType"?

Thanks,

Narsing

Script Include:

var FetchPromptStrctureBasedOnChangeServiceType = Class.create();
FetchPromptStrctureBasedOnChangeServiceType.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    // Function to fetch questions based on the selected type
    getQuestionsBasedOnType: function () {
 
var promptStructure;
var xmsresponse;
        var selectedType = this.getParameter('sysparm_selectedType');
gs.addInfoMessage("serverSideType"+selectedType);
 
        // Query the 'ServiceType' table and retrieve questions
        var serviceTypeGR = new GlideRecord('x_1296451_legaladv_servicetype');
serviceTypeGR.addQuery('type', selectedType);
serviceTypeGR.query();
 
        if (serviceTypeGR.next()) {
gs.addInfoMessage("serviceoog"+serviceTypeGR);
            promptStructure = serviceTypeGR.getValue('promptstructure');
 
         
        }
gs.addInfoMessage("promptStructure "+promptStructure);
    return promptStructure ; // Return an empty string if PromptStructure is null
 
      
    },
 
    type: 'FetchPromptStrctureBasedOnChangeServiceType'
});
 
ClientScript;
 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below


var selectedType = g_form.getValue('type');
g_form.addInfoMessage("selectedValueInClient" + selectedType);

// Fetch Questions based on the selected Type using GlideAjax
getQuestionsBasedOnType(selectedType, function(questions) {
g_form.addInfoMessage("CheckResponse", questions);
// Check if the response has an error
if (questions && response.responseXML && response.responseXML.documentElement) {
var errorMessage = response.responseXML.documentElement.getAttribute('error');
if (errorMessage) {
g_form.addInfoMessage('GlideAjax Error:', errorMessage);
}
}

// Create and show the popup
var dialog = new GlideDialogWindow('ServiceRequestGuidance');
dialog.setTitle("Legal Service Matters Detail");
dialog.setWidth(600);
dialog.setHeight(400);

var html = "<div>";
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
html += "<label>" + question + "</label>";
html += '<input type="text" id="question_' + i + '"/><br>';
}
html += '</div>';

dialog.renderWithContent(html);

dialog.setPreference('selectedType', selectedType);
dialog.setPreference('numOfQuestions', questions.length);
dialog.setPreference('table', g_form.getTableName());
dialog.setPreference('recordSysId', g_form.getUniqueValue());

dialog.show();
});

}

function getQuestionsBasedOnType(selectedType, callback) {
var ga = new GlideAjax('FetchPromptStrctureBasedOnChangeServiceType');
ga.addParam('sysparm_name', 'getQuestionsBasedOnType');
ga.addParam('sysparm_selectedType', selectedType);
ga.getXML(function(response) {
if (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var questions = answer.split(',');
callback(questions);
} else {
alert("Empty or invalid 'answer' attribute in the response from the server.");

}
} else {
alert("Empty or invalid response received from the server.");
}
});

 

2) Yes parameter get value as id  both side client side and server side .and i get type which value of service type table .

payalpatel26_0-1704655517759.png

 

3)

  • Have you tried using the JavaScript executer and pass a hardcoded value for "sysparm_selectedType"?  -- yes as upper screen shot you can see value of service type which I check from inspect element which is right ?