3

I'm trying to create a Flow from apex using the MetadataService.Flow, in this case what I want to do is:

  • Create the flow
  • Define some criteria
  • Invoke an apex class (defined with the @InvocableMethod)

So far I've defined this code:

    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader                = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId      = UserInfo.getSessionId();

    // Create Flow
    MetadataService.Flow flow = new MetadataService.Flow();
    flow.fullName             = 'FlowFromApex';
    flow.description          = 'Automatic creation';
    flow.label                = 'New Flow';
    flow.processType          = 'Flow';

    MetadataService.FlowApexPluginCall apex_pluggin = new MetadataService.FlowApexPluginCall();
    apex_pluggin.apexClass = 'mssgdirect_evaluateCondiitonHandler';

    List<MetadataService.FlowActionCall> actionCallsArray = new List<MetadataService.FlowActionCall>();
    MetadataService.FlowActionCall actioncall = new  MetadataService.FlowActionCall();
    actioncall.actionName = 'mssgdirect_invoke_apex';
    actioncall.actionType = 'chatterPost';
    actioncall.locationX  = 100;
    actioncall.locationY = 100;
    actionCallsArray.add(actioncall);


    flow.actionCalls = actionCallsArray;
    handleSaveResults(service.createMetadata(new List<MetadataService.Metadata> { flow })[0]);

I'm using this code inside a Remote action method so when I execute this, I'm getting this error:

enter image description here

Am I missing something here, what is the best practice defining the tree for the flow (Example: Flow > FlowActionCall > FlowApexPluginCall > condition etc, etc.)

7

