Loading
Salesforce에서 이메일을 보내기 위해서는 도메인 인증이 필요합니다.더 많이 읽기

Salesforce 에러: You have uncommitted work pending. Please commit or rollback before calling out.

게시 일자: Aug 18, 2025
상세 설명

레코드를 만든 후 웹 서비스에서 제공하는 정보로 업데이트해야 할 수 있습니다. 동일한 트랜잭션 내에서 DML 문 이후에 웹 서비스 콜아웃이 발생하지 않을 수 있습니다. 필요한 작업을 수행하려면 웹 서비스 콜아웃이 발생하기 전에 DML 트랜잭션이 완료되도록 트랜잭션을 두 부분으로 분리해야 합니다.

솔루션

두 개의 개별 Ajax 프로세스로 트랜잭션 분할

먼저 레코드를 삽입한 후 콜아웃을 수행하면 새로 삽입된 레코드를 업데이트할 수 있습니다.

TestWsCallout 페이지

<apex:page controller="TestWsCallout" tabstyle="Account">
    <apex:form >
        <apex:actionFunction action="{!InsertRecord}" name="InsertRecord_JS" Rerender="statuses" status="Status1" oncomplete="CallWebService_JS();"/>
        <apex:actionFunction action="{!CallWebService}" name="CallWebService_JS" status="Status2" reRender="statuses, msg"/>
        <apex:outputPanel id="statuses">
            <apex:actionStatus id="Status1" startText="...Inserting Record Into DB..." />
            <apex:actionStatus id="Status2" startText="...Calling Web Service..." />
        </apex:outputPanel>
        <apex:outputPanel id="msg">
            <apex:pageMessages />
        </apex:outputPanel>
        <div><input name="DoAction" class="btn" type="button" value="Do Action" onclick="InsertRecord_JS();return false;"/></div>
    </apex:form>
</apex:page>


TestWsCallout 클래스

public class TestWsCallout{
    
    Account myAccount;
   
    public PageReference InsertRecord() {
        myAccount = new Account(name = 'Test Account');
        insert myAccount;
        // Calling a Web Service here would throw an exception
        return null;
    }
    
    public PageReference CallWebService() {
        
        // Execute a call to a Web Service
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://MyWebService12345678790.com?id=' + myAccount.Id);
        req.setMethod('GET');
        HttpResponse response = new Http().send(req);
        
        // Simulate an update
        myAccount.Name = 'Test Account 2';
        update myAccount;        
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'WebService Called on New Account: ' + myAccount.Name));
        return null;
    }
}
추가 자원

Apex DML Operations

Knowledge 기사 번호

000385374

 
로드 중
Salesforce Help | Article