Field message with GlideAjax

Viktor Stepanov
Mega Contributor

Hello all,

I need to create display field messages in an incident form.

The idea is to check whether the field in question fulfills some parameters (ie. CI = category: computer) and then show the info message under the field.
This has to be done with GlideAjax and it has to reflect the language used (ie. Spanish).

I would like to ask for a roadmap  - I'm rather new with SN and am thinking that this would be basically done with a client callable Script Include, calling the CI table via a Glide Record - then being used by a Client Script with GlideAjax?

 

Thanks,

VS

8 REPLIES 8

There is no need to write any code for this.

If you use the getMessage() function, the correct lanugage will be displayed for the users if the message is specified in the language table for the users language.

Each user can select their own language on their profile and this is what will decide which message will be displayed (what language). If no language is displayed or if message is missing, the default language will display.

RAHUL Khanna1
Mega Guru

You can do one thing....

First Create all the messages in English and Spainish in the Message Table with Keys.

 

Include the following code in SCript Incllude from where your are calling the category. 

var myUserObject = gs.getUser();
var lan = myUserObject.getLanguage();

msg = gs.getMessaage('key_english');

send the Language and Category from SI to CS. 

in CS 

check like this 

if(lan == en )

g_form.showFieldMEssage('category', msg);

 

 

Viktor Stepanov
Mega Contributor

Thank you all very much for your awesome suggestions - going through them this is what I ended up with that seems to be working, leaving here for any further modifications or if anybody else would use it:

 

UI Messages:

Two messages per language with same key

 

Client Script:

Table: Incident / Type: onChange / Field Name: Configuration Item / Messages: list of UI msgs prepared

Script:

    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
     if (newValue == '') {
      return;
     }  
       
        var configVal = g_form.getValue('cmdb_ci');
       
        var ga = new GlideAjax('GetConfigData');
        ga.addParam('sysparm_name', 'getCIData');
        ga.addParam('sysparm_configVal', configVal);
        ga.getXMLAnswer(stateCallback);
    
        function stateCallback(answer) {
            if (answer=='Installed' || answer == 'Installiert'){
            g_form.showFieldMsg('cmdb_ci', getMessage('msg1') ,'info');
        }else{
            g_form.showFieldMsg('cmdb_ci', getMessage('msg3') ,'error');
        }
            }
        }
        

 

 

Script Include:

    var GetConfigData = Class.create();
    GetConfigData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
        getCIData : function() {
            
            var configVal = this.getParameter('sysparm_configVal');
            
            var map = new GlideRecord('cmdb_ci');
            map.addQuery('sys_id', configVal);
            map.query();
            
            if (map.next()) {
                return map.install_status.getDisplayValue();
            }
        },
        type: 'GetConfigData'
        
    });

 

I'm not very happy with the fact it's returning a string (installed/installiert), if I find a way to tweak this further so it's more elegant I'll let you know (or any ideas are of course welcomed)

At this moment though my issue is solved.

Thanks all again!

 

Good you found a solution for this. I would change the return value to be the value of the install_status instead of the display value

return map.getValue('install_status');

By doing this you'll get a number value for the Status, it'll be the same number for all languages. Better to use that since you might have a third language later and then you would have to change the code to get it to work with the new language. With using the value of the state you don't have to do that.

Would be good if you could mark an answer as correct as well so this thread doesn't show up as unsolved.