How can I design a query using SOQL for designing medical records of patients?

129    Asked by Deepabhawana in Salesforce , Asked on May 20, 2024

I am a Salesforce developer currently working on a custom search functionality for a healthcare organization’s patient management system. The system includes a custom object called “Medical_Record__C” which stores detailed information about patients' medical history. How can I use THE SOQL contains keyword for constructing a query to search for patients who are taking “Aspirin” medicine for their treatment? 

Answered by himanshu

 In the context of Salesforce, you can construct a SOQL query by using the CONTAINS” keyword to search for the patient who is currently taking the medication “Aspirin” within their medic records by using the following approach:-

Firstly, you won't need to create an apex class called “PatientSearchController” –

Public class PatientSearchController {
    Public List searchPatientsWithAspirin() {
        // Perform the SOQL query to search for patients with Aspirin in their medical records
        List patientsWithAspirin = [
            SELECT Id, Name,
                (SELECT Id, Medication__c FROM Medical_Records__r WHERE CONTAINS(Medication__c, ‘Aspirin’))
            FROM Patient__c
        ];
        Return patientsWithAspirin;
    }
}

Next, you would be need to create a Visualforce page ‘PatientSearchPage’ for displaying the search results:-


   

       

           

               

               

               

           

       

   


Here is the java based coding structure given:-

Import java.util.ArrayList;
Import java.util.List;
Class Patient {
    Private String id;
    Private String name;
    Private List medicalRecords;
    Public Patient(String id, String name) {
        This.id = id;
        This.name = name;
        This.medicalRecords = new ArrayList<>();
    }
    Public void addMedicalRecord(MedicalRecord record) {
        medicalRecords.add(record);
    }
    Public List getMedicalRecords() {
        Return medicalRecords;
    }
    Public String getId() {
        Return id;
    }
    Public String getName() {
        Return name;
    }
}
Class MedicalRecord {
    Private String id;
    Private String medication;
    Public MedicalRecord(String id, String medication) {
        This.id = id;
        This.medication = medication;
    }
    Public String getId() {
        Return id;
    }
    Public String getMedication() {
        Return medication;
    }
}
Public class PatientSearch {
    Public List searchPatientsWithMedication(List patients, String medicationName) {
        List patientsWithMedication = new ArrayList<>();
        For (Patient patient : patients) {
            For (MedicalRecord record : patient.getMedicalRecords()) {
                If (record.getMedication().toLowerCase().contains(medicationName.toLowerCase())) {
                    patientsWithMedication.add(patient);
                    break; // Break to avoid adding the same patient multiple times if they have multiple matching records
                }
            }
        }
        Return patientsWithMedication;
    }
    Public static void main(String[] args) {
        // Create sample patients and medical records
        Patient patient1 = new Patient(“P001”, “John Doe”);
        Patient1.addMedicalRecord(new MedicalRecord(“MR001”, “Aspirin”));
        Patient1.addMedicalRecord(new MedicalRecord(“MR002”, “Ibuprofen”));
        Patient patient2 = new Patient(“P002”, “Jane Smith”);
        Patient2.addMedicalRecord(new MedicalRecord(“MR003”, “Paracetamol”));
        Patient2.addMedicalRecord(new MedicalRecord(“MR004”, “Aspirin”));
        List patients = new ArrayList<>();
        Patients.add(patient1);
        Patients.add(patient2);
        // Search for patients taking a specific medication
        PatientSearch patientSearch = new PatientSearch();
        List patientsTakingAspirin = patientSearch.searchPatientsWithMedication(patients, “Aspirin”);
        // Display the search results
        System.out.println(“Patients taking Aspirin:”);
        For (Patient patient : patientsTakingAspirin) {
            System.out.println(“Patient ID: “ + patient.getId() + “, Name: “ + patient.getName());
        }
    }
}
Here is the Python based coding structure given:-
Class Patient:
    Def __init__(self, id, name):
        Self.id = id
        Self.name = name
        Self.medical_records = []
    Def add_medical_record(self, medical_record):
        Self.medical_records.append(medical_record)
Class MedicalRecord:
    Def __init__(self, id, medication):
        Self.id = id
        Self.medication = medication
Class PatientSearch:
    Def search_patients_with_medication(self, patients, medication_name):
        Patients_with_medication = []
        For patient in patients:
            For record in patient.medical_records:
                If medication_name.lower() in record.medication.lower():
                    Patients_with_medication.append(patient)
                    Break # Break to avoid adding the same patient multiple times if they have multiple matching records
        Return patients_with_medication
# Create sample patients and medical records
Patient1 = Patient(“P001”, “John Doe”)
Patient1.add_medical_record(MedicalRecord(“MR001”, “Aspirin”))
Patient1.add_medical_record(MedicalRecord(“MR002”, “Ibuprofen”))
Patient2 = Patient(“P002”, “Jane Smith”)
Patient2.add_medical_record(MedicalRecord(“MR003”, “Paracetamol”))
Patient2.add_medical_record(MedicalRecord(“MR004”, “Aspirin”))
Patients = [patient1, patient2]
# Search for patients taking a specific medication
Patient_search = PatientSearch()
Patients_taking_aspirin = patient_search.search_patients_with_medication(patients, “Aspirin”)
# Display the search results
Print(“Patients taking Aspirin:”)
For patient in patients_taking_aspirin:
    Print(“Patient ID:”, patient.id, “, Name:”, patient.name)

Your Answer

Interviews

Parent Categories