Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Access variables of record producer from Business Rule

abirakundu23
Giga Sage

Hi All/ @Ankur Bawiskar ,

We are trying to access record producer varibles value using Business Rule. But we are not able to get the values.

Please guide me. Async/Update BR. We submit the Record Producer after that record is creted respective table. We wrote BR of that table.

(function executeRule(current, previous /*null when async*/) {
    
gs.info('ITP HR service ' + current.hr_service);
    
    var transfer_person = current.variables.employee_name;
    gs.log("Abira transfer for " +transfer_person);
    var position = current.variables.new_position;
    gs.log("Abira Position " +position);
    
    var effecdate = current.variables.effective_date;
    gs.log("Abira date " +effecdate);
    var country = current.variables.transfered_position_country;
    gs.log("Abira country " +country);
    var loc = current.variables.transfered_position_location;
    gs.log("Abira location " +loc);

})(current, previous);

1 ACCEPTED SOLUTION

So the BR would run when the record is updated and the HR Service is Matching your service

1) in your BR you cannot use producer object; you need to use current

2) gs.log() won't work in scoped application and your script will break so use gs.info()

(function executeRule(current, previous /*null when async*/) {
    
gs.info('ITP HR service ' + current.hr_service);
    
    var transfer_person = current.variables.employee_name;
    gs.info("Abira transfer for " +transfer_person);
    var position = current.variables.new_position;
    gs.info("Abira Position " +position);
    
    var effecdate = current.variables.effective_date;
    gs.info("Abira date " +effecdate);
    var country = current.variables.transfered_position_country;
    gs.info("Abira country " +country);
    var loc = current.variables.transfered_position_location;
    gs.info("Abira location " +loc);
    
        var req_body='{'+
        '"effectivedate":"'+effecdate+'",'+
        '"trapercountry":"'+country+'",'+
        '"location":"'+loc+'",'+
        '"Position":"'+position+'",'+
        '}';
gs.info('ITP payload details'+req_body);

})(current, previous);

Regards
Ankur

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

View solution in original post

18 REPLIES 18

Hi,

did you check data for variable is present in the question_answer table for those variables and that record?

If yes then it should work fine

Regards
Ankur

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

Hi Ankur,

Variables is present in the question_answer table for those variables. Still logs not comes up for variable using BR. Could you please guide ? I want to send payload after fetching variables value.

Hi,

share your BR configuration screenshot

BR is on which table?

Did you check as admin?

Regards
Ankur

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

Hi Ankur,

(function executeRule(current, previous /*null when async*/) {
    
gs.info('ITP HR service ' + current.hr_service);
    
    var transfer_person = producer.variables.employee_name;
    gs.log("Abira transfer for " +transfer_person);
    var position = producer.variables.new_position;
    gs.log("Abira Position " +position);
    
    var effecdate = producer.variables.effective_date;
    gs.log("Abira date " +effecdate);
    var country = producer.variables.transfered_position_country;
    gs.log("Abira country " +country);
    var loc = producer.variables.transfered_position_location;
    gs.log("Abira location " +loc);
    
        var req_body='{'+
        '"effectivedate":"'+effecdate+'",'+
        '"trapercountry":"'+country+'",'+
        '"location":"'+loc+'",'+
        '"Position":"'+position+'",'+
        '}';
gs.info('ITP payload details'+req_body);

})(current, previous);

BR is on  "sn_hr_core_case_global_mobility" table.
    

Hi,

BR configuration screenshot i.e. when to run etc

is the record producer and it's variable also in same scope as that of the BR

Regards
Ankur

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