business rule not working as expected

Ak8977
Tera Expert

In the sys_user table I have a street field. I am writing a before business rule when a new user was created and we select his location then the street field map with the street field in cmn_location table.
I have written the following business rule. But it was not working.

 

var gr = new GlideRecord('cmn_location');
gr.addQuery('sys_id',current.location);
gr.query();

current.short_description = current.location;
while(gr.next()){
current.short_description = "inside if loop";
if(!gs.nil(gr.u_street.toString())){
current.street= gr.u_street.toString();
current.update();
}
}
current.update();

 

It was not going to the if loop.

1 ACCEPTED SOLUTION

Hello Aman,
we need to write async business rule,
because asset and CI are creating Simultaneously.

View solution in original post

12 REPLIES 12

Aman Kumar S
Kilo Patron

Hi @Ak8977 ,

Make sure you BR is created on Incident table and should be set as Before Insert/Update and condition as Location changes and location is not empty.

Please modify script as below:

var grLocation = new GlideRecord('cmn_location');
if(grLocation.get(current.getValue("location"))){
          if(grLocation.getValue("u_street") != ''){
                  current.street= grLocation.getValue("u_street");
          }
}

 

Best Regards
Aman Kumar

@Ak8977 

Did you try this?

Best Regards
Aman Kumar

Can you help me on this 

I have same kind of scenarion, but this logic was not working over there.
I have created an asset, it automatically creates the CI. In that CI I need to set the short description as some serial number. In this case it was not working.

created the before business rule on CI table


var gr = new GlideRecord('alm_asset');
gr.addQuery('sys_id',current.asset);
gr.query();
if(gr.next()){
gr.short_description = "inside if loop";
if(!gs.nil(gr.serial_number.toString())){
current.short_description = gr.serial_number;
}
}

Update as:

var gr = new GlideRecord('alm_asset');
gr.addQuery('sys_id',current.asset);
gr.query();
if(gr.next()){
if(gr.serial_number.toString() != ''){
          current.short_description = gr.serial_number.toString();
}
}

 

And to be very frank you dont even need script for this,

just go in the business rule and in the set values section

Select field description and same as operator and dot walk to the serial number field

that should do the trick.

Best Regards
Aman Kumar

Hello Aman,
we need to write async business rule,
because asset and CI are creating Simultaneously.