1

I'm developing a test application that modifies the workflow by reading the retrieved Workflow files that are downloaded. The files are read, the workflow switch is modified and submitted to Metadata deploy. Currently, the files are read through XMLDocument tree. However, it was discovered that the Metadata objects itself have an api to read and write to xml. I took some time on trying my hands with the code:

private void writeXmlTest() throws Exception
{
    final boolean changed = false;
    final Path workFlowFile = Paths.get("Account.workflow");
    final Workflow workFlow = new Workflow();
    final XmlInputStream xmlInputStream = new XmlInputStream();
    final InputStream inputStream = Files.newInputStream(workFlowFile);
    xmlInputStream.setInput(inputStream, "UTF-8");
    final String namespace = xmlInputStream.getNamespace();
    final TypeMapper typeMapper = new TypeMapper();
    workFlow.load(xmlInputStream, typeMapper);
    final List<WorkflowRule> operableRules = Arrays.stream(workFlow.getRules())
            .filter(WorkflowRule::isActive)
            .collect(Collectors.toList());
    if (!operableRules.isEmpty())
    {
        final Workflow changedWorkFlow = new Workflow();
        changedWorkFlow.setFullName(workFlow.getFullName());
        operableRules.stream().forEach(rule -> rule.setActive(false));
        changedWorkFlow.setRules(operableRules.toArray(new WorkflowRule[operableRules.size()]));
        try (XmlOutputStream os = new XmlOutputStream(
                Files.newOutputStream(workFlowFile.resolveSibling("testfile.workflow")), true);)
        {
            final QName __element = new QName("urn:partner.soap.sforce.com", "Workflow");
            changedWorkFlow.write(__element, os, typeMapper);
        }
    }
}

I have an issue where the writing to XML path prints the file as:

<n1:Workflow n2:type="Workflow" xmlns:n1="urn:partner.soap.sforce.com"
   xmlns:n2="http://www.w3.org/2001/XMLSchema-instance">
 <n3:fullName n2:nil="true" xmlns:n3="http://soap.sforce.com/2006/04/metadata" />
 <n4:rules n2:type="n4:WorkflowRule" xmlns:n4="http://soap.sforce.com/2006/04/metadata">
  <n4:fullName>deleted account</n4:fullName>
  <n4:active>false</n4:active>
  <n4:criteriaItems>
   <n4:field>Account.Status__c</n4:field>
   <n4:operation>equals</n4:operation>
   <n4:value>Delete</n4:value>
  </n4:criteriaItems>
  <n4:triggerType>onCreateOrTriggeringUpdate</n4:triggerType>
 </n4:rules>
</n1:Workflow>

I seem to miss the point on QName. Is there a way that can make the intended file to look like this?

<?xml version="1.0" encoding="UTF-8"?>
<Workflow xmlns="http://soap.sforce.com/2006/04/metadata">
    <rules>
        <fullName>deleted account</fullName>
        <active>true</active>
        <criteriaItems>
            <field>Account.Status__c</field>
            <operation>equals</operation>
            <value>Delete</value>
        </criteriaItems>
        <triggerType>onCreateOrTriggeringUpdate</triggerType>
    </rules>
</Workflow>

Your Answer

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

Browse other questions tagged or ask your own question.