I spent almost last night trying to figure it out, and I could create my process with this code, hope it helps:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader                = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId      = UserInfo.getSessionId();



    MetadataService.FlowActionCallInputParameter inputparam1 = new MetadataService.FlowActionCallInputParameter();

    List<MetadataService.FlowActionCall> actionCallsArray = new List<MetadataService.FlowActionCall>();
    MetadataService.FlowActionCall actioncall             = new  MetadataService.FlowActionCall();        
    actioncall.name                                       = 'myRule_1_A1';
    actioncall.label                                      = 'invoca clase de apex';
    actioncall.locationX                                  = 100;
    actioncall.locationY                                  = 200;
    actioncall.actionName                                 = 'mssgdirect_evaluateCondiitonHandler';
    actioncall.actionType                                 = 'apex';
    actionCallsArray.add(actioncall);

    /**
     * ::::: CONDITIONS ::::::
     */
    MetadataService.FlowElementReferenceOrValue elementValue1= new Metadataservice.FlowElementReferenceOrValue();
    elementValue1.stringValue = 'String';

    List<Metadataservice.FlowMetadataValue> list_metadataValue = new List<Metadataservice.FlowMetadataValue>();
    Metadataservice.FlowMetadataValue metadatavalue            = new MetadataService.FlowMetadataValue();
    metadatavalue.name                                         = 'operatorDataType';
    metadatavalue.value                                        = elementValue1;
    list_metadataValue.add(metadatavalue);

    Metadataservice.FlowMetadataValue metadatavalue2           = new MetadataService.FlowMetadataValue();
    metadatavalue2.name                                        = 'inputDataType';
    metadatavalue2.value                                       = elementValue1;
    list_metadataValue.add(metadatavalue2);

    Metadataservice.FlowMetadataValue metadatavalue3           = new MetadataService.FlowMetadataValue();
    metadatavalue3.name                                        = 'leftHandSideType';
    metadatavalue3.value                                       = elementValue1;
    list_metadataValue.add(metadatavalue3);

    Metadataservice.FlowMetadataValue metadatavalue4           = new MetadataService.FlowMetadataValue();
    metadatavalue4.name                                        = 'rightHandSideType';
    metadatavalue4.value                                       = elementValue1;
    list_metadataValue.add(metadatavalue4);        

    MetadataService.FlowElementReferenceOrValue valueCriteria= new Metadataservice.FlowElementReferenceOrValue();
    valueCriteria.stringValue = 'YUCEL1';

    List<MetadataService.FlowCondition> listConditions = new List<MetadataService.FlowCondition>();
    MetadataService.FlowCondition condition            = new MetadataService.FlowCondition();
    condition.processMetadataValues                    = list_metadataValue;
    condition.operator                                 = 'EqualTo';
    condition.rightValue                               = valueCriteria;
    condition.leftValueReference                       = 'myVariable_current.Name';    
    listConditions.add(condition);


    /**
     * ::::: RULES ::::::
     */
    Metadataservice.FlowConnector flowConnectorinRule = new Metadataservice.FlowConnector();
    flowConnectorinRule.targetReference               = 'myRule_1_A1';

    List<MetadataService.FlowRule> list_rules = new List<MetadataService.FlowRule>();
    MetadataService.FlowRule rule             = new Metadataservice.FlowRule();
    rule.name                                 = 'myRule_1';
    rule.conditionLogic                       = 'and';
    rule.conditions                           = listConditions;
    rule.connector                            = flowConnectorinRule;
    rule.label                                = 'Valida criterios';
    list_rules.add(rule);


     /*
        ::::: decitions :::::
     */

    List<Metadataservice.FlowDecision> list_desicions = new List<Metadataservice.FlowDecision>();
    Metadataservice.FlowDecision flowDecision         = new Metadataservice.FlowDecision();
    flowDecision.name                                 = 'myDecision';
    flowDecision.label                                = 'Primary Decision';
    flowDecision.locationX                            = 50;
    flowDecision.locationY                            = 0;
    flowDecision.defaultConnectorLabel                = 'default';
    flowDecision.rules                                = list_rules;

    list_desicions.add(flowDecision);


     /*
        ::::: ASSIGMENTs :::::
     */
    Metadataservice.FlowConnector flowConnector          = new Metadataservice.FlowConnector();
    flowConnector.targetReference                        = 'myDecision';

    List<MetadataService.FlowAssignment> list_assigments = new List<MetadataService.FlowAssignment>();
    MetadataService.FlowAssignment assigment             = new MetadataService.FlowAssignment();
    assigment.name                                       = 'myVariable_waitStartTimeAssignment';
    assigment.label                                      = 'myVariable_waitStartTimeAssignment';
    assigment.locationX                                  = 0;
    assigment.locationY                                  = 0;
    assigment.connector                                  = flowConnector;

    List<MetadataService.FlowAssignmentItem> listAssigmentItem = new List<MetadataService.FlowAssignmentItem>();
    MetadataService.FlowAssignmentItem assigmentItem           = new MetadataService.FlowAssignmentItem();

    MetadataService.FlowElementReferenceOrValue elementValue   = new Metadataservice.FlowElementReferenceOrValue();
    elementValue.dateTimeValue                                 = Datetime.now();

    assigmentItem.assignToReference                            = 'myVariable_waitStartTimeVariable';
    assigmentItem.operator                                     = 'Assign';
    assigmentItem.value                                        = elementValue;
    listAssigmentItem.add(assigmentItem);

    assigment.assignmentItems                                  = listAssigmentItem;
    list_assigments.add(assigment);

     /*
        ::::: METADATA VALKUES :::::
     */
    List<Metadataservice.FlowMetadataValue> flow_processMetadataValuesList = new List<Metadataservice.FlowMetadataValue>();        
    MetadataService.FlowElementReferenceOrValue processMetatdataElement1 = new Metadataservice.FlowElementReferenceOrValue();
    Metadataservice.FlowMetadataValue meta1                              = new Metadataservice.FlowMetadataValue();
    processMetatdataElement1.stringValue                                 = 'Account';        
    meta1.name                                                           = 'ObjectType';
    meta1.value                                                          = processMetatdataElement1;
    flow_processMetadataValuesList.add(meta1);

    MetadataService.FlowElementReferenceOrValue processMetatdataElement2 = new Metadataservice.FlowElementReferenceOrValue();
    Metadataservice.FlowMetadataValue meta2                              = new Metadataservice.FlowMetadataValue();
    processMetatdataElement2.elementReference                            = 'myVariable_current';        
    meta2.name                                                           = 'ObjectVariable';
    meta2.value                                                          = processMetatdataElement2;
    flow_processMetadataValuesList.add(meta2);

    MetadataService.FlowElementReferenceOrValue processMetatdataElement3 = new Metadataservice.FlowElementReferenceOrValue();
    Metadataservice.FlowMetadataValue meta3                              = new Metadataservice.FlowMetadataValue();
    processMetatdataElement3.elementReference                            = 'myVariable_old';        
    meta3.name                                                           = 'OldObjectVariable';
    meta3.value                                                          = processMetatdataElement3;
    flow_processMetadataValuesList.add(meta3);

    MetadataService.FlowElementReferenceOrValue processMetatdataElement4 = new Metadataservice.FlowElementReferenceOrValue();
    Metadataservice.FlowMetadataValue meta4                              = new Metadataservice.FlowMetadataValue();
    processMetatdataElement4.stringValue                                 = 'onCreateOnly';        
    meta4.name                                                           = 'TriggerType';
    meta4.value                                                          = processMetatdataElement4;
    flow_processMetadataValuesList.add(meta4);

    /*
        ::::: VARIABLES :::::
     */
    List<MetadataService.FlowVariable> list_variables = new List<MetadataService.FlowVariable>();        
    MetadataService.FlowVariable variable = new  MetadataService.FlowVariable();
    variable.name                         = 'myVariable_current';
    variable.dataType                     = 'SObject';
    variable.isCollection                 = false;
    variable.isInput                      = true;
    variable.isOutput                     = true;
    variable.objectType                   = 'Account';
    list_variables.add(variable);

    MetadataService.FlowVariable variable2 = new  MetadataService.FlowVariable();
    variable2.name                         = 'myVariable_old';
    variable2.dataType                     = 'SObject';
    variable2.isCollection                 = false;
    variable2.isInput                      = true;
    variable2.isOutput                     = true;
    variable2.objectType                   = 'Account';
    list_variables.add(variable2);

    MetadataService.FlowElementReferenceOrValue datetime_elementReference = new Metadataservice.FlowElementReferenceOrValue();
    datetime_elementReference.dateTimeValue = Datetime.now();

    MetadataService.FlowVariable variable3  = new  MetadataService.FlowVariable();
    variable3.name                          = 'myVariable_waitStartTimeVariable';
    variable3.dataType                      = 'DateTime';
    variable3.isCollection                  = false;
    variable3.isInput                       = false;
    variable3.isOutput                      = false;
    variable3.value                         = datetime_elementReference;
    list_variables.add(variable3);  

    /*
        ::::: FLOW :::::
     */
    MetadataService.Flow flow  = new MetadataService.Flow();
    flow.description           = 'Flujo sobre NombreDelObjeto';
    flow.interviewLabel        = 'Objecto_flujo-1_Account';
    flow.fullName              = 'myFlow-1';
    flow.label                 = 'Objecto flujo';
    flow.processType           = 'Workflow';
    flow.processMetadataValues = flow_processMetadataValuesList;
    flow.variables = list_variables;
    flow.assignments = list_assigments;
    flow.startElementReference = 'myVariable_waitStartTimeAssignment';
    flow.actionCalls = actionCallsArray;
    flow.decisions             = list_desicions;



    handleSaveResults(service.createMetadata(new List<MetadataService.Metadata> { flow })[0]);

This is the result:

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.