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())
    if (!operableRules.isEmpty())
        final Workflow changedWorkFlow = new Workflow();
        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"
 <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>

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">
        <fullName>deleted account</fullName>

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.