ソースフィールドを使用してassigned_toが参照しているユーザーのみ挿入できるようにしたい。

izanacart111
Tera Contributor

problem:
importする際にassigned_toの参照していないユーザーも挿入できるような状態になっている。

reference:
sys_userの中で以下の条件で当てはまったuserである。

rolls=itil^ORroles=a.user^ORroles=b.user^EQ

試したコード:
ソーススクリプトでproblemを解決するために作成したコードは以下である。
結果は、どのユーザーでもrejectされskipする

answer = (function transformEntry(source) {

                 var table = new GlideRecord('sys_user');

table.addEncodedQuery('rolls=itil^ORroles=a.user^ORroles=b.user^EQ');

                 table.query();

                 if (table.next()) {

                                                  return source.u_assigned_to;

                 }

})(source

分かる方がいたら教えてほしいです。
お願い致します。

1 ACCEPTED SOLUTION

@izanacart111  why are you returning assigned_to,  I think you should return variable table.

answer = (function transformEntry(source) {

                 var table = source.u_assigned_to;

                 if(table){

                                  var userRoles = new GlideRecord('sys_user');

                 userRoles.addEncodedQuery('rolesINitil,a.user,b.user^sys_id='+table);

                                  userRoles.query();

                                  if (userRoles.next()) {

                                                  return table;

                                  }

                 }

})(source);  

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog

View solution in original post

3 REPLIES 3

Anand Kumar P
Giga Patron
Giga Patron

Hi @izanacart111,

Try below script 

answer = (function transformEntry(source) {
var assignTo = source.u_assign_to;
if (assignTo) {
var userRoles = new GlideRecord('sys_user');
userRoles.addEncodedQuery('rolesINitil,a.user,b.user^sys_id=' + assignTo);
userRoles.query();
if (userRoles.next()) {
return assignTo;
}
}
})(source);

Thanks,

Anand

 dear Anand Kumar P

まずはアドバイスありがとう。
実際にAnandのコードを試してみた。
コード以下である

answer = (function transformEntry(source) {

                 var table = source.u_assigned_to;

                 if(table){

                                  var userRoles = new GlideRecord('sys_user');

                 userRoles.addEncodedQuery('rolesINitil,a.user,b.user^sys_id='+table);

                                  userRoles.query();

                                  if (userRoles.next()) {

                                                  return assigned_to;

                                  }

                 }

})(source);  


結果は、スキップになってしまった。
エラーメッセージは以下である
Reference field value for task.assigned_to rejected: undefined

@izanacart111  why are you returning assigned_to,  I think you should return variable table.

answer = (function transformEntry(source) {

                 var table = source.u_assigned_to;

                 if(table){

                                  var userRoles = new GlideRecord('sys_user');

                 userRoles.addEncodedQuery('rolesINitil,a.user,b.user^sys_id='+table);

                                  userRoles.query();

                                  if (userRoles.next()) {

                                                  return table;

                                  }

                 }

})(source);  

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog