多选选项列表与 apex 中文本格式字段的比较

问题描述 投票:0回答:2

我有一个对象,其中包含此格式的记录数,名称为文本格式 -

Id  Name
 1  A,B,C
 2  A,B
 3  A
 4  B
 5  A,C
 6  A,D

我有一个多选选项列表,其值为:

A
B
C
D

因此,如果我在选项列表中选择 B 和 C,那么我必须拥有名称由 B 或 C 或 (B 和 C) 组成的表中的记录,即在这种情况下,它应该显示 4 条记录:

1 A,B,C
2 A,B
4 B
5 A,C

我正在使用带有 IN 和 INCLUDES 关键字的 SQL 查询,但我无法实现此功能。

salesforce apex soql
2个回答
0
投票

这取决于您使用 soql 有多少选项。我可以看到它的一种方式是:

Object__c myObject = new Object__c();
object.Name = 'A,B,D';
object.Multi_Select_Name__c = 'A;E';
insert object;
String query = 'select Id from Object__c';

//now we want to do a multi picklist, which is a String separated by ';'
List<String> nameSelections = object.Multi_Select_Name__c.split(';');
if(nameSelections.size() > 0) {
    query += ' where ';
    for(Integer i = 0; i < nameSelections.size(); i++) {
        nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
    }
    query += String.join(nameSelections, ' or ');
}
List<Object__c> objects = (List<Object__c)database.query(query);

这将创建一个如下查询:

'select Id from Object__c where Name like '%A%' or Name like '%E%'

选择名称包含 A 或 E 的所有 Object__c,从多选生成。

可能存在语法错误,我只是很快写了这个:)我会仔细检查自己。

编辑: 此代码用于构建查询

String query = 'select Id from Object__c';
String multiSelect = 'A;E';
List<String> nameSelections = multiSelect.split(';');
if(nameSelections.size() > 0 ){
    query += ' where ';
    for(Integer i = 0; i < nameSelections.size(); i++) {
        nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
    }
    query += String.join(nameSelections, ' or ');
}
system.debug(LoggingLevel.INFO, query);

0
投票

有没有办法在 salesforce apex 中比较和合并 2 行

公共类 DynamicTextMerger {

// Method to convert rich text into plain text (to compare with Task object description)
public static String convertRichTextToPlain(String richText) {
    // Remove HTML tags to convert rich text to plain text (basic implementation)
    String plainText = richText.replaceAll('<[^>]*>', '');
    System.debug('Converted Rich Text to Plain Text: ' + plainText);
    return plainText;
}

// Helper method to split text into words while retaining spaces
public static List<String> splitIntoWordsWithSpaces(String text) {
    // Ensure the input is not null
    if (text == null) {
        System.debug('Input text is null.');
        return new List<String>();
    }

    List<String> wordsWithSpaces = new List<String>();
    Pattern regex = Pattern.compile('(\\S+|\\s+)'); // Match words or spaces
    Matcher matcher = regex.matcher(text);

    while (matcher.find()) {
        wordsWithSpaces.add(matcher.group(0));
    }

    System.debug('Split Text into Words with Spaces: ' + wordsWithSpaces);
    return wordsWithSpaces;
}

// Method to format changes by applying <b> tags to new or changed words
public static String formatChanges(List<String> taskWords, List<String> richTextWords) {
    List<String> formattedWords = new List<String>();
    Integer i = 0;

    // Iterate over task words
    for (String taskWord : taskWords) {
        if (i < richTextWords.size() && taskWord == richTextWords[i]) {
            // Unchanged word, add as is
            formattedWords.add(richTextWords[i]);
        } else {
            // New or changed word, apply formatting
            formattedWords.add('<b>' + taskWord + '</b>');
        }

        // Move to the next rich text word if they match
        if (i < richTextWords.size() && taskWord == richTextWords[i]) {
            i++;
        }
    }

    // Join formatted words, preserving spaces
    String formattedText = String.join(formattedWords, '');
    System.debug('Formatted Text with Changes (excluding removed words): ' + formattedText);
    return formattedText;
}

// Main method to merge the descriptions with formatting
public static String mergeTextWithFormatting(String taskPlainText, String customRichText) {
    // Step 1: Convert the rich text to plain text
    String existingPlainText = convertRichTextToPlain(customRichText);

    // Step 2: Split both texts into words while retaining spaces
    List<String> taskWords = splitIntoWordsWithSpaces(taskPlainText);
    List<String> richTextWords = splitIntoWordsWithSpaces(existingPlainText);

    // Step 3: Format and merge changes
    String mergedRichText = formatChanges(taskWords, richTextWords);

    return mergedRichText;
}

// Example demonstration method
public static void demonstrateTextMerge() {
    // Example plain text from Task object
    String taskPlainText = 'This task is now updated and followed with client regarding billing details';

    // Example rich text from Custom Object
    String customRichText = 'Hi, this task is created to <b>follow on billing</b> and manage tasks.';

    // Merge the plain text changes into the existing rich text
    String mergedRichText = mergeTextWithFormatting(taskPlainText, customRichText);

    // Log the final merged rich text
    System.debug('Final Merged Rich Text: ' + mergedRichText);

    // Assuming Custom Object has a Description__c field to store the merged content
    // Uncomment the following lines to update your custom object
    /*
    CustomObject__c customObject = [SELECT Id, Description__c FROM CustomObject__c WHERE Id = :yourCustomObjectId LIMIT 1];
    customObject.Description__c = mergedRichText;
    update customObject;
    */
}

}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.