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

Mohith Devatte
Tera Sage
Tera Sage

hello @Ak8977 ,

Please remove the occurrence's of current.update()  in your script  as in before BR there is no need to update your form .Also i have made few changes to your script please try with this one  

current.short_description = current.location.name;
var gr = new GlideRecord('cmn_location');
gr.addQuery('sys_id',current.location);
gr.query();
if(gr.next()){
current.short_description = "inside if loop";
if(!gs.nil(gr.u_street.toString())){
current.street= gr.u_street.toString();

}
}

Hope this helps 

Mark my answer correct if this helps you 

Thanks

hello Mohith,
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;
}
}

@Ak8977 is it before insert BR that you are trying to execute?

 

Also remove gr.short_description = "inside if loop"; line from the above code and try

 

if this works , Mark the appropriate answer as correct 

THanks

 

I tried but it was not working.
yes it is a before insert business rule