OnChange client script - API

tsoct
Tera Guru

Hello all,
The API retrieved the following value, and the user can select Contact Name from the drop down.

 

{"value":[{"UniqueID":"369851","ContactName":"JESSICA","Relation":"Vendor"},{"UniqueID":"789456","ContactName":"ANGELICA","Relation":"Vendor"},{"UniqueID":"951753","ContactName":"DAVID","Relation":"Vendor"},{"UniqueID":"201698","ContactName":"MARGRET","Relation":"Client"}]}

 

And my question, how can the onChange script be scripted if the Relation (variable type = string) should be updated depending on the Contact Name (variable type = Select box)?

 

The current onload script:

function onLoad() {
    var clientindex = g_service_catalog.parent.getValue('client_index');

    var gaFields = new GlideAjax('CLIENTdata');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_id', clientindex);
    gaFields.getXML( showData);

    function showData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('Answer: ' + answer);
        var obj = JSON.parse(answer);
        
        for (var i = 0; i < obj.value.length; i++) {
            g_form.addOption('contact_name', obj.value[i].ContactName, obj.value[i].ContactName);
            g_form.setValue('relationship', obj.value[i].Relation);
        }
    }
}

 

 

 

 

1 ACCEPTED SOLUTION

Hello @Sanjay191 ,

Edit: I found the issue.

Thanks

 

The corrected script:

 

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

    var clientindex = g_service_catalog.parent.getValue('client_index');

    var gaFields = new GlideAjax('CLIENTdata');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_id', clientindex);
    gaFields.getXML( showData);

    function showData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('Answer: ' + answer);
        var obj = JSON.parse(answer); 
        
        for (var i = 0; i < obj.value.length; i++) {
		if (obj[i].ContactName === newValue)
            g_form.setValue('relationship', obj.value[i].Relation);
        }
    }
}

 

 

 

View solution in original post

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Not sure what you're trying to do.

Seems like you have a script include named CLIENTdata that returns a json Contact Name in your question.

This script include in called onLoad so the select box will be filled when the form is loaded. Not sure why you have g_form.setValue('relationship', obj.value[i].Relation); in your onLoad in the for loop.

 

If you want to change the value of field relationship when field contact_name is changed, create a onChange client script on variable contact_name. Use the "newValue" to get the selected value. Call the CLIENTdata to get the json once again in the onChange and get the matching Relation value.

 

EDIT:Probably would be better to add a function in Script Include to return only the corresponding relationship value given "newValue" of contact_name as an argument to the function.

Hello @Hitoshi Ozawa The API is returning all of the values. Do you have a sample script that retrieves only the Relation based on the specified Contact Name? For example, when David is selected, Relation should appear as a vendor.

Thanks

Sanjay191
Tera Sage
 
    // Sample data from your example
    var contacts = [
        { "UniqueID": "369851", "ContactName": "JESSICA", "Relation": "Vendor" },
        { "UniqueID": "789456", "ContactName": "ANGELICA", "Relation": "Vendor" },
        { "UniqueID": "951753", "ContactName": "DAVID", "Relation": "Vendor" },
        { "UniqueID": "201698", "ContactName": "MARGRET", "Relation": "Client" }
    ];

    // Loop through each contact and update the Relation based on ContactName
    contacts.forEach(function(contact) {
       

        gs.info("ContactName::"+contact.ContactName);
        gs.info("relationName::"+  contact.Relation);
        if(ContactName == 'JESSICA'){
            g_form.setValue("backendvalue of relationname on table ",contact.Relation);
        }
        else if(ContactName == 'ANGELICA'){
      g_form.setValue("backendvalue of relationname on table",contact.Relation )
        }


    });


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You



Hello @Sanjay191 ,

Edit: I found the issue.

Thanks

 

The corrected script:

 

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

    var clientindex = g_service_catalog.parent.getValue('client_index');

    var gaFields = new GlideAjax('CLIENTdata');
    gaFields.addParam('sysparm_name', 'getDetails');
    gaFields.addParam('sysparm_id', clientindex);
    gaFields.getXML( showData);

    function showData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert('Answer: ' + answer);
        var obj = JSON.parse(answer); 
        
        for (var i = 0; i < obj.value.length; i++) {
		if (obj[i].ContactName === newValue)
            g_form.setValue('relationship', obj.value[i].Relation);
        }
    }
}