Client Script help, how to check CI for if value is empty

Jay Jay
Giga Expert

I have this script that sets values on INC/CHG/PROB forms if field is empty. By customer requirement I wish to change it so that it allways overwrite values in form, but only if field is not empty in the CI it gets value from.

 

To sum it up. CI gets selected on incident form, it checks if CI has value in Ass group, category and subcategory and if it does overwrite values on Incident form. How should i alter below script ?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
 return;
 }
 var ci = g_form.getReference('cmdb_ci', getci);
 //Type appropriate comment here, and begin script below
 
}

 

function getci(ci){
if (g_form.getValue('category')== '' && ci.category !='')
 {
 
 g_form.setValue('category', ci.category);
 }
 if (g_form.getValue('subcategory')== '' && ci.subcategory !='')
 {
 
 g_form.setValue('subcategory', ci.subcategory);
 }
 
 
 
 
 if (g_form.getValue('assignment_group')== '' && ci.assignment_group !='')
 {
 
 g_form.setValue('assignment_group', ci.assignment_group);
 }
 
 
 
 
}
 
1 ACCEPTED SOLUTION

You need to have if conditions to check whether category on ci is empty or not

Try the below script

 


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ci = g_form.getReference('cmdb_ci', getci);
    //Type appropriate comment here, and begin script below
}


function getci(ci){
    if (ci.category !='')
        {
        g_form.setValue('category', ci.category);
    }
    
    
    if (ci.subcategory !='')
        {
        g_form.setValue('subcategory', ci.subcategory);
    }
    
    
    if (ci.assignment_group !='')
        g_form.setValue('assignment_group', ci.assignment_group); {
        
    }
    
}

View solution in original post

6 REPLIES 6

Michael Fry1
Kilo Patron

You should be able to add something like:

if (newValue == '' || newValue == null) {
      return;

Thanx. Could you show me how to put it in above script ?

Actually, you already have the check if it's blank. Sorry didn't notice before. It's right here:

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

 

newValue === '' <-- checks if the newValue is blank, then do nothing (return)

But if i run the below script. And Assignment group, category and subcategory is filled out on the incident form and the CI' has empty fields on those 3, it still clears the field on the incident form to empty…

 

What is wrong ? 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
 return;
 }
 var ci = g_form.getReference('cmdb_ci', getci);
 //Type appropriate comment here, and begin script below
 
}

 

function getci(ci){
//if (g_form.getValue('category')== '' && ci.category !='')
 {
 
 g_form.setValue('category', ci.category);
 }
 
 

 {
 
 g_form.setValue('subcategory', ci.subcategory);
 
}
 
 
 
 
g_form.setValue('assignment_group', ci.assignment_group); {
    
 }
 
 
 
 
}