LogiUpSkill

Scripting

 

  • How to access last worknotes?

      • We can use “current.comments.getJournalEntry(1)” to access last worknotes.
    • hasRole()

      • It returns true if the current user has the specified role or the admin role
    • How to get system property?

      • getProperty(“PROPERTY_NAME “);
    • How to set system property?

      • setProperty(“PROPERTY_NAME “, “property_value”);
    • How to alert Assigned to user name from client script?

      • var p=g_form.getReference(“assigned_to”).name;
      • alert(p);
    • How to access user data from client script

      • g_user.userName
      • g_user.userID
      • g_user.firstName
    • How to access user data from Server Side Script

      • var firstName = gs.getUser().getFirstName();
      • var email = gs.getUser().getEmail();
      • getUser();
    • How to get sys_id from client script

      • alert(g_form.getUniqueValue());
    • How to get current form table display value in client script

      • alert(g_form.getDisplayValue()); and
      • alert(g_form.getDisplayValue(“assigned_to”))
    • How to hide the related list from client script

      • hideRelatedList(“REL:SYSIDOFRELATEDLIST”);
    • How to hide section from script

      • setSectionDisplay(“closure_information”, true);
    • How to fire Event from Server-side script

      • eventQueue(“EVENTNAME”, GlideRecord, Parm1, Parm2)
    • How to write GlideRecord on incident to get active records

      • Var gr=New GlideRecord(“incident”);
      • addQuery(“active”,true);
      • query();
      • if(gr.next()){
      • info(gr.number);
      • }
    • G-FORM:GlideForm.js is the Javascript class used to customize forms.

      • getValue(‘short_description’)
      • setValue(‘short_description’, “”)
      • addOption(‘priority’, ‘2.5’, ‘2.5 – Moderately High’, 3)
      • getTableName()
      • addErrorMessage(‘This is an error’)
      • addInfoMessage(‘The top five fields in this form are mandatory’)
      • showFieldMsg(‘impact’,’Low impact response time can be one week’,’info’)
      • showFieldMsg(‘impact’,’Low impact not allowed with High priority’,’error’)
      • flash(“incident.number”, “#FFFACD”, 0)
  • g_user: g_user is a global object in GlideUser, can only used in Client Script contains name and role information about the current user.

    • userName
    • userID
    • firstName
    • getClientData(“loginlanguage”)
    • hasRole(‘admin’)
    • hasRoleExactly(‘util’)
    • hasRoleFromList(“itil, maint”)
    • hasRoles()


Client Script

1.To alert fullname of user:

function onLoad() {

       var p=g_user.getFullName();

    alert(‘This is my first client Script= ‘+p);

}

2.To alert If the Record is new or not:

function onLoad() {

    if (g_form.isNewRecord()){

        alert(‘This is new Record’);

    }

    else{

        alert(‘This is not a new record’);

    }

}

3.Use of different API’s:

function onLoad() {

    g_form.setReadOnly(‘u_email’,true);

    g_form.setValue(‘u_description’,’hiiiiiii’);

      g_form.clearOptions(‘u_category’);

    g_form.getLabelOf(‘u_number’);

    g_form.addInfoMessage(‘hi’);

    g_form.setMandatory(‘u_first_name’,true);

    g_form.setMandatory(‘u_last_name’,true);

    if (g_user.hasRole(admin)){

    g_form.removeOption(‘u_state’,’Maharashtra’);

    }

}

4.To make field visible after creating record but not while creating Record

function onLoad() {

   if(g_form.isNewRecord()){

g_form.setVisible(“u_convert_to”,false);

}

    else {

        g_form.setVisible(“u_convert_to”,true);

    }

}

5.when Assigned To value changes, populte mail id of changed user in email field and populate user name in short description

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if (isLoading || newValue === ”) {

      return;

   }

var p=g_form.getReference(“u_assigned_to”).email;

    g_form.setValue(“u_email”,p);

    g_form.setMandatory(“u_first_name”,true);

    g_form.setValue(“u_short_description”,’This is :’+g_user.getFullName());

}

6.when Assigned To value changes,populate user’s first name in short description

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === ”) {

return;

  }

