I'm getting a System.CalloutException: You have uncommitted work pending error when making an HTTP callout after calling ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate. No DML operations are present in my code, and removing the Einstein call resolves the issue.
Here’s the relevant code:
public class MyClass { public void einsteinCall(String text) { ConnectApi.WrappedValue propertyValue = new ConnectApi.WrappedValue(); propertyValue.value = text; Map<String, ConnectApi.WrappedValue> inputParams = new Map<String, ConnectApi.WrappedValue>(); inputParams.put('Input:input', propertyValue); ConnectApi.EinsteinPromptTemplateGenerationsInput executeTemplateInput = new ConnectApi.EinsteinPromptTemplateGenerationsInput(); executeTemplateInput.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput(); executeTemplateInput.additionalConfig.applicationName = 'PromptBuilderPreview'; executeTemplateInput.isPreview = false; executeTemplateInput.inputParams = inputParams; ConnectApi.EinsteinPromptTemplateGenerationsRepresentation generationsOutput = ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate( PROMPT_TEMPLATE_NM, executeTemplateInput ); ConnectApi.EinsteinLLMGenerationItemOutput response = generationsOutput.generations[0]; String op = response.text; op = op.replace('<output>', '').replace('</output>', '').replaceAll('```json', '').replace('```', ''); System.debug('Prompt Template response: ' + op); PromptResponse promptResult = (PromptResponse) JSON.deserialize(op, PromptResponse.class); System.debug('Prompt Template response: ' + promptResult.toString()); } public void callEinsteinAndMakeCallout(String text) { einsteinCall(text); HttpRequest request = new HttpRequest(); request.setEndpoint('callout:msprodnc/v1/code'); request.setMethod('POST'); Http http = new Http(); HttpResponse response = http.send(request); System.debug('HTTP Response: ' + response.getBody()); }}
Issue
The HTTP callout fails with System.CalloutException when preceded by einsteinCall.
Without einsteinCall, the callout works.
Questions
- Does ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate create pending work?
- How can I commit the pending transaction so it allows http call ?
#Salesforce Developer
1 件の回答
Please check below link, which might be helpful.