I have a requirement to reset the form fields to empty when UI Action 'Reset to Draft'
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
10-03-2025 02:38 AM
I have a requirement to reset the form fields to empty when UI Action 'Reset to Draft' .There is a UI Action button in the demand table that appears when the state changes to submitted or screening or qualified and for the rest of the stage when i click on reset to draft UI action all the form fields except the number has to set to empty.Please help me to achieve this
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
10-03-2025 02:49 AM
You can create UI Action with following
Client - Checked
Onclick - resetFields()
condition - current.state == "submitted" || current.state == "screening" || current.state == "qualified"
// Update the above state condition based on the backed value of the State
Script
function resetFields(){
  for(var i = 0; x < g_form.elements.length; i++){
      if(g_form.elements[i].fieldName != "number"){
        g_form.setValue(g_form.elements[i].fieldName, "");
      }
  }
}
					
				
			
			
				Palani
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
10-03-2025 02:50 AM
In Condition: (current.state == 'submitted' || current.state == 'screening' || current.state == 'qualified')
    // Set the state to Draft (replace with correct value or constant)
    current.state = 'draft';
    // Store the number since we don't want to clear it
    var number = current.number;
    // Clear all fields except number
    var fields = current.getFields();
    for (var i = 0; i < fields.size(); i++) {
        var fieldName = fields.get(i).getName();
        // Skip 'number' and some important system fields
        if (fieldName != 'number' && fieldName != 'sys_id' && fieldName != 'sys_created_on' &&
            fieldName != 'sys_created_by' && fieldName != 'sys_updated_on' && fieldName != 'sys_updated_by') {
            try {
                current.setValue(fieldName, '');
            } catch (e) {
                // If it's a reference or complex field, continue without error
                gs.info('Could not clear field: ' + fieldName + ' - ' + e.message);
            }
        }
    }
    // Restore number (if needed — usually not necessary, just precaution)
    current.number = number;
    // Update the record
    current.update();Or  Recommended Alternative Approach (Safer)
Instead of looping through all fields, explicitly clear only those you know should be reset:
// List of fields to clear
var fieldsToClear = ['short_description', 'description', 'priority', 'requested_by', 'business_case']; // etc.
fieldsToClear.forEach(function(field) {
    if (current.isValidField(field)) {
        current.setValue(field, '');
    }
});
Thanks,
Vignesh 
"If this solution resolves your issue, kindly mark it as correct."
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
10-03-2025 03:07 AM
you can use a server side UI action for this
UI Action
Condition:
['2','3','-4'].indexOf(current.state.toSring()) > -1
Script:
clearFields();
function clearFields() {
    try {
        var fields = current.getFields();
        for (var i = 0; i < fields.size(); i++) {
            var field = fields.get(i);
            var descriptor = field.getED();
            var fieldName = descriptor.getName();
            current[fieldName] = '';
        }
        current.update();
    } catch (ex) {
        gs.info(ex);
    }
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