var p = g_form.getValue(“u_assigned_to”);

    var name= g_form.getReference(“u_assigned_to”);

    g_form.setValue(“u_short_description”, ‘This is’+name.first_name);

alert(newValue);

alert(oldValue);   

}

7.Confirm details before submit form

function onSubmit() {

   //Type appropriate comment here, and begin script below

   var a = g_form.getValue(“u_first_name”);

    var b = g_form.getValue(“u_last_name”);

    var con= confirm( ‘You Entered below datails-\n The first name is- ‘+a+’\n The last name is- ‘+b+’\n  Do you confirm the details?’ );

    if(con==true){

        return;

    }

    else{

        return 0;

    }

}

8. On Submit

function onSubmit() {

   //Type appropriate comment here, and begin script below

    var p = g_form.getValue(“u_assigned_to”);

    var name= g_form.getReference(“u_assigned_to”);

    g_form.setValue(“u_short_description”, name.first_name);

alert(p);

9. On editing first name in list view (onCellEdit Client Script)

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

  var saveAndClose = true;

    alert(sysIDs);

    alert(table);

    alert( oldValues);

    alert(newValue);

    alert(callback);

 callback(saveAndClose); 

}

Advance Client Script

Client Script:

Client Side Objects:

  • 1. G-FORM:GlideForm.js is the Javascript class used to customize forms.

    g_form.getValue(‘short_description’)
    g_form.setValue(‘short_description’, “”)
    g_form.addOption(‘priority’, ‘2.5’, ‘2.5 – Moderately High’, 3)
    g_form.getTableName()
    g_form.addErrorMessage(‘This is an error’)
    g_form.addInfoMessage(‘The top five fields in this form are mandatory’)
    g_form.showFieldMsg(‘impact’,’Low impact response time can be one week’,’info’)
    g_form.showFieldMsg(‘impact’,’Low impact not allowed with High priority’,’error’)
    g_form.flash(“incident.number”, “#FFFACD”, 0)

 

  • 2. G-User: g_user is a global object in GlideUser. g_user is a Client Script Object and contains name and role information about the current user.


    g_user.userName
    g_user.userID
    g_user.firstName
    g_user.getClientData(“loginlanguage”)
    g_user.hasRole(‘admin’)
    g_user.hasRoleExactly(‘util’)
    g_user.hasRoleFromList(“itil, maint”)
    g_user.hasRoles()

Client Side Examples:

1. Problem Statement: For New Record show alert.


function onLoad() {

if (g_form.isNewRecord()){

alert(‘This is new Record’);

}

else{

alert(‘This is not a new record’);

}

}

………………………………………………………………………………………………………..

2. Problem Statement: To make “Convert to” field visible after creating record but not while creating Record


function onLoad() {

 

if(g_form.isNewRecord()){

g_form.setVisible(“u_convert_to”,false);

}

else {

g_form.setVisible(“u_convert_to”,true);

}

}

3. Problem Statement: When Assigned To value changes, populte mail id of changed user in email field and populate user name in short description


function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === ”) {

return;

}

var p=g_form.getReference(“u_assigned_to”).email;

g_form.setValue(“u_email”,p);

g_form.setMandatory(“u_first_name”,true);

g_form.setValue(“u_short_description”,’This is :’+g_user.getFullName());

}


………………………………………………………………………………………………………..

4. Problem Statement: Confirm details before submit form

function onSubmit() {

//Type appropriate comment here, and begin script below

var a = g_form.getValue(“u_first_name”);

var b = g_form.getValue(“u_last_name”);

var con= confirm( ‘You Entered below datails-\n The first name is- ‘+a+’\n The last name is- ‘+b+’\n Do you confirm the details?’ );

if(con==true){

return;

}

else{

return 0;

}

}

………………………………………………………………………………………………………..

5. Problem Statement: Hide the Clouser Information section from form while creating new record

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.isNewRecord()){
g_form.setSectionDisplay(“closure_information”, false);
}else{
g_form.setSectionDisplay(“closure_information”, true)
}
}

………………………………………………………………………………………………………..

