-1

I am trying to create AMPscript to parse through an XML payload and output the correct status in each classification. However, currently, my code is outputting every status in every classification I think I am on the right track, but now I am stuck.

Desired output:

Held 10
Pending Release 20
Pending Completion 10
Pending Fulfillment 20
Released 30
Pending Shipment 30
Ready to Ship 60
Ready for Service 55
Ready to Pack 50
Ready to Pick 40

Current output:

Held 10
Pending Release 20
Pending Completion 10
Released 30
Ready to Ship 60
Ready for Service 55
Ready to Pack 50
Ready to Pick 40
Pending Fulfillment 20
Pending Release 20
Pending Completion 10
Released 30
Ready to Ship 60
Ready for Service 55
Ready to Pack 50
Ready to Pick 40
Pending Shipment 30
Pending Release 20
Pending Completion 10
Released 30
Ready to Ship 60
Ready for Service 55
Ready to Pack 50
Ready to Pick 40

Code Sample:

%%[

VAR @ShipmentsReleasedRows
SET @ShipmentsReleasedRows = BuildRowSetFromXML(@order_xml, "//root/ShipmentClassification", 1)
SET @ShipmentsReleasedRowsCount = RowCount(@ShipmentsReleasedRows)

/* Begin ShipmentsReleasedRowCount Loop */
IF @ShipmentsReleasedRowsCount > 0 THEN
FOR @a = 1 TO @ShipmentsReleasedRowsCount DO

    SET @ShipmentsReleasedRow = Row(@ShipmentsReleasedRows, @a)
    SET @ShipmentsReleasedRow_xml = Field(@ShipmentsReleasedRow ,"XML")
    SET @ShipmentsReleasedRow_xml = Concat('<root>',@ShipmentsReleasedRow_xml,'</root>')


        SET @CLASSIF_DISPLAY_ORDER = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/CLASSIF_DISPLAY_ORDER", 1)
    IF RowCount(@CLASSIF_DISPLAY_ORDER) > 0 THEN
        SET @CLASSIF_DISPLAY_ORDER = Field(Row(@CLASSIF_DISPLAY_ORDER, 1), "Value")
    ELSE
        SET @CLASSIF_DISPLAY_ORDER = "0"
    ENDIF



        SET @CLASSIF_NAME = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/CLASSIF_NAME", 1)
    IF RowCount(@CLASSIF_NAME) > 0 THEN
        SET @CLASSIF_NAME = Field(Row(@CLASSIF_NAME, 1), "Value")
    ELSE
        SET @CLASSIF_NAME = "No Name"
    ENDIF


]%%


 <table align="center" border="0" cellpadding="0" cellspacing="0" style="margin:auto;border-collapse: collapse;border-spacing: 0; width:100%;text-align: center; max-width:520px;">

  <tr align="center">
  <td style="background-color:##F5F5F5;color:#222222;font-family: 'Santral', sans-serif;font-size:14px; font-weight: bold; text-align: left; padding-top: 18px;">
  %%=v(@CLASSIF_NAME)=%%
  </td>
  <td style="background-color:##F5F5F5;color:#222222;font-family: 'Santral', sans-serif;font-size:14px; font-weight: bold; text-align: right; padding-top: 18px;">
  %%=v(@CLASSIF_DISPLAY_ORDER)=%%
  </td>
 </tr>




%%[

/* Begin ShipmentsReleasedStatusRows loop - nested inside the ShipmentsReleasedRowCount Loop */
SET @ShipmentsReleasedStatusRows = BuildRowSetFromXML(@order_xml, "//root/ShipmentClassification/ShipmentStatus", 1)
IF RowCount(@ShipmentsReleasedStatusRows) > 0 THEN
    FOR @j = 1 TO RowCount(@ShipmentsReleasedStatusRows) DO

        SET @ShipmentsReleasedStatusRow = Row(@ShipmentsReleasedStatusRows, @j)
        SET @ShipmentsReleasedStatusRow_xml = Field(@ShipmentsReleasedStatusRow, "XML")
        SET @ShipmentsReleasedStatusRow_xml = Concat('<root>',@ShipmentsReleasedStatusRow_xml,'</root>')


        SET @STATUS_NAME = BuildRowSetFromXML(@ShipmentsReleasedStatusRow_xml, "//root/STATUS_NAME", 1)
    IF RowCount(@STATUS_NAME) > 0 THEN
        SET @STATUS_NAME = Field(Row(@STATUS_NAME, 1), "Value")
    ELSE
        SET @STATUS_NAME = "No Name"
    ENDIF


        SET @STATUS_DISPLAY_ORDER = BuildRowSetFromXML(@ShipmentsReleasedStatusRow_xml, "//root/STATUS_DISPLAY_ORDER", 1)
    IF RowCount(@STATUS_DISPLAY_ORDER) > 0 THEN
        SET @STATUS_DISPLAY_ORDER = Field(Row(@STATUS_DISPLAY_ORDER, 1), "Value")
    ELSE
        SET @STATUS_DISPLAY_ORDER = "0"
    ENDIF

  ]%%

 <tr align="center">
  <td style="background-color:##F5F5F5;color:#222222;font-family: 'Santral', sans-serif;font-size:14px; text-align: left; padding-top: 5px;">
  %%=v(@STATUS_NAME)=%%
  </td>
  <td style="background-color:##F5F5F5;color:#222222;font-family: 'Santral', sans-serif;font-size:14px; text-align: right; padding-top: 5px;">
  %%=v(@STATUS_DISPLAY_ORDER)=%%
  </td>
 </tr>

 %%[

    NEXT @j
    ENDIF
]%%


 %%[
NEXT @a  /* NEXT ShipmentsReleasedRow */
ENDIF  /* END ShipmentsReleasedRowCount Loop */
ENDIF
]%%

