Populating a field based on the value of another table?

Nani6
Mega Guru

Hi,

I am facing issue with the below business rule. Please assist me.

This is my code in demo instance:-

function ondisplay(current, g_scratchpad){

var ou= new glideRecord('u_test');

ou.addQuery();

ou.query();

while(ou.next()){

current.u_organization = ou.u_organization;

current.update();

}

}

My requirement is populating a field based on value of another table. Let me explain u clearly:- I have created a new custom table called "test" which contains following fields (Name, Email, Organization) and we have base table called "Users". Both the tables contains the user information, Now I want to populate Organization field of User table based on Test table. So I have written the above business rule, it is populating the organization field but the issue is it is fetching the value of last record of Test table.

I know that I am missing something in the above code, I have to include one more line comparing the records of both the tables (I mean I have to take Email field as Reference and compare values of both the tables, if match is found organization field of User table should get populated with the organization field value of Test table).

Please assist me.......

Thanks in Advance.

1 ACCEPTED SOLUTION

var ou= new GlideRecord('u_test');


ou.query();


while(ou.next())


{


var getUserData = new GlideRecord('sys_user');


getUserData.addQuery('email',ou.u_email);


getUserData.query();


if(getUserData.next())


{


ou.u_organization = getUserData.u_organization;


ou.update();


}


}



//change the field names as per your setup


View solution in original post

14 REPLIES 14

How is your table and user table related? Any common fields to connect them?


Thanks for Reply. Let me explain you clearly:-


User Table:- It contains majorly (fields) like Name, Email, Organization (reference field to company table). Here Organization field does not contains values, we have to populate this field based on Test table.


Test Table:- It contains majorly (fields) like Name, Email, Organization (reference field to Company table). Here Organization field contains values.



Here Email field is common in both the tables, depending upon this(email) field I want to populate Organization field of User table.


var ou= new GlideRecord('u_test');


ou.query();


while(ou.next())


{


var getUserData = new GlideRecord('sys_user');


getUserData.addQuery('email',ou.u_email);


getUserData.query();


if(getUserData.next())


{


ou.u_organization = getUserData.u_organization;


ou.update();


}


}



//change the field names as per your setup


It worked for me. Thank you very much Kalaiarasan.


Hi Kalaiarasan,


The above code worked for me but the issue I am facing is system performance, I mean when I click on user table it is taking lone time to display records and also when I click on particular user record it is taking long time to display the info.


I have used on display business rule and checked Query option. I want to schedule this business rule, I know we can do that using scheduled jobs but I see an script field in this should I copy this code in that field or have to write something else.



These issue might be very silly to u but I am new to scripting world. So I want to clarify the doubts.



Thanks in Advance.