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

This code should not be in business rule. Remove it from it.


If it is a one time activity, you may either use background script or on-demand scheduled job.


Thanks for the Reply. I want this code to be run after every 4 hours. So I should use a Scheduled job right?


Yup


Thanks Kalaiarasan. It worked fine now.


Thank you very much..........