+ Start a Discussion
Adam08Adam08 

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);

 

}

}

}

 

 

Adam08Adam08

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);

 

}

 

}

osamanosaman

Try this

 

 

for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Tigger.old[i].Checbox__c != true && Trigger.new[i].Checkbox__c == true)
{
Approval.ProcessSubmitRequest app = newApproval.ProcessSubmitRequest();
app.setObjectId(Trigger.new[i].id);
}
}

 

 


 

Adam08Adam08

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 :-)

 

 

osamanosaman

Paste your complete code here. The issue is with one of the missing brackets.

Adam08Adam08

Thanks. I have added the bracket :-)

There is an error on line 6:  Variable does not exist: Tigger.old 

 

trigger MDFSubmitForApproval on MDF__c (after insert, after update) {


for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Tigger.old[i].Submitted__c != true && Trigger.new[i].Submitted__c == true)
{
Approval.ProcessSubmitRequest app = newApproval.ProcessSubmitRequest();
app.setObjectId(Trigger.new[i].id);
}
}
}

 

Class:

 

@isTest
private class TestMDFSubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        MDF__c mdf = new MDF__c();
        mdf.Name = 'Test';
        mdf.Account_id__c = '00120000001Sqvv';
        // insert the new MDF
        insert mdf;
        mdf.Submitted__c = true;
        update mdf;
        
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :mdf.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 

osamanosaman

Its common sense. It should be Trigger and not Tigger

Adam08Adam08

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() 

osamanosaman

Replace 

 

newApproval.ProcessSubmitRequest() 

 

with

 

new Approval.ProcessSubmitRequest() 

Adam08Adam08

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.

osamanosaman

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.

 

Adam08Adam08

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 MDFSubmitForApproval on MDF__c (after insert, after update) {


for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Trigger.new[i].Submitted__c == true && (Trigger.isUpdate && Trigger.old[i].Submitted__c != true))
{
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();

app.setObjectId (Trigger.new[i].id);

Approval.ProcessResult result = Approval.process(app);

}
}
}

 

 

@isTest
private class TestMDFSubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        MDF__c mdf = new MDF__c();
        mdf.Name = 'Test';
        mdf.Account_id__c = '00120000001Sqvv';
        mdf.Submitted__c = false;
        mdf.Leads_Uploaded__c = true;
        // insert the new MDF
        insert mdf;
        mdf.Submitted__c = true;
        update mdf; 
        
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :mdf.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 

osamanosaman

Trigger should be on before insert and remove Trigger.old[i] condition