How to update the Opened record from script inlcude via calling script include from UI action .

NamanChaturvedi
Tera Contributor

Hi
I have a requirement , say
I have a sample JSON object as array of objects with some key value pairs in it.
On the incident form I have created a UI action , on click of that button I triggered a Script include which checks if the current opened incident is matching any JSON Incident number. If match occurs I have to update the current opened incident record with the JSON key-values given. 

Attaching my UI action script and Script Include here:
Can anyone help me completing the script include to update the current opened record.


UI Action Script:

 

function checkAndUpdateIncidentDetails(){

	var incNumber = g_form.getValue('number');
	g_form.addInfoMessage("From UI action Incident is : "+incNumber);

	var ga = new GlideAjax('assignmentUIActionScriptInclude');
	ga.addParam('sysparm_name' , 'getPropertyAndUpdateRecord');
	ga.addParam('sysparm_incident_no' , incNumber);

	ga.getXML(myFuncForScriptInclude);

	function myFuncForScriptInclude(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		alert("Answer fetched is : "+answer);
	}
}

 

 

 

 

Script Include :

 
 

 

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

    getPropertyAndUpdateRecord: function() {
        var prop = gs.getProperty("json.property.assignment");
        var parsedProp = JSON.parse(prop);
		// gs.addInfoMessage(parsedProp);

		// get the values from UI action
		var currentFormIncident = this.getParameter('sysparm_incident_no');

        gs.addInfoMessage("Incident recieved from UI action in Script include is : "+currentFormIncident);
		
		count = 0;
		var inciFound = false;
        while (parsedProp.length > count) {

            var properties = parsedProp[count];
			// gs.addInfoMessage(properties);

            if (properties.incNumber == currentFormIncident) {
				
				gs.addInfoMessage("Inside if "+properties.incNumber);
                for (key in properties) {
                    gs.addInfoMessage("Key is : "+key);
                    // gs.addInfoMessage("\t value is : "+properties[key]);
					if(key !== "incNumber"){
						
						gr.description = properties[key];
					}
				}
				gr.update();
                // Object.values(properties);
				inciFound = true;
                break;
            }
            count++;
        }
        gs.print(count);
		return inciFound;
	},
	
	type: 'assignmentUIActionScriptInclude'
});

 








 

1 REPLY 1

Astik Thombare
Tera Sage

Hi @NamanChaturvedi  ,

 

Please check if below script include will be helpful to you or not .I have made some changes to it 

 

 

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

  getPropertyAndUpdateRecord: function() {
    var prop = gs.getProperty("json.property.assignment");
    var parsedProp = JSON.parse(prop);

    // get the values from UI action
    var currentFormIncident = this.getParameter('sysparm_incident_no');
    
    gs.addInfoMessage("Incident recieved from UI action in Script include is : "+currentFormIncident);

    var gr = new GlideRecord('incident'); // Replace 'incident' with your actual incident table
    gr.addQuery('number', currentFormIncident);
    gr.query();

    if (gr.next()) {
      var count = 0;
      var inciFound = false;
      while (parsedProp.length > count) {

        var properties = parsedProp[count];

        if (properties.incNumber == currentFormIncident) {
          gs.addInfoMessage("Inside if "+properties.incNumber);
          for (key in properties) {
            if(key !== "incNumber"){
              gr.setValue(key, properties[key]); // Update the field with corresponding key-value
            }
          }
          gr.update();
          inciFound = true;
          break;
        }
        count++;
      }
      return inciFound;
    } else {
      // Handle case where incident record is not found
      gs.addErrorMessage("Incident record not found for number: " + currentFormIncident);
      return false;
    }
  },
  
  type: 'assignmentUIActionScriptInclude'
});

 

 

If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                      By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

Astik