public class ComboBox_Con {
    private Object targetField;
    public String labelField { get; set; }
    public String valueField { get; set; }
    public String activeField { get; set; }
    public String sObjVal { get; set; }
    public Integer randomJsIden { get; set; }

    public ComboBox_Con() {
        randomJsIden = getRandomNumber(1000000);
    }

    /*
     *Setter method to set the targetField
     ***/
    public void setTargetField(Object targetField) {
        if (targetField == null) {
            this.targetField = targetField;
        }
    }

    /*
     *Random number generator to change the js function name if multiple components us
     ***/
    private Integer getRandomNumber(Integer size) {
        Double d = math.random() * size;
        return d.intValue();
    }

    /*
     *This method queries data according to the passed parameters
     ***/
    @RemoteAction
    public static List<Map<String, String>> getData(
        String sObjVal,
        String labelField,
        String valueField,
        String param,
        String filterField,
        String filterField2,
        String filterField3,
        String match,
        String match2,
        String match3,
        String activeField,
        String orderByField
    ) {
        param = String.escapeSingleQuotes(param);
        match = String.escapeSingleQuotes(match);
        match2 = String.escapeSingleQuotes(match2);
        match3 = String.escapeSingleQuotes(match3);

        String query =
            'select ' +
            valueField +
            ',' +
            labelField +
            ' from ' +
            sObjVal +
            ' where ' +
            labelField +
            ' like \'%' +
            param +
            '%\'';
        if (match != null && match != '') {
            query += ' and ' + filterField + '=\'' + match + '\'';
        }
        if (match2 != null && match2 != '') {
            query += ' and ' + filterField2 + '=\'' + match2 + '\'';
        }
        if (match3 != null && match3 != '') {
            query += ' and ' + filterField3 + '=\'' + match3 + '\'';
        }
        if (activeField != null && activeField != '') {
            query += ' and ' + activeField + '=true';
        }
        if (orderByField != null && orderByField != '') {
            query += ' ORDER BY ' + orderByField;
        }
        system.debug('dynamic query--> ' + query);
        List<SObject> results = Database.query(query);
        Map<String, String> uniqueValues = new Map<String, String>();
        List<Map<String, String>> returnResults = new List<Map<String, String>>();

        for (SObject o : results) {
            String label;
            if (labelField.contains('.')) {
                String objName = labelField.substringBefore('.');
                String fieldName = labelField.substringAfter('.');
                label = (String) o.getSObject(objName).get(fieldName);
            } else {
                label = (String) o.get(labelField);
            }
            String value;
            if (valueField.contains('.')) {
                String objName = valueField.substringBefore('.');
                String fieldName = valueField.substringAfter('.');
                value = (String) o.getSObject(objName).get(fieldName);
            } else {
                value = (String) o.get(valueField);
            }
            uniqueValues.put(value, label);
        }

        Set<String> values = uniqueValues.keySet();
        for (String v : values) {
            Map<String, String> record = new Map<String, String>{ 'label' => uniqueValues.get(v), 'value' => v };
            returnResults.add(record);
        }

        return returnResults;
    }
}
