Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How populate string if short Description contains particular value then value populate anothe field

AJAYKUMAR G
Tera Contributor

If short Description : Registered as y_6778 journey 

Field name : Key (string)

 

My question is I need populate the "y_6778" value in key field. How to approach.

1 ACCEPTED SOLUTION

Sohail Khilji
Kilo Patron

Hi @AJAYKUMAR G ,

 

As you have not mentioned that when do you need this to take place , i assume that you need it when the record is updated or create / if its on change of field on form then you can use onchange client scipt or if on insert or update you need to use business rule.

 

for client script :

 

function onChangeShortDescription(control, oldValue, newValue, isLoading) {
  
    if (newValue && newValue.startsWith("y_")) {
        var match = newValue.match(/y_\d+/);
        if (match) {
            var extractedValue = match[0];
            g_form.setValue('key', extractedValue);
        }
    }
}

 

if its business rule :

 

(function executeRule(current, previous /*null when async*/) {
   
    if (current.short_description.changes() && current.short_description.startsWith("y_")) {
 
        var match = current.short_description.match(/y_\d+/);
        if (match) {
            var extractedValue = match[0];
            // Set the extracted value into the key field
            current.key = extractedValue;
        }
    }

})(current, previous);

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

7 REPLIES 7

Sohail Khilji
Kilo Patron

Hi @AJAYKUMAR G ,

 

As you have not mentioned that when do you need this to take place , i assume that you need it when the record is updated or create / if its on change of field on form then you can use onchange client scipt or if on insert or update you need to use business rule.

 

for client script :

 

function onChangeShortDescription(control, oldValue, newValue, isLoading) {
  
    if (newValue && newValue.startsWith("y_")) {
        var match = newValue.match(/y_\d+/);
        if (match) {
            var extractedValue = match[0];
            g_form.setValue('key', extractedValue);
        }
    }
}

 

if its business rule :

 

(function executeRule(current, previous /*null when async*/) {
   
    if (current.short_description.changes() && current.short_description.startsWith("y_")) {
 
        var match = current.short_description.match(/y_\d+/);
        if (match) {
            var extractedValue = match[0];
            // Set the extracted value into the key field
            current.key = extractedValue;
        }
    }

})(current, previous);

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Short description not starts with "y_" . short decription contains any where "y_".

 

Robbie
Kilo Patron
Kilo Patron

Hi @AJAYKUMAR G,

 

So my response is not lost in the thread.

This is easily achieved with an adapted onChange Client Script to look for a string of 'y_'.

See the below-updated script and screenshot for guidance.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

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

   var valToComapre = newValue;
   var containsVal = valToComapre.indexOf('y_');
   if(containsVal > -1){
		var result = newValue.slice(containsVal).trim().split(' ')[0];
		g_form.setValue('your_field_name', result);
   }
   
}