transform script target field

Praveen75
Tera Contributor

Hi, I have a scenario where if I get any value that doesn't exist in choice list (target table field is a choice list where label and field = ABC). The issue is how to set the target field value to the default value as 'ABC'. Please help me with this. Thanks.

If(source.u_company_name == 'ABC' || source.u_company_name == 'DEF'){

ignore =false;

} else {

target.u_company_name = 'ABC';  // not able to get this set

ignore =true;

}

1 ACCEPTED SOLUTION

Glad to know that my script worked.

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Tony Chatfield1
Kilo Patron

If the script is running in a field map then your need to return a result, not try to map the value directly. Try something like

var myResult;

if(source.u_company_name == 'ABC' || source.u_company_name == 'DEF'){
myResult = source.u_company_name;
} else {
myResult = 'ABC';
}

return myResult;

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onbefore transform script to set the value

If(source.u_company_name == 'ABC' || source.u_company_name == 'DEF'){

ignore =false;

} else {

target.u_company_name = 'ABC';  // are you setting correct choice value?

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi, Thanks for your reply. I tried on Before script and am giving correct choice value, where the target field is a choice list having the choice list as follows. When no match is found from the import data, need to set the company name to 'ABC' (which is not happening). Please help. Thanks.

 

Hi,

Did you check the script is getting inside the else part or not?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Praveen,

May be source company name coming as an object. Please try to convert that to string first and then match it like the below example.

var companyNanme=source.u_company_name+"";

If(companyNanme == 'ABC' || companyNanme== 'DEF'){

}

Regards,

Nayan