how to auto populate the cost center field value from caller/user to incident form using the business rule

Naresh44
Giga Contributor

Hi all,

I tried auto populating the 'cost center' field value from users table,  using the getReference method in business rule for incident form, but it didn't worked at all.

Please help me in retrieving the value of the cost center field value in the incident form

 

1 ACCEPTED SOLUTION

Hi Naresh,

 

As suggested by Ankur above won't work in business rule as g_form.getReference() are client specific side options to be used.

Try using below business rule that runs After Insert on required table & make sure you have correct field name passed.

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

    // Add your code here
    current.location = current.caller_id.location;

    current.cost_center = current.caller_id.cost_center;

    current.state = current.caller_id.state;

    current.city = current.caller_id.city;

    current.country = current.caller_id.country;
	
	current.update();

})(current, previous);

View solution in original post

25 REPLIES 25

I agree with @Jaspal Singh , if you have a reference to the User record with Caller, why not just add the fields from the Caller(+) to the form, rather than creating redundant fields and attempting to script them. You'll want to make them read only on the form/list, if a user changes them on the Incident form, it will change them on the user record.

It's best practice to navigate users to the pop-up for the Caller reference field to view these fields, but if necessary to show them on the incident form the above method can be used.

Thanks for the confirmation.

Also, can you confirm if state, location,street, cost-center exists on table on which Business rule is written. If so, from your statement I see you are having Business Rule on incident table which I doubt have street, locatin, state, etc. unless custom created.

If custon created is should be

current.u_state=current.caller_id.state; & so on

Else, you can try below.

 

This will get in value for required fields you want from User table without having Client Scripts or Business Rule to make it work.

Thank you for the response.

The BR code is  working for every caller except for abraham lincoln -- only country field value is not populating.

Thanks & Regards

Naresh

If you choose to script this solution, please make sure you're not using current.update() in an After Business Rule.

Per ServiceNow:  

Do not use current.update() in a Business Rule script. The update() method triggers Business Rules to run on the same table for insert and update operations, potentially leading to a Business Rule calling itself over and over. Changes made in before Business Rules are automatically saved when all before Business Rules are complete, and after Business Rules are best used for updating related, not current, objects. When a recursive Business Rule is detected, ServiceNow stops it and logs the error in the system log. However, this behavior may cause system performance issues and is never necessary.

https://developer.servicenow.com/dev.do#!/guide/newyork/now-platform/tpb-guide/business_rules_techni...

Thank you for the reponse.

The BR code is working fine for every caller except for the ABRAHAM LINCOLN --- i.e country field value is not populating on the incident form.

May I know why?

Thanks & Regards

Naresh Uppu