XSLT更新审批状态前如何校验当前字段值避免重复更新
实现步骤
你需要在原有XSLT基础上做3处调整即可实现要求的校验逻辑:
- 新增变量获取当前审批记录的现有状态值
- 简化邮件审批结果的识别逻辑,统一转小写后判断,减少冗余的大小写分支
- 新增外层判断条件,仅当现有审批状态为Pending且邮件识别到有效审批结果时,才生成更新指令,否则跳过更新操作,避免重复修改状态。
修改后完整XSLT代码
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:template match="/" name="Approval_Update"> <!-- *********************** --> <!-- BEGIN: Define Variables --> <!-- *********************** --> <!-- find the profile link recid of the email sender --> <xsl:variable name="profilelink_recid"> <!-- for each RelatedBusinessObject --> <xsl:for-each select="BusinessObjectList/BusinessObject/RelatedBusinessObjectList/RelatedBusinessObject/BusinessObject"> <xsl:choose> <xsl:when test="@Name = 'Employee'"> <xsl:for-each select="./FieldList/Field"> <xsl:choose> <xsl:when test="@Name = 'RecId'"> <xsl:value-of select="."/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:variable> <!-- find the profile link category of the email sender --> <xsl:variable name="profilelink_cat"> <!-- for each RelatedBusinessObject --> <xsl:for-each select="BusinessObjectList/BusinessObject/RelatedBusinessObjectList/RelatedBusinessObject/BusinessObject"> <xsl:choose> <xsl:when test="@Name = 'Employee'"> <xsl:value-of select="'Employee'"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:variable> <!-- find subject and subjectID --> <xsl:variable name="emailSubject" select = "BusinessObjectList/BusinessObject/EmailMessage/Subject" /> <xsl:variable name="emailSubjectID" select = "BusinessObjectList/BusinessObject/EmailMessage/SubjectID" /> <xsl:variable name="emailBody" select = "BusinessObjectList/BusinessObject/EmailMessage/Body" /> <xsl:variable name="EmailStatus" select="substring-before($emailBody,' ')"/> <!-- 新增:获取当前审批记录的现有状态 --> <xsl:variable name="currentApprovalStatus" select="BusinessObjectList/BusinessObject[@Name='FRS_ApprovalVoteTracking']/FieldList/Field[@Name='Status']/text()" /> <!-- 新增:识别邮件中的有效审批结果 --> <xsl:variable name="targetStatus"> <xsl:choose> <xsl:when test="contains(lower-case($EmailStatus),'approve')">Approved</xsl:when> <xsl:when test="contains(lower-case($EmailStatus),'deny')">Denied</xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> </xsl:variable> <!-- 新增:校验条件,仅当现有状态为Pending且有有效审批结果时才执行更新 --> <xsl:if test="$currentApprovalStatus = 'Pending' and $targetStatus != ''"> <xsl:for-each select="BusinessObjectList/BusinessObject"> <xsl:element name="BusinessObject"> <xsl:attribute name="Name"><xsl:value-of select="'FRS_ApprovalVoteTracking'"/></xsl:attribute> <!-- Set Transaction as 'Update'. It will try to update existing record using UniqueKey value, otherwise it will fail. --> <xsl:element name="Transaction">Update</xsl:element> <!--Unique Key List--> <xsl:element name="UniqueKeyList"> <xsl:element name="UniqueKey"> <xsl:element name="Field"> <xsl:attribute name="Name"><xsl:value-of select="'TrackingID'"/></xsl:attribute> </xsl:element> </xsl:element> </xsl:element> <FieldList> <!-- Fields --> <xsl:for-each select="EmailMessage/node()"> <xsl:choose> <xsl:when test="name() = 'SubjectID'"> <xsl:element name="Field"> <xsl:attribute name="Name"><xsl:text>TrackingID</xsl:text></xsl:attribute> <xsl:attribute name="Type">System.Int32</xsl:attribute> <xsl:value-of select="."/> </xsl:element> </xsl:when> <xsl:when test="name() = 'Body'"> <xsl:element name="Field"> <xsl:attribute name="Name"><xsl:text>Status</xsl:text></xsl:attribute> <xsl:value-of select="$targetStatus"/> </xsl:element> <xsl:element name="Field"> <xsl:attribute name="Name"><xsl:text>Reason</xsl:text></xsl:attribute> <xsl:choose> <xsl:when test="$targetStatus = 'Denied'"> <xsl:text>Denied by Email</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text></xsl:text> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:when> </xsl:choose> </xsl:for-each> </FieldList> <!-- Copy the RelatedBusinessObjectList element--> <xsl:element name="RelatedBusinessObjectList"> <xsl:for-each select="RelatedBusinessObjectList"> <xsl:copy-of select="node()"/> </xsl:for-each> </xsl:element> </xsl:element> </xsl:for-each> </xsl:if> </BusinessObjectList> </xsl:template> </xsl:stylesheet>
内容的提问来源于stack exchange,提问作者PaulMorton
相关产品推荐
相关产品推荐

