You need to sign in to do that
Don't have an account?
Adam08
Auto Approval for custom object
Hi,
I have set up a trigger that automatically submits a custom object for approval.
Currently this triggers on insert of new records, but I need to change this to trigger after a checkbox is updated, which can happen upon insert or afteran update. this is the only criteria for the trigger.
Appreciate any help.
here is my trigger:
trigger MDFSubmitForApproval on MDF__c (after insert) {
for
(MDF__c a : trigger.new) {
Approval.ProcessSubmitRequest app = newApproval.ProcessSubmitRequest();
app.setObjectId(a.id);
Approval.ProcessResult result = Approval.process(app);
}
}
}
and here is the class:
@isTest
private classTestMDFSubmitForApproval {
statictestMethodvoidtestApprovalSuccess() {
MDF__c mdf = newMDF__c();
mdf.Name = 'Test';
mdf.Account_id__c = '00120000001Sqvv';
insertmdf;
// ensure that the opp was submitted for approval
List<ProcessInstance> processInstances = [select Id, Status fromProcessInstancewhereTargetObjectId = :mdf.id];
System.assertEquals(processInstances.size(),1);
}
}
Try this
Thanks for this Osaman, I tried deploying your code but I now get the error:
'expecting right curly bracket, found 'EOF' at line 0
I have set this to trigger after insert or after update.
Help is appreciated :-)
Paste your complete code here. The issue is with one of the missing brackets.
Thanks. I have added the bracket :-)
There is an error on line 6: Variable does not exist: Tigger.old
Class:
Its common sense. It should be Trigger and not Tigger
Thanks,
sorry I'm fairly new to apex and had copied your original code and failed to spot this.
Apologies if this is also common sense but the error is now:
Description Resource Path Location Type
Save error: Method does not exist or incorrect signature: newApproval.ProcessSubmitRequest()
Replace
newApproval.ProcessSubmitRequest()
with
new Approval.ProcessSubmitRequest()
I guess there is an issue with triggering after insert and update?:
Description Resource Path Location Type
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MDFSubmitForApproval: execution of AfterInsert
Thanks I really appreciate your guidance on this.
Change the trigger condition to Before Insert and it should work as record insertion means the record should be available for everyone who has access to it. Making it before insert it should submit it for approval process and when its approved it will then be available to access.
Osaman,
I think i'm quite close now.
The approval now triggers automatically upon update to the checkbox on the record. However it does not trigger on initial submission where the checkbox is set to TRUE.
TRIGGER:
Trigger should be on before insert and remove Trigger.old[i] condition