6. Problem Statement: Remove New state from the choice if it is not New

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.getValue(“state”)==1){
}else{
g_form.removeOption(“state”,’1′);
}

}

7. Problem Statement: On change—Comments Make Work Notes Optional

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var requireOtherField = (newValue == ”);
g_form.setMandatory(‘work_notes’, requireOtherField);
}


………………………………………………………………………………………………………..

8. Problem Statement: On Change- Work Notes Make Comments Optional

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var requireOtherField = (newValue == ”) ;
g_form.setMandatory(‘comments’, requireOtherField);
}

9. Problem Statement: If Caller is a VIP User then priority should be always P1.

On Change Client Script on Caller Field

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
g_form.getReference(‘caller_id’, priorityCallback);

}

function priorityCallback(caller){


if (caller.vip == ‘true’) {
g_form.setValue(“impact”,1);
g_form.setValue(“urgency”,1);

}
}


………………………………………………………………………………………………………..

10. Problem Statement: Start date should be less than End Date,if not show alert


Start date validation –
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}

var start_date = g_form.getValue(“u_start_date”);
var end_date = g_form.getValue(“u_end_date”);
var format = g_user_date_time_format;

if (start_date != “” && end_date != “”) {
var isEndBeforeStart = compareDates(start_date, format, end_date, format);
//0 – means valid date(end date is more than start date)
//1 – means start is grater than end
if (isEndBeforeStart) {
alert(“Start date can not be grater than end date”);
g_form.setValue(“u_start_date”, ” “);

}
}

}


………………………………………………………………………………………………………..

11. Problem Statement: Expected Date Should be State date + 5 days

OnChange Client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ”) {
return;
}
var start_date = g_form.getValue(“u_start_date”);
var glideAjax = new GlideAjax(“ExpectedCloseDateSI”);
glideAjax.addParam(“sysparm_name”,”getStartDate”);
glideAjax.addParam(“sysparm_start_date”,start_date);
glideAjax.getXML(callBackFunction);

function callBackFunction(response){
var res = response.responseXML.documentElement.getAttribute(“answer”);
g_form.setValue(“u_expected_close_date”,res);
}
//Type appropriate comment here, and begin script below

}


Script include
ExpectedCloseDateSI

var ExpectedCloseDateSI = Class.create();
ExpectedCloseDateSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getStartDate : function (){

var start_date_from_client = this.getParameter(“sysparm_start_date”);
var new_expected_date = new GlideDateTime(start_date_from_client);
new_expected_date.addDays(5);
return new_expected_date;
},
type: ‘ExpectedCloseDateSI’
});

12. Problem Statement: Populate Email, Department, Contact, Company,location as per User value.

Client Script: On Change of User.

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == ”) {
return;
}

var name = g_form.getValue(‘user_name’);
var ga = new GlideAjax(‘ClientSideUtils’);
ga.addParam(‘sysparm_name’, ‘getUserDetails’);
ga.addParam(‘sysparm_user’, newValue);
ga.getXML(setUserDetails);


function setUserDetails(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute(“answer”);
var split = answer.split(‘#’);
g_form.setValue(’email’, split[0]);
g_form.setValue(‘department’, split[1]);
g_form.setValue(‘user_contact’, split[2]);
g_form.setValue(‘company’,split[3]);
g_form.setValue(‘region’,split[4]);
g_form.setValue(‘business_unit’,split[5]);
}

}

 

Server Side Script:

var ClientSideUtils = Class.create();
ClientSideUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getUserDetails: function() {
var usr = this.getParameter(‘sysparm_user’);
var gr = new GlideRecord(‘sys_user’);
gr.addQuery(‘sys_id’, usr);
gr.query();
if (gr.next()) {
var email = gr.email;
var dept = gr.department;
var mob = gr.mobile_phone;
var com=gr.company;
var loc=gr.location;
var bunit=gr.location.getDisplayValue()+” Business Unit”;
return email + “#” + dept + “#” + mob+”#”+com+”#”+loc+”#”+bunit;
}
},
type: ‘ClientSideUtils’
});

 

Client Scripting