需求: 当承认申请开始的时候,填写备注。或者,承认申请自动拒绝的时候,填写备注。
现象
在数据的页面上,我们点击【Submit for Approval】按钮开始一个承认申请的时候、承认申请的Comments是空的。
自动拒绝后,承认申请的Comments的内容也是空的。
Tips
在Salesforce平台上,如果我们要解决一个问题,那么我们首先要调查一下,有没有这个标准的功能。
有没有这样的标准设置,我们可以直接拿来用。
但调查了一下,用标准功能,不能实现我们的这个需求。
找到了几个Idea,但是过去时间已经好几年了,也没有被解决掉。
所以在将来,也是不可能被对应掉的。
https://ideas.salesforce.com/s/idea/a0B8W00000GdiYEUAZ/submit-for-approval-with-comments
在Salesforce上面,如果要想到自动申请 ,或者自动拒绝,那么大概率我们可以用Apex来实现。
就往着这个方向,继续找解决办法。
大家可能就很容易找到下面这个文章,找到就对了,就延着这个方向继续确认。
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm
用Apex来实现承认申请的提出,和自动拒绝的代码
先把代码留在这里,文章后面再写
代码也是需要再整理一下的。
trigger AutomateApprove on ChildObject__c(After insert, After update)
{
for (Integer i = 0; i < Trigger.new.size(); i++)
{
try
{
if( Trigger.isInsert || (Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit'))
{
submitForApproval(Trigger.new[i]);
}
else if(Trigger.isInsert || (Trigger.new[i].Next_Step__c == 'Approve' && Trigger.old[i].Next_Step__c != 'Approve'))
{
approveRecord(Trigger.new[i]);
}
else if(Trigger.isInsert || (Trigger.new[i].Next_Step__c == 'Reject' && Trigger.old[i].Next_Step__c != 'Reject'))
{
rejectRecord(Trigger.new[i]);
}
}catch(Exception e)
{
Trigger.new[i].addError(e.getMessage());
}
}
/**
* This method will submit the ChildObject__c automatically
**/
public void submitForApproval(ChildObject__c opp)
{
System.debug('submitForApproval-----33-------->');
//创建一个ApprovalProcess
// Create an approval request for the ChildObject__c
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
//设置备注
req1.setComments('Submitting request for approval automatically using Trigger');
req1.setObjectId(opp.id);
//设置承认者
req1.setNextApproverIds(new Id[] {opp.Next_Approver__c});
// Submit the approval request for the ChildObject__c
Approval.ProcessResult result = Approval.process(req1);
//在这里检查条件,如果不符号这个条件,那么就结束这个ApprovalProcess
//关于条件的检查代码,需要自己按需求进行开发,这里直接省略条件检查的代码
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
//设置备注
req.setComments('Rejected request using Trigger');
req.setAction('Reject');
Id workItemId = getWorkItemId(opp.id);
if(workItemId == null)
{
opp.addError('Error Occured in Trigger');
}
else
{
req.setWorkitemId(workItemId);
// Submit the request for approval
Approval.ProcessResult result2 = Approval.process(req);
}
}
/**
* Get ProcessInstanceWorkItemId using SOQL
**/
public Id getWorkItemId(Id targetObjectId)
{
System.debug('getWorkItemId-----Started-------->');
Id retVal = null;
List<ProcessInstanceWorkitem> ProcessInstanceWorkitemList = [Select p.Id from ProcessInstanceWorkitem p ];
System.debug('ProcessInstanceWorkitemList------------->' + ProcessInstanceWorkitemList);
//for(ProcessInstanceWorkitem workItem : [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId =: targetObjectId])
for(ProcessInstanceWorkitem workItem : ProcessInstanceWorkitemList)
{
retVal = workItem.Id;
}
return retVal;
}
/**
* This method will Approve the ChildObject__c
**/
public void approveRecord(ChildObject__c opp)
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using Trigger');
req.setAction('Approve');
req.setNextApproverIds(new Id[] {opp.Next_Approver__c});
Id workItemId = getWorkItemId(opp.id);
System.debug('workItemId-----11-------->' + opp.id);
System.debug('workItemId-----11-------->' + workItemId);
if(workItemId == null)
{
opp.addError('Error Occured in Trigger');
}
else
{
req.setWorkitemId(workItemId);
// Submit the request for approval
Approval.ProcessResult result = Approval.process(req);
}
}
/**
* This method will Reject the ChildObject__c
**/
public void rejectRecord(ChildObject__c opp)
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Rejected request using Trigger');
req.setAction('Reject');
Id workItemId = getWorkItemId(opp.id);
System.debug('workItemId-----22-------->' + opp.id);
System.debug('workItemId-----22-------->' + workItemId);
if(workItemId == null)
{
opp.addError('Error Occured in Trigger');
}
else
{
req.setWorkitemId(workItemId);
// Submit the request for approval
Approval.ProcessResult result = Approval.process(req);
}
}
}
上面是承认申请的开始部分,下面是拒绝承认申请的部分。
下面是实现的效果