Smile CDR v2024.05.PRE
On this page:

30.2.1Custom Matching Script


To make the $member-match operation more adaptive to different health providers, the System-to-System Data Exchange Module supports custom matching scripts that can be defined by clients in the form of JavaScript.

The function signature must match the following template, where the first parameter contains the memberPatient and coverageToMatch parameters to the $member-match operation, and the second parameter contains details about the request that is about to be processed.

function matchPatient(theMemberMatchRequest, theRequestDetails) {
		// your implementation goes here

The function may return Patient if a matching patient is found, or null otherwise. Note that if null is returned, then the system will perform the matching again using default matching.

Below is an example of the custom matching script, which fetches Coverage resources that have the same health plan Identifier as the coverageToMatch parameter to $member-match, and performs some validation on the referenced patient:

function matchPatient(theMemberMatchRequest, theRequestDetails){
	 // get the memberPatient and the coverageToMatch from the first parameter
    let memberPatient = theMemberMatchRequest.getPatient();
    let coverageToMatch = theMemberMatchRequest.getCoverage();
    // search for coverages that have the same identifier as coverageToMatch on
    // an accepted list of health plans
    var coverageList = ["",
    var coverageSystem = null;
    var coverageValue = null;
    for (let i = 0; i < coverageToMatch.identifier.length; i++) {
        if (coverageList.includes(coverageToMatch.identifier[i].system)) {
            coverageSystem = coverageToMatch.identifier[i].system;
            coverageValue = coverageToMatch.identifier[i].value;

    // return null if coverageToMatch does not fit to any of the health plans
    if (coverageSystem == null) {
        return null;

    // perform actual search
    var coverageSearchResult = Fhir
    .whereToken('identifier', coverageSystem, coverageValue)

    // get the patient ids from the resulting coverages
    patientIds = '';
    for (let i = 0; i < coverageSearchResult.length; i++) {[i].beneficiary.reference);
        patientIds = patientIds
    // return null if no patient reference was found
    if (patientIds == '') {
        return null;

    // search for the patient ids
    var patientSearchResult = Fhir
    .where('_id', patientIds)

    // perform validation on the patients
    let resultPatient = null;
    for (let i = 0; i < patientSearchResult.length; i++) {
        if (validatePatient(patientSearchResult[i], memberPatient)){
            resultPatient = patientSearchResult[i];

    return resultPatient;

// validate the patient by comparing gender with the input member patient,
// and return the first one that matches
function validatePatient(thePatient, theMemberPatient) {
		if (thePatient && theMemberPatient) {
			if (thePatient.gender && theMemberPatient.gender) {
				return thePatient.gender.toString().toLowerCase == theMemberPatient.gender.toString().toLowerCase;
			} else if (!thePatient.gender && !theMemberPatient.gender) {
				return true; // neither have a gender - we'll say they're the same
		return false;