You need to sign in to do that
Don't have an account?
Help: Trigger not executing automatic approval process
Hi,
I want whenever the user enters the recods from web to lead process. It should automatically be submitted to worflow approvals. I have written a trigger , mentioned below. When I create a new record , it is giving this error: I am not getting why this error is coming.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger leadApprovalSubmit caused an unexpected exception, contact your administrator: leadApprovalSubmit: execution of BeforeInsert caused by: System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []: Trigger.leadApprovalSubmit: line 15, column 1
trigger leadApprovalSubmit on Lead (before insert) {
for (Lead l : trigger.new) {
// Create an approval request for the lead
Approval.ProcessSubmitRequest req1 =
new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(l.id);
// Submit the approval request for the lead
Approval.ProcessResult result = Approval.process(req1);
// Verify the result
System.assert(result.isSuccess());
}
}
Ps- error is same with after insert also. Pls help
try this. I guess you are comparing the lead recordtype right?
All Answers
You need the Id of the lead record, and you wouldnt have one in before insert.
Try this.
Hey Naidu ,
Thanks for your reply . Actually , I did mention the record Id's but I was not using after insert event. Thanks for pointing out my mistake. There is a one more thing . I want this approval process to work for only particular record type. Below is my code with little changes but I don't know why it is giving error. Kindly help me.
error
Compile Error: Initial term of field expression must be a concrete SObject: LIST<Lead> at line 5 column 10
trigger leadApprovalSubmit on Lead (After insert)
{
for (Lead l : trigger.new)
{
if(trigger.new.RecordType == '01280000000Q2G'){
// Create an approval request for the lead
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(l.id);
// Submit the approval request for the lead
Approval.ProcessResult result = Approval.process(req1);
// Verify the result
System.assert(result.isSuccess());
}
}
}
try this. I guess you are comparing the lead recordtype right?
Hey Naidu ,
Thanks for letting me know my silly mistakes. Really new to development world so tend make these kind of mistakes. You save me for the second time. Thanks a lot . You rock sir.
and yeah, I Wrote a test class for this which is giving 28% test coverage . Although I was able to deploy it But I want it to have more than 75% . Below is the test class . I will really appreciate if you can take a look to increase the test coverage.
@IsTest(SeeAllData=true)
public class leadApprovalSubmit_test{
static testmethod void MyUnitTest(){
Lead l = new Lead(Company='ABC',street_address__c='jkl'
,Primary_Distributor__c='test',
Email='hj@gmail.com',Industry='bio',Years_in_Business__c=89,primary_market__c='klo',
sales_geography_description__c='mmm', LastName='kop',Geography__c='united states');//Populate all the mandatory fields for lead to insert a record
insert l;
List<ProcessInstance> processInstances = [select Id from ProcessInstance where TargetObjectId = :l.id];
//System.assertEquals(processInstances.size(),1);
}
}