4

Without using the Migration Tool, I need to create a workflow rule using apex and this MetadataService class. I have the following code, and it does run, however I get a exception in my custom class (which is thrown so I can see which component has the error).

public void createWorkflowRules()
{
    MetadataService.MetadataPort ms = MetaDataJob.createService();
    MetadataService.Workflow workflow = new MetadataService.Workflow();
    MetadataService.WorkflowActionReference workflowActionRef = new MetadataService.WorkflowActionReference();
    MetadataService.WorkflowRule workflowRule = new MetadataService.WorkflowRule();
    MetadataService.WorkflowTimeTrigger workflowTimeTrigger = new MetadataService.WorkflowTimeTrigger();
    MetadataService.WorkflowFieldUpdate workflowFieldUpdate = new MetadataService.WorkflowFieldUpdate();

    // Workflow Field Update
    workflowFieldUpdate.fullName           = 'TEST_Active_Permission';
    workflowFieldUpdate.description        = 'Activates a permission.';
    workflowFieldUpdate.field              = 'Active__c';
    workflowFieldUpdate.literalValue       = '1';
    workflowFieldUpdate.name               = 'TEST Active Permission';
    workflowFieldUpdate.notifyAssignee     = false;
    workflowFieldUpdate.operation          = 'Literal';
    workflowFieldUpdate.protected_x        = false;
    workflowFieldUpdate.reevaluateOnChange = true;
    workflowFieldUpdate.targetObject       = getNameSpace() + 'Permission__c';

    // Workflow Action Reference
    workflowActionRef.name   = 'TEST_Active_Permission';
    workflowActionRef.type_x = 'FieldUpdate';

    // Workflow Time Trigger
    workflowTimeTrigger.offsetFromField         = 'Permission__c.BeginDate__c';
    workflowTimeTrigger.timeLength              = '0';
    workflowTimeTrigger.workflowTimeTriggerUnit = 'Hours';
    workflowTimeTrigger.actions                 = new MetadataService.WorkflowActionReference[]{workflowActionRef};

    // Workflow Rule
    workflowRule.fullName             = 'TEST_ManageActiveTemporaryPermission';
    workflowRule.active               = true;
    workflowRule.description          = 'Manages when a temporary permission has to be set as active.';
    workflowRule.formula              = 'IF(RecordType.DeveloperName=\'Temporary\' && BeginDate__c > Today(),TRUE,FALSE)';
    workflowRule.triggerType          = 'onCreateOrTriggeringUpdate';
    workflowRule.actions              = new MetadataService.WorkflowActionReference[]{workflowActionRef};
    workflowRule.workflowTimeTriggers = new MetadataService.WorkflowTimeTrigger[] {workflowTimeTrigger};

    workflow.fullName = 'Permission__c';
    workflow.rules    = new MetadataService.WorkflowRule[]{workflowRule};

    MetadataService.Metadata[] theMetadata = new MetadataService.Metadata[]{};

    // -------------------

    theMetadata.add(workflow);
    theMetadata.add(workflowRule);
    theMetadata.add(workflowFieldUpdate);

    MetadataService.SaveResult[] results = ms.createMetadata(theMetadata);

    for (MetadataService.SaveResult sr : results)
    {
        MetaDataJob.handleSaveResults(sr);
    }
}

And I'm trying to replicate the following metadata (obtained with the Migration Tool):

<rules>
    <fullName>Manage Active Temporary Permission</fullName>
    <active>true</active>
    <description>Manages when a temporary permission has to be set as Active.</description>
    <formula>IF(RecordType.DeveloperName=&apos;Temporary&apos; &amp;&amp; BeginDate__c &gt; Today(),TRUE,FALSE)</formula>
    <triggerType>onCreateOrTriggeringUpdate</triggerType>
    <workflowTimeTriggers>
        <actions>
            <name>Active_Permission</name>
            <type>FieldUpdate</type>
        </actions>
        <offsetFromField>Permission__c.BeginDate__c</offsetFromField>
        <timeLength>0</timeLength>
        <workflowTimeTriggerUnit>Hours</workflowTimeTriggerUnit>
        </workflowTimeTriggers>
</rules>

<fieldUpdates>
    <fullName>Active_Permission</fullName>
    <description>Actives a permission.</description>
    <field>Active__c</field>
    <literalValue>1</literalValue>
    <name>Active Permission</name>
    <notifyAssignee>false</notifyAssignee>
    <operation>Literal</operation>
    <protected>false</protected>
</fieldUpdates>

I have this metadata because in the development organization we have those rules setted. However, we can't put them in the package because it throws some errors during the package install. The solution we came up with is to create them dynamically during installation.

When I run the code above, I get an error saying that a fullName is missing:

pl01.MetaDataJob.MetadataServiceException: Error occured processing component TEST_ManageActiveTemporaryPermission. Need to specify full name, Name:TEST_ManageActiveTemporaryPermission, Delimiter:. (FIELD_INTEGRITY_EXCEPTION).

Note: the error is pretty like this because of the MetadataJob class, which filters the error in the debug log already.

1 Answer 1

1

I know I am 4 years late, but I must thank you for this code.

I think you are the only person on the internet to have posted an Apex Class that creates a Workflow/Rule/Action/FieldUpdate combo using the fantastic MetadataAPI.

I presume you have fixed the error by now :)

I used this code to dynamically change the formulas in some of our Workflow Rules & FieldUpdates.

workflow.fullName = 'Permission__c';
workflow.rules = new MetadataService.WorkflowRule[]{workflowRule};
//And also append fieldUpdates [Array] here...
workflow.fieldUpdates = new MetadataService.WorkflowFieldUpdate[]{workflowFieldUpdate};

MetadataService.Metadata[] theMetadata = new MetadataService.Metadata[]{};

// -------------------
 //The Rules and and FieldUpdates have been appended to the Workflow above so we just add the Workflow ...
theMetadata.add(workflow);
//theMetadata.add(workflowRule);
//theMetadata.add(workflowFieldUpdate); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .