Es posible que tenga que crear un registro y luego actualizarlo con la información proporcionada por un servicio web. No obstante, no se deberá producir una llamada al servicio web después de una instrucción DML dentro de la misma transacción. Para realizar la acción que se requiere, la transacción debe separarse en dos partes de modo que la transacción DML se complete antes de que se produzca la llamada al servicio web.
El primero inserta el registro y el segundo realiza la llamada y es capaz de actualizar el registro que se acaba de insertar.
Página 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="...Insertando registro en base de datos..." /> <apex:actionStatus id="Status2" startText="...Llamando al servicio web..." /> </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>public class TestWsCallout{ Account myAccount; public PageReference InsertRecord() { myAccount = new Account(name = 'Test Account'); insert myAccount; // La llamada a un servicio web aquí arrojaría una excepción return null; } public PageReference CallWebService() { // Ejecutar una llamada a un servicio web HttpRequest req = new HttpRequest(); req.setEndpoint('http://MyWebService12345678790.com?id=' + myAccount.Id); req.setMethod('GET'); HttpResponse response = new Http().send(req); // Simular una actualización myAccount.Name = 'Test Account 2'; update myAccount; ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Servicio web llamado en nueva cuenta: ' + myAccount.Name)); return null; } }
000385374

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.