我已经创建了一个触发器,它将在活动字段的更新时触发,并限制用户在拒绝记录时将评论字段留空。
这是我的触发代码:
trigger RequireRejectionComment on Campaign (before update)
{
Map<Id, Campaign > rejectedStatements
= new Map<Id, Campaign>{};
for(Campaign inv: trigger.new)
{
Campaign oldInv = System.Trigger.oldMap.get(inv.Id);
if ((oldInv.BR_ApprovalStatusRegulatory__c != 'Reprovado'
&& inv.BR_ApprovalStatusRegulatory__c == 'Reprovado')||
(oldInv.BR_ApprovalStatusLegal__c!= 'Reprovado' &&
inv.BR_ApprovalStatusLegal__c== 'Reprovado') )
{
rejectedStatements.put(inv.Id, inv);
}
}
if (!rejectedStatements.isEmpty())
{
List<Id> processInstanceIds = new List<Id>{};
for (Campaign invs : [SELECT (SELECT ID
FROM ProcessInstances
ORDER BY CreatedDate DESC
LIMIT 1)
FROM Campaign
WHERE ID IN :rejectedStatements.keySet()])
{
processInstanceIds.add(invs.ProcessInstances[0].Id);
}
// Now that we have the most recent process instances, we can check
// the most recent process steps for comments.
for (ProcessInstance pi : [SELECT TargetObjectId,
(SELECT Id, StepStatus, Comments
FROM Steps
ORDER BY CreatedDate DESC
LIMIT 1 )
FROM ProcessInstance
WHERE Id IN :processInstanceIds
ORDER BY CreatedDate DESC])
{
if ((pi.Steps[0].Comments == null ||
pi.Steps[0].Comments.trim().length() == 0))
{
Trigger.new[0].parentId.addError(' My error Message ');
//rejectedStatements.get(pi.TargetObjectId).addError(
// ' My error Message');
}
}
}
}
此触发器工作正常,但它在新页面上显示错误消息..
我的要求:错误信息应该出现在记录或同一页面上,同时拒绝记录。
请推荐,谢谢。。