how to write onchange code

NiharikaM859103
Tera Contributor

If a user changes the caller in the incident form : Description should be the incident is
created by the user : caller full name , cmdb mandatory,state should be in progress.

5 REPLIES 5

Srikanth_9
Kilo Sage

Hi @NiharikaM859103,

 

Please refer to below code:

 

Srikanth_9_0-1779552301375.png

 

Srikanth_9_1-1779552337682.png

 

If the provided solution is useful/working, please Accept as Solution and hit the Helpful. 
 
Thanks & Regards,
Srikanth Akula.

vaishali231
Kilo Sage

hey @NiharikaM859103 
try this :

Table: Incident [incident]

Type: onChange

Field name: Caller 
Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.getReference('caller_id', function(caller) {
        var callerName = caller.name;
        g_form.setValue(
            'description',
            'Incident is created by the user : ' + callerName
        );
        g_form.setMandatory('cmdb_ci', true);
        g_form.setValue('state', '2'); // In Progress
    });

}

*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb




hey @NiharikaM859103 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread . This will help other readers find the solution more easily.

 

Thankyou & Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb








Tanushree Maiti
Tera Patron

Hi @NiharikaM859103 

 

Try this:

 

  • Table: Incident [incident]
  • Type: onChange
  • Field name: Caller [caller_id]
  • Script:

 

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

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

        return;

    }

g_form.getReference('caller_id', function(callerRecord) {

        if (callerRecord) {

            var descriptionText = 'The incident is created by the user: ' + callerRecord.name;

            g_form.setValue('description', descriptionText);

                g_form.setValue('state', '2');

            g_form.setMandatory('cmdb_ci', true);

        }

    });

}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti