HoTwo snRecordPicker's depend on another

techies
Kilo Expert

Hi All,

I have two Sn-record-pickers as follows.

        <sn-record-picker field="company"   table="'core_company'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>

                <sn-record-picker field="location" table="'cmn_location'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>

I was wondering if it is at all possible to have the location filtered by the company, i.e default-query="company.nameSTARTSWITH{{data.company}}". I am stuck as to how I could approach this. Any help would be appreciated.

Thanks.

1 ACCEPTED SOLUTION

larstange
Mega Sage

Hi



You also need to add a listener to capture when the record pickers are updated



$scope.$on("field.change", function(evt, parms) {


        if (parms.field.name == 'company') {


                  c.data.company_dv = parms.displayValue;


                  c.data.company = parms.newValue;


        }


});


View solution in original post

8 REPLIES 8

Hi,


I'm not sure if you ever figured this out on your own, but since I struggled mightily with it and just figured it out, I thought I would post.


In my HTML body I have this


  <sn-record-picker field="department" table="'x_ahho_movable_med_location'"


                                              display-field="'display_value'"


                                              search-fields="'name,hospital'"


                                              page-size="100"


                                              default-query=c.data.dept_query


                                              ng-readonly=c.data.customer_found>


                  </sn-record-picker>


Then in my client controller I have a listener that sets the entire default-query. Not just the dynamic part.   So instead of default-query="'hospital=c.data.company_id'", I construct the entire default query in my client controller.   Hope that helps.


$scope.$on("field.change", function(evt, parms) {


  if (parms.field.name == 'company')


  c.data.company = parms.displayValue;


  c.data.company_id = parms.newValue;


        c.data.dept_query="hospital="+parms.newValue;


          console.log(c.data.company_id);


      c.server.update().then(function(response) {            


              spUtil.update($scope);  


      });


});


Hi,

 

Can you please help on the below code. I am trying similar approach but its not working

https://community.servicenow.com/community?id=community_question&sys_id=5ebda7d51b888dd0be4955fa234bcb72&anchor=answer_cf9eeb591b480d90d018c8ca234bcbe4

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

My comment on this blog post might be helpful : Reference Fields with the snRecordPicker Directive

 

Body HTML template

 <div class="row">
   <div class="col-md-6">
     <label class="control-label required">Division</label>
     <sn-record-picker id="division" field="division" table="'core_company'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" default-query="''"></sn-record-picker>
   </div>
   <div class="col-md-6">
     <label class="control-label required">Department</label>
     <sn-record-picker id="department" field="department" table="'cmn_department'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" default-query="'company=' + data.division_sys_id" sn-disabled="!data.division_sys_id"></sn-record-picker>
   </div>
 </div>

 

Client controller

function($scope) {
  var c = this;

  $scope.division = {
    displayValue: $scope.data.division_name,
    value: $scope.data.division_sys_id,
    name: 'division'
  };

  $scope.department = {
    displayValue: $scope.data.department_name,
    value: $scope.data.department_sys_id,
    name: 'department'
  };

  $scope.$on("field.change", function(evt, parms) {
    if (parms.field.name == 'division') {
      if (c.data.division_name != parms.newValue || !parms.newValue) {
        $scope.department.displayValue = '';
        $scope.department.value = '';
      }
      c.data.division_name = parms.displayValue;
      c.data.division_sys_id = parms.newValue;
    }
  });
}

@Sheldon Swift

Can you please check the below link where I am using similar code but some how its not working

 

https://community.servicenow.com/community?id=community_question&sys_id=5ebda7d51b888dd0be4955fa234bcb72&anchor=answer_cf9eeb591b480d90d018c8ca234bcbe4