After Business rule not working

shun6
Giga Sage

Hi developer,

 

I have created after business rule, but it dosen't work.

When I set this business rule to "before" it works.

I'm not sure why this happenes. Any suggestions?

 

Detail:

  • When specific fields on "cmn_location" table are updated, populate that change info to "sn_grc_profile" table. 
  • There is annother Before Business rule which should be executed before this business rule. (update fields within cmn_location)

 

When: after, update

Script: 

(function executeRule(current, previous /*null when async*/ ) {
	var facilityId = current.sys_id;
    var grFacilityEntity = new GlideRecord('sn_grc_profile');
    grFacilityEntity.addQuery('cmn_location', facilityId);
    grFacilityEntity.query();

    while (grFacilityEntity.next()) {
        grFacilityEntity.setValue('name', current.name);
        grFacilityEntity.setValue('u_country', current.u_state.parent);
        grFacilityEntity.setValue('u_business_area', current.u_business_area);
        grFacilityEntity.setValue('u_department', current.u_business_area.u_department);
        grFacilityEntity.setValue('u_company', current.u_business_area.company);
        grFacilityEntity.update();
    }

})(current, previous);

 

Thanks.

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@shun6 Can you try the following and see if it works.

 

(function executeRule(current, previous /*null when async*/ ) {
	var facilityId = current.sys_id.toString();
    var grFacilityEntity = new GlideRecord('sn_grc_profile');
    grFacilityEntity.addQuery('cmn_location', facilityId);
    grFacilityEntity.query();

    while (grFacilityEntity.next()) {
        grFacilityEntity.setValue('name', current.name.toString());
        grFacilityEntity.setValue('u_country', current.u_state.parent.toString());
        grFacilityEntity.setValue('u_business_area', current.u_business_area.toString());
        grFacilityEntity.setValue('u_department', current.u_business_area.u_department.toString());
        grFacilityEntity.setValue('u_company', current.u_business_area.company.toString());
        grFacilityEntity.update();
    }

})(current, previous);

shun6
Giga Sage

@Sandeep Rajput 

Thank you for your reply.

I added ".toStoring" but it dosen't work.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

What is the condition of the BR? Script seems okay nothing standing out on why it runs before but not after.

Also is there any other script that runs on this transaction that might be cancelling further BRs? Like something that has setWorkflow(false)?

-Anurag

@Anurag Tripathi 

I'm not using condition.

 

There is a Before Business rule using "setAbortAction" for specific condition.

Though this if condition evaluate to false and following update actions have been done, After Business rule did't work. 

 

 

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

 var gr = new GlideRecord('cmn_location');
    gr.addQuery('u_name', current.u_name);
    gr.addQuery('company', current.company);
    gr.setLimit(1);
    gr.query();
    
    if (gr.next()) {
        gs.addErrorMessage(gs.getMessage("Duplicate Error of Facility Name"));
        current.setAbortAction(true);
    }

	var facilityName = current.u_name;
	var companyCode = current.company.u_company_code;
	current.name = companyCode + "_" + facilityName;
})(current, previous);