Salesforce 플로 - ID 목록을 사용하여 레코드 쿼리

소개



Flow Builder에서 ID 컬렉션을 사용하여 레코드를 가져오는 것은 간단한 작업이어야 하지만 SF에는 여전히 이 기능이 없습니다. 때때로 우리는 흐름 루프 내에서 GET 요소를 사용하는 것 외에는 선택의 여지가 없기 때문에 이것은 모범 사례의 시행을 제한합니다.

이 문서의 아이디어는 이 제한을 처리하기 위한 Apex 작업의 간단한 구현을 보여주는 것입니다.

기본 구현



독자가 Invocable Methods/Variables 및 Flow에서 Apex를 호출하는 방법에 이미 익숙하다고 가정합니다.

즉, 문제를 해결할 수 있는 이 정점 클래스가 있습니다. 연락처 레코드를 가져오고 싶다고 가정해 봅시다.

public with sharing class FlowQueryHelper{


    @InvocableMethod(label = 'GET Contacts where Ids in Collection')
    public static List<Output> getContactsInColletion(List<Input> params){
        String ids = params[0].ids;
        List<Contact> contacts = [
                SELECT Id, Name 
                FROM Contact 
                WHERE Id IN :ids];

        Output output = new Output();
        output.contacts = contacts;

        return new List<Output>{ output };
    }  

    public class Input{
        @InvocableVariable(label = 'Text collection variable (Ids only)' required = true)
        public List<String> ids;
    }

    public class Output{
        @InvocableVariable(label = 'Contacts Collection Variable')
        public List<Contact> contacts;
    }
}


그것은 매력처럼 작동하지만 이 클래스를 다른 객체 유형과 함께 재사용하려는 경우에 대비하여 좀 더 일반적으로 만들 수 있습니다.

더 일반적인



클래스는 모든 사용자 지정 또는 표준 sObject에 대해 실행할 수 있어야 합니다. 이는 우리가 전달하는 모든 ID 컬렉션에 대해 실행하기 위해 sObject API 이름 및 필드를 인식하지 못한다는 것을 의미합니다.

API 이름



describe 메서드를 사용하여 Id에서 직접 API 이름을 가져올 수 있습니다. 자세한 내용은 here .

    private static String getObjectName(String objId){
        return ((Id)objId).getSObjectType().getDescribe().getName();
    }


필드



이번에도 잘 알려진 Schema 클래스와 describe 메서드를 사용합니다. 자세한 내용은 here .

    private static String getObjectFields(String objectName) {
        List<String> fields = new List<String>(Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().keySet());
        return String.join(fields, ',');
    }    


SOQL 쿼리



이제 SOQL 문이 있습니다. 레코드를 가져오기 위해 동적 SOQL을 사용할 것입니다. 자세한 내용은 here .

Database.query('SELECT ' + String.escapeSingleQuotes(objectFields) + ' FROM ' + String.escapeSingleQuotes(objectName) + ' WHERE Id IN :ids')


최종 버전




public with sharing class FlowQueryHelper{


    @InvocableMethod(label = 'GET Records where Ids in Collection')
    public static List<Output> getRecordsInColletion(List<Input> params){

        String objectName = getObjectName(params[0].ids[0]);
        String objectFields = getObjectFields(objectName);
        List<String> ids = params[0].ids;

        List<sObject> records = Database.query('SELECT ' + String.escapeSingleQuotes(objectFields) + ' FROM ' + String.escapeSingleQuotes(objectName) + ' WHERE Id IN :ids');

        Output output = new Output();
        output.records = records;

        return new List<Output>{ output };
    }

    private static String getObjectName(String objId){
        return ((Id)objId).getSObjectType().getDescribe().getName();
    }

    private static String getObjectFields(String objectName) {
        List<String> fields = new List<String>(Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().keySet());
        return String.join(fields, ',');
    }

    public class Input{
        @InvocableVariable(label = 'Text Collection variable (Ids only)' required = true)
        public List<String> ids;
    }

    public class Output{
        @InvocableVariable(label = 'Record Collection Variable')
        public List<sObject> records;
    }
}


그리고 그게 다야, 여러분. 아래 스크린샷은 계정 개체에 대한 예입니다.



결론



이것은 적어도 SF가 보다 최종적이고 견고한 솔루션을 제시할 때까지 트릭을 수행할 것입니다. 이 작업을 호출할 때 작업 출력에 대한 개체 유형을 선택하고 동일한 유형의 변수에 할당해야 한다는 점에 유의해야 합니다.

좋은 웹페이지 즐겨찾기