how to populate company field in sc task using business rule servicenow

Ashok
Tera Contributor

Hi All,

Please suggest how to achieve the below requirement:

 

how to populate company field in sc task using business rule servicenow

 

I have tried below script but not works:

 

(function executeRule(current, previous /*null when async*/ ) {

// Get current record Requested For user
var requestedFor = current.requested_for;

// Get Requested For user details
var getUserCompany = new GlideRecord("sys_user");
getUserCompany.get(requestedFor);

// Store Requested For Comapnay
var userCompany = getUserCompany.company;

// Update RITM Company field
current.company = userCompany;
current.update();

// Update SCTASk Company field
var getCatalogTask = new GlideRecord("sc_task");
getCatalogTask.addQuery("request_item", current.sys_id);
getCatalogTask.query();

while (getCatalogTask.next()) {

getCatalogTask.company = userCompany;
getCatalogTask.update();
}

})(current, previous);

3 REPLIES 3

J Siva
Tera Sage

Hi @ Ashok 

May I know how you are managing the service requests?

Using workflow or flow designer??

 

If it's a flow designer, then you can achieve this within the flow. It's way easier than the business rule.

Write on before business rule on sc_task table.

// Fetch user company.
var user = new GlideRecord('sys_user');
user.get(current.request_item.requested_for);
var company _id = user.company;
current.company = company_id;//set company value in the script task record 

Regards,

Siva

Sandeep Rajput
Tera Patron
Tera Patron

@ Ashok First of all never use current.update() in a business rule, it can bring a lot of unwanted problems for you. Secondly you can achieve this using an onBefore insert/update business rule on sc_task table with following code.

current.company = current.request_item.request.requested_for.company+'';

 

You just need this single line to update the company field on your sc_task record.

 

Hope this helps.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@ Ashok 

you can use before insert BR for this on sc_task with proper conditions and use Actions Tab and no scripting required

AnkurBawiskar_0-1744602704720.png

something like this

request company on sctask.gif

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

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