Loading

U hebt nog openstaand, niet-doorgevoerd werk. Voer wijzigen door of draai ze terug voordat u de aanroepbewerking uitvoert.

Publicatiedatum: Mar 30, 2020
Beschrijving

Het kan zijn dat u een record moet maken en deze vervolgens moet bijwerken met informatie die door een webservice wordt verstrekt. Er mag echter geen webservice worden aangeroepen na een DML-instructie binnen dezelfde transactie. Om de vereiste actie te realiseren, moet de transactie in twee delen worden gesplitst, zodat de DML-transactie wordt voltooid voordat de webservice wordt aangeroepen.

Oplossing

Splits de transactie in twee afzonderlijke Ajax-processen.

Met het eerste wordt de record ingevoegd. Het tweede voert de aanroep uit en kan de nieuw ingevoegde record bijwerken.

Pagina '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>

Klasse '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;     } }
Knowledge-artikelnummer

000385374

 
Wordt geladen
Salesforce Help | Article