Loading
Uw bedrijfsprocessen automatiseren met Salesforce Flow
Een stroom starten vanuit Apex

Een stroom starten vanuit Apex

Stromen die vanuit Apex worden gestart, kunnen complexe bedrijfslogica en uitgebreide foutafhandeling bevatten, en kunnen effectiever worden geïntegreerd met externe systemen. Deze benadering bevordert herbruikbaarheid door logica in te kapselen binnen stromen die naar behoefte kunnen worden aangeroepen. Het biedt ook meer controle over de uitvoeringscontext en de volgorde van bewerkingen. De combinatie van Apex en stroom helpt u krachtigere en flexibelere apps te maken die gemakkelijker te onderhouden zijn.

Opmerking
Opmerking Als u als stroombeheerder een stroom start vanuit Apex, wordt de nieuwste versie van de stroom gebruikt, ongeacht de activeringsstatus.

U kunt de Invocable.Action of de Flow.Interview klasse gebruiken om een stroom te starten vanuit Apex. Denk aan de verschillen tussen de twee klassen.

  • Als u de klasse Invocable.Action gebruikt om stromen uit te voeren, worden de stromen uitgevoerd in batches met bulkverwerking, maar kunt u alleen dynamisch naar stromen verwijzen. U geeft een tekenreekswaarde op voor de stroomnaam die geen verwijzingsintegriteit biedt. Deze benadering kan beter zijn wanneer u willekeurige stromen wilt uitvoeren, maar niet wanneer u een specifieke stroom aanroept.
  • Gebruik voor het verkrijgen van verwijzingsintegriteit de klasse Flow.Interview om statisch naar stromen te verwijzen. U kunt de Apex niet in een pakket opnemen of implementeren zonder dat de stroom bestaat in de doelorganisatie, de implementatie of het pakket. De Flow.Interview kan echter niet in batches worden uitgevoerd door bulkverwerking te gebruiken.

Salesforce Object Query Language (SOQL) en Data Manipulation Language (DML)-limieten zijn van toepassing tijdens de uitvoering van de stroom. Zie Stroomlimieten per transactie in de Salesforce Help.

Denk in de voorbeelden aan deze voorbeeldstroom:

  • API-naam stroom: MyFunFlow
  • De stroom heeft twee tekstvariabelen die beschikbaar zijn voor invoer:
    • inputA
    • inputB
  • De stroom heeft één tekstvariabele die beschikbaar is voor uitvoer:
    • outputVariable
  • De stroom heeft een element Toewijzing dat de waarden van inputA en inputB aaneenvoegt en het resultaat toewijst aan outputVariable

Eén automatisch gestarte stroom uitvoeren vanuit Apex met de actieklasse Aanroepbaar.

Als u een stroom wilt uitvoeren met behulp van Invocable.Action, maakt u een exemplaar van de Invocable.Action met behulp van de methode createCustomAction(). Stel de waarden van de invoervariabele in voor een of meer exemplaren van de stroom die u wilt uitvoeren en voer vervolgens de methode invoke() uit. De methode invoke() retourneert een object dat alle fouten of uitvoer van de stroom kan lezen.

Met deze benadering kunt u meerdere exemplaren van dezelfde stroom uitvoeren voor verschillende sets invoer.

/* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
Map<String,Object> flowInputVariables = new Map<String,Object>();
flowInputVariables.put('inputA','hello');
flowInputVariables.put('inputB','world');

/* Put the input map(s) into a List. Each item in this list represents a flow interview that's invoked together in a single batch run that uses flow bulkification. You must use a list, even if you want to run just one instance of the flow. */
List<Map<String,Object>> flowInputs = new List<Map<String,Object>{ flowInputVariables };

/* Initialize the flow as an action with the Action Type 'flow' and the API name of the flow */
Invocable.Action flowAction = Invocable.Action.createCustomAction('flow','MyFunFlow');

/* Use the input parameters to prepare the invocations that will be invoked */
flowAction.setInvocations(flowInputVariables);

/* Invoke the flow, assigning the results to a list of Invocable.Action.Result */
List<Invocable.Action.Result> flowResults = flowAction.invoke();

/* Read the results and outputs of the invocation */
for (Invocable.Action.Result result : flowResults) {
	if (result.isSuccess() == false) {
		// This invocation failed! read what the errors are with getErrors()
		List<Invocable.Action.Error> errors = result.getErrors();
	} else {
		// Success! Read the outputs with getOutputParameters()
		Map<String,Object> outputValues = result.getOutputParameters();
		String myFlowOutput = (String)outputValues.get('outputVariable');
		System.debug('Result: ' + myFlowOutput);
	}
}

De foutopsporingslogboeken tonen:

> Result: Hello World

Meerdere exemplaren van een automatisch gestarte stroom uitvoeren met de actieklasse Aanroepbaar en een enkele aanroep()

/* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value. */
Map<String,Object> flowInput1 = new Map<String,Object>();
flowInput1.put('inputA','hello');
flowInput1.put('inputB','world');

Map<String,Object> flowInput2 = new Map<String,Object>();
flowInput2.put('inputA','foo');
flowInput2.put('inputB','bar');

/* Put the input map(s) into a list. Each item in this list represents a flow interview invoked together in a single batch run that uses  flow bulkification . You must use a list, even if you want to run one instance of the flow only. */
List<Map<String,Object>> flowInputs = new List<Map<String,Object>();
flowInputs.add(flowInput1);
flowInputs.add(flowInput2);

/* Initialize the flow as an action with the Action Type 'flow' and the API name of the flow */
Invocable.Action flowAction = Invocable.Action.createCustomAction('flow','MyFunFlow');

/* Use the input parameters to prepare the invocations that will be invoked */
flowAction.setInvocations(flowInputs);

/* Invoke the flow, setting the result to a list of Invocable.Action.Result */
List<Invocable.Action.Result> flowResults = flowAction.invoke();

/* Read the results and outputs of the invocation */
for (Invocable.Action.Result result : flowResults) {
	if (result.isSuccess() == false) {
		// This invocation failed! Read what the errors are with getErrors()
		List<Invocable.Action.Error> errors = result.getErrors();
	} else {
		// Success! Read the outputs with getOutputParameters()
		Map<String,Object> outputValues = result.getOutputParameters();
		String myFlowOutput = (String)outputValues.get('outputVariable');
		System.debug('Result: ' + myFlowOutput);
	}
}

De foutopsporingslogboeken tonen:

> Result: hello world > Result: foo bar
> Result: foo bar

Eén automatisch gestarte stroom uitvoeren vanuit Apex met de stroom.Interactieklasse

Als u een stroom wilt uitvoeren met behulp van de klasse Flow.Interview, maakt u een exemplaar van de Flow.Interview met behulp van de methode createInterview() of een statisch exemplaar van Flow.Interview.FlowApiName. Stel de invoervariabelen in en voer vervolgens de methode start() uit. Na het uitvoeren van de methode start() kunt u de resulterende waarden van alle uitvoervariabelen lezen met behulp van de methode getVariableValue() in het exemplaar van de Flow.Interview.

U kunt slechts één Flow.Interview tegelijk uitvoeren met de dynamische of statische benadering.

Dynamisch de stroom uitvoeren met Flow.Interview.createInterview()

/* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
Map<String,Object> flowInputVariables = new Map<String,Object>();
flowInputVariables.put('inputA','hello');
flowInputVariables.put('inputB','world');

/* Initialize the flow using the API name of the flow and the list of inputs you've defined */
Flow.Interview myFlow = Flow.Interview.createInterview('MyFunFlow',flowInputVariables);

/* Invoke the flow, setting the result to a list of Invocable.Action.Result */
myFlow.start();

/* Read the results and outputs of the invocation */
String myFlowOutput = (String)myFlow.getVariableValue('outputVariable');

System.debug('Result: ' + myFlowOutput);
Result: hello world

De stroom statisch uitvoeren met Flow.Interview.FlowApiName

/* Set the input variable values into a Map<String,Object> with each input as a key, and the input value you want to set as the value */
Map<String,Object> flowInputVariables = new Map<String,Object>();
flowInputVariables.put('inputA','hello');
flowInputVariables.put('inputB','world');

/* Initialize the flow using the API name of the flow and the list of inputs you've defined. By using this mechanism, you are creating a static reference to the flow, meaning any deployment or packaging of this Apex  requires the flow to exist in the target org,deployment, or package */
Flow.Interview myFlow = new Flow.Interview.MyFunFlow(flowInputVariables);

/* Invoke the flow, setting the result to a list of Invocable.Action.Result */
myFlow.start();

/* Read the results and outputs of the invocation */
String myFlowOutput = (String)myFlow.getVariableValue('outputVariable');

System.debug('Result: ' + myFlowOutput);
> Result: hello world

Schermstromen uitvoeren in aangepaste code

Vanuit Visualforce: zie Een stroom inbedden in een Visualforce pagina

Vanuit een aangepaste Lightning component: zie Een stroom inbedden in een aangepaste Aura component

 
Wordt geladen
Salesforce Help | Article