</table>

XML Sample:

    <ShipmentClassification>
        <CLASSIF_CODE>HELD</CLASSIF_CODE>
        <CLASSIF_NAME>Held</CLASSIF_NAME>
        <CLASSIF_DESCRIPTION>The shipment is currently held from fulfillment.</CLASSIF_DESCRIPTION>
        <CLASSIF_DISPLAY_ORDER>10</CLASSIF_DISPLAY_ORDER>
        <TOTAL_COUNT>0</TOTAL_COUNT>
        <ShipmentStatus>
            <STATUS_CODE>PENDING_RELEASE</STATUS_CODE>
            <STATUS_NAME>Pending Release</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is fully defined and is pending release for partner fulfillment.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>20</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>0</TOTAL_COUNT>
        </ShipmentStatus>
        <ShipmentStatus>
            <STATUS_CODE>PENDING_COMPLETION</STATUS_CODE>
            <STATUS_NAME>Pending Completion</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is currently being defined and is held from partner fulfillment.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>10</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>0</TOTAL_COUNT>
        </ShipmentStatus>
    </ShipmentClassification>
    <ShipmentClassification>
        <CLASSIF_CODE>PENDING_FULFILLMENT</CLASSIF_CODE>
        <CLASSIF_NAME>Pending Fulfillment</CLASSIF_NAME>
        <CLASSIF_DESCRIPTION>The shipment is ready to be fulfilled.</CLASSIF_DESCRIPTION>
        <CLASSIF_DISPLAY_ORDER>20</CLASSIF_DISPLAY_ORDER>
        <TOTAL_COUNT>1</TOTAL_COUNT>
        <ShipmentStatus>
            <STATUS_CODE>RELEASED</STATUS_CODE>
            <STATUS_NAME>Released</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment has been released to the partner for fulfillment.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>30</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>1</TOTAL_COUNT>
        </ShipmentStatus>
    </ShipmentClassification>
    <ShipmentClassification>
        <CLASSIF_CODE>PENDING_SHIPMENT</CLASSIF_CODE>
        <CLASSIF_NAME>Pending Shipment</CLASSIF_NAME>
        <CLASSIF_DESCRIPTION>The shipment is in the process of being fulfilled.</CLASSIF_DESCRIPTION>
        <CLASSIF_DISPLAY_ORDER>30</CLASSIF_DISPLAY_ORDER>
        <TOTAL_COUNT>2</TOTAL_COUNT>
        <ShipmentStatus>
            <STATUS_CODE>READY_TO_SHIP</STATUS_CODE>
            <STATUS_NAME>Ready to Ship</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is ready to ship.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>60</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>0</TOTAL_COUNT>
        </ShipmentStatus>
        <ShipmentStatus>
            <STATUS_CODE>READY_FOR_SERVICE</STATUS_CODE>
            <STATUS_NAME>Ready for Service</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is now ready for delivery services to be applied.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>55</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>0</TOTAL_COUNT>
        </ShipmentStatus>
        <ShipmentStatus>
            <STATUS_CODE>READY_TO_PACK</STATUS_CODE>
            <STATUS_NAME>Ready to Pack</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is now ready to be packed.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>50</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>1</TOTAL_COUNT>
        </ShipmentStatus>
        <ShipmentStatus>
            <STATUS_CODE>READY_TO_PICK</STATUS_CODE>
            <STATUS_NAME>Ready to Pick</STATUS_NAME>
            <STATUS_DESCRIPTION>The shipment is now ready to have the shipment lines picked from within the warehouse.</STATUS_DESCRIPTION>
            <STATUS_DISPLAY_ORDER>40</STATUS_DISPLAY_ORDER>
            <TOTAL_COUNT>1</TOTAL_COUNT>
        </ShipmentStatus>
    </ShipmentClassification>

1 Answer 1

0

Why didn't you follow the pattern I outlined in my previous answer? It will produce precisely what you intended -- with just a few minor adjustments:

%%[

var @order_xml, @nodes, @rowCount
set @order_xml = AttributeValue("order_xml")

set @order_xml = ""
set @order_xml = concat(@order_xml,"<root>")
set @order_xml = concat(@order_xml,"  <ShipmentClassification>")
set @order_xml = concat(@order_xml,"    <CLASSIF_CODE>HELD</CLASSIF_CODE>")
set @order_xml = concat(@order_xml,"    <CLASSIF_NAME>Held</CLASSIF_NAME>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DESCRIPTION>The shipment is currently held from fulfillment.</CLASSIF_DESCRIPTION>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DISPLAY_ORDER>10</CLASSIF_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"    <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>PENDING_RELEASE</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Pending Release</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is fully defined and is pending release for partner fulfillment.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>20</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>PENDING_COMPLETION</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Pending Completion</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is currently being defined and is held from partner fulfillment.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>10</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"  </ShipmentClassification>")
set @order_xml = concat(@order_xml,"  <ShipmentClassification>")
set @order_xml = concat(@order_xml,"    <CLASSIF_CODE>PENDING_FULFILLMENT</CLASSIF_CODE>")
set @order_xml = concat(@order_xml,"    <CLASSIF_NAME>Pending Fulfillment</CLASSIF_NAME>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DESCRIPTION>The shipment is ready to be fulfilled.</CLASSIF_DESCRIPTION>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DISPLAY_ORDER>20</CLASSIF_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"    <TOTAL_COUNT>1</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>RELEASED</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Released</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment has been released to the partner for fulfillment.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>30</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>1</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"  </ShipmentClassification>")
set @order_xml = concat(@order_xml,"  <ShipmentClassification>")
set @order_xml = concat(@order_xml,"    <CLASSIF_CODE>PENDING_SHIPMENT</CLASSIF_CODE>")
set @order_xml = concat(@order_xml,"    <CLASSIF_NAME>Pending Shipment</CLASSIF_NAME>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DESCRIPTION>The shipment is in the process of being fulfilled.</CLASSIF_DESCRIPTION>")
set @order_xml = concat(@order_xml,"    <CLASSIF_DISPLAY_ORDER>30</CLASSIF_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"    <TOTAL_COUNT>2</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>READY_TO_SHIP</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Ready to Ship</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is ready to ship.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>60</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>READY_FOR_SERVICE</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Ready for Service</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is now ready for delivery services to be applied.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>55</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>READY_TO_PACK</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Ready to Pack</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is now ready to be packed.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>50</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>1</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"    <ShipmentStatus>")
set @order_xml = concat(@order_xml,"      <STATUS_CODE>READY_TO_PICK</STATUS_CODE>")
set @order_xml = concat(@order_xml,"      <STATUS_NAME>Ready to Pick</STATUS_NAME>")
set @order_xml = concat(@order_xml,"      <STATUS_DESCRIPTION>The shipment is now ready to have the shipment lines picked from within the warehouse.</STATUS_DESCRIPTION>")
set @order_xml = concat(@order_xml,"      <STATUS_DISPLAY_ORDER>40</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"      <TOTAL_COUNT>1</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"    </ShipmentStatus>")
set @order_xml = concat(@order_xml,"  </ShipmentClassification>")
set @order_xml = concat(@order_xml,"</root>")

if indexOf(@order_xml,"<root>") > 0 then

  set @nodes = BuildRowsetFromXML(@order_xml,"//root/ShipmentClassification",0)
  set @rowCount = rowcount(@nodes)

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @nodepath
      var @displayOrder
      var @classif_name
      var @url

      set @nodepath = concat("//root/ShipmentClassification[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_DISPLAY_ORDER"))) > 0 then
          set @displayOrder = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_DISPLAY_ORDER"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_NAME"))) > 0 then
          set @classif_name = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_NAME"),0),1),'Value')
      endif

      /* you can end your AMPscript block here and output the HTML directly */
      ]%%

      <br><b>%%=v(@classif_name)=%% %%=v(@displayOrder)=%%</b>

      %%[

      set @nodes2 = BuildRowsetFromXML(@order_xml, concat("//root/ShipmentClassification[",@i,"]/ShipmentStatus")) 
      set @rowCount2 = rowcount(@nodes2)

      if @rowCount2 > 0 then

        for @j = 1 to @rowCount2 do 

          var @nodepath2
          var @status_name
          var @status_display_order

          set @nodepath2 = concat("//root/ShipmentClassification[",@i,"]/ShipmentStatus[",@j,"]")

          if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_NAME"))) > 0 then
              set @status_name = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_NAME"),0),1),'Value')
          endif

          if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_DISPLAY_ORDER"))) > 0 then
              set @status_display_order = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_DISPLAY_ORDER"),0),1),'Value')
          endif

          ]%%

          <br>%%=v(@status_name)=%% %%=v(@status_display_order)=%%

          %%[

        next @j

      endif

    next @i
  
  endif

endif

]%%

Output:

output

3
  • I felt that I failed to explain the full scope of my question. The XML will not be static. I was unsure how to tweak your answer to ensure it would work with a payload that is constantly changing. Thank you so much for helping me with this problem.
    – TehGoose
    Commented Jun 2, 2021 at 18:04
  • Take out the hard-coded XML string in my example and set @order_xml = AttributeValue("order_xml") should return the XML value from your send context. I only included the static version so I could test it myself. Commented Jun 2, 2021 at 18:06
  • This worked perfectly. Thank you!
    – TehGoose
    Commented Jun 3, 2021 at 11:05

You must log in to answer this question.

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