This is a Apex Test Class code, what i have here is that this class will contain hardcode value to create submit the ticket for approval.
I was wondering if i can get any guidance here since i can't really get from other place. Thank you for any help provided.
PS. this is Salesforce platform - Remedyforce related.
@isTest
public class Test_CustomRequireRejectionComment {
/*
create an object for approval, then
simulate rejecting the approval with an added comment.
The rejection should be processed normally without being interrupted.
*/
private static testmethod void testRejectionWithComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// Reject the submitted request, providing a comment.
Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
testRej.setComments('Rejecting request with a comment.');
testRej.setAction ('Reject');
testRej.setWorkitemId(testWorkItemId);
Test.startTest();
// Process the rejection
Approval.ProcessResult testRejResult = Approval.process(testRej);
Test.stopTest();
// Verify the rejection results
System.assert(testRejResult.isSuccess(), 'Rejections that include comments should be permitted');
System.assertEquals('Rejected', testRejResult.getInstanceStatus(),
'Rejections that include comments should be successful and instance status should be Rejected');
}
/*
For this test, create an object for approval, then reject the request, then
without a comment explaining why. The rejection should be halted, and
and an apex page message should be provided to the user.
*/
private static testmethod void testRejectionWithoutComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// Reject the submitted request, without providing a comment.
Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
testRej.setComments('');
testRej.setAction ('Reject');
testRej.setWorkitemId(testWorkItemId);
Test.startTest();
// Attempt to process the rejection
try
{
Approval.ProcessResult testRejResult = Approval.process(testRej);
system.assert(false, 'A rejection with no comment should cause an exception');
}
catch(DMLException e)
{
system.assertEquals('Operation Cancelled: Please provide a rejection reason!',
e.getDmlMessage(0),
'error message should be Operation Cancelled: Please provide a rejection reason!');
}
Test.stopTest();
}
/*
When an approval is approved instead of rejected, a comment is not required,
mark the approval status as pending, then ensure that this functionality still holds together.
*/
private static testmethod void testApprovalWithoutComment()
{
// Generate sample work item using utility method.
Id testWorkItemId = generateAndSubmitObject();
// approve the submitted request, without providing a comment.
Approval.ProcessWorkitemRequest testApp = new Approval.ProcessWorkitemRequest();
testApp.setComments ('');
testApp.setAction ('Approve');
testApp.setWorkitemId(testWorkItemId);
Test.startTest();
// Process the approval
Approval.ProcessResult testAppResult = Approval.process(testApp);
Test.stopTest();
// Verify the approval results
System.assert(testAppResult.isSuccess(),
'Approvals that do not include comments should still be permitted');
System.assertEquals('Approved', testAppResult.getInstanceStatus(),
'All approvals should be successful and result in an instance status of Approved');
}
/*
Put many objects through the approval process, some rejected, some approved,
some with comments, some without. Only rejctions without comments should be
prevented from being saved.
*/
private static testmethod void testBatchRejctions()
{
List<BMCServiceDesk__Change_Request__c> testBatchIS = new List<BMCServiceDesk__Change_Request__c>{};
for (Integer i = 0; i < 200; i++)
{
testBatchIS.add(new BMCServiceDesk__Change_Request__c());
}
insert testBatchIS;
List<Approval.ProcessSubmitRequest> testReqs =
new List<Approval.ProcessSubmitRequest>{};
for(BMCServiceDesk__Change_Request__c testinv : testBatchIS)
{
Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
testReq.setObjectId(testinv.Id);
testReqs.add(testReq);
}
List<Approval.ProcessResult> reqResults = Approval.process(testReqs);
for (Approval.ProcessResult reqResult : reqResults)
{
System.assert(reqResult.isSuccess(),
'Unable to submit new batch invoice statement record for approval');
}
List<Approval.ProcessWorkitemRequest> testAppRejs
= new List<Approval.ProcessWorkitemRequest>{};
for (Integer i = 0; i < 50 ; i++)
{
Approval.ProcessWorkitemRequest testRejWithComment = new Approval.ProcessWorkitemRequest();
testRejWithComment.setComments ('Rejecting request with a comment.');
testRejWithComment.setAction ('Reject');
testRejWithComment.setWorkitemId(reqResults[i*4].getNewWorkitemIds()[0]);
testAppRejs.add(testRejWithComment);
Approval.ProcessWorkitemRequest testRejWithoutComment = new Approval.ProcessWorkitemRequest();
testRejWithoutComment.setAction ('Reject');
testRejWithoutComment.setWorkitemId(reqResults[(i*4)+1].getNewWorkitemIds()[0]);
testAppRejs.add(testRejWithoutComment);
Approval.ProcessWorkitemRequest testAppWithComment = new Approval.ProcessWorkitemRequest();
testAppWithComment.setComments ('Approving request with a comment.');
testAppWithComment.setAction ('Approve');
testAppWithComment.setWorkitemId(reqResults[(i*4)+2].getNewWorkitemIds()[0]);
testAppRejs.add(testAppWithComment);
Approval.ProcessWorkitemRequest testAppWithoutComment = new Approval.ProcessWorkitemRequest();
testAppWithoutComment.setAction ('Approve');
testAppWithoutComment.setWorkitemId(reqResults[(i*4)+3].getNewWorkitemIds()[0]);
testAppRejs.add(testAppWithoutComment);
}
Test.startTest();
// Process the approvals and rejections
try
{
List<Approval.ProcessResult> testAppRejResults = Approval.process(testAppRejs);
system.assert(false, 'Any rejections without comments should cause an exception');
}
catch(DMLException e)
{
system.assertEquals(50, e.getNumDml());
for(Integer i = 0; i < 50 ; i++)
{
system.assertEquals((i*4) + 1, e.getDmlIndex(i));
system.assertEquals('Operation Cancelled: Please provide a rejection reason!',
e.getDmlMessage(i));
}
}
Test.stopTest();
}
/*
Utility method for creating single object, and submitting for approval.
The method should return the Id of the work item generated as a result of the submission.
***Include required field and set status.
*/
private static Id generateAndSubmitObject()
{
// Create a sample object and then submit it for approval.
BMCServiceDesk__Change_Request__c testIS = new BMCServiceDesk__Change_Request__c();
testIS = [Select Id From BMCServiceDesk__Change_Request__c Where Name ='CR00002135'];
insert testIS;
Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest();
testReq.setObjectId(testIS.Id);
Approval.ProcessResult reqResult = Approval.process(testReq);
System.assert(reqResult.isSuccess(),'Unable to submit new invoice statement record for approval');
return reqResult.getNewWorkitemIds()[0];
}
}
after test run the test class, i got these error message,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[View] 0:00 Test_CustomRequireRejectionComment testApprovalWithoutComment Fail System.QueryException: List has no rows for assignment to SObject Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: line 187, column 1 Class.Test_CustomRequireRejectionComment.testApprovalWithoutComment: line 71, column 1
[View] 0:18 Test_CustomRequireRejectionComment testBatchRejctions Fail System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process was found.: [] Class.Test_CustomRequireRejectionComment.testBatchRejctions: line 115, column 1
[View] 0:00 Test_CustomRequireRejectionComment testRejectionWithComment Fail System.QueryException: List has no rows for assignment to SObject Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: line 187, column 1 Class.Test_CustomRequireRejectionComment.testRejectionWithComment: line 13, column 1
[View] 0:00 Test_CustomRequireRejectionComment testRejectionWithoutComment Fail System.QueryException: List has no rows for assignment to SObject Class.Test_CustomRequireRejectionComment.generateAndSubmitObject: line 187, column 1 Class.Test_CustomRequireRejectionComment.testRejectionWithoutComment: line 40, column 1
The problem appears to be in your generateAndSubmitObject() static method. There is a lot of weird behavior.
I am going to break it down line-by-line.
Line 1: testIS gets a newly instantiated BMCServiceDesk__Change_Request__c object.
Line 2: testIS gets a list of BMCServiceDesk__Change_Request__c objects (but only the ID field) where the Name is equal to 'CR00002135'. There are two problems here. First, this action makes Line 1 completely useless. Two, this will return no results. You don't have any CRs named CR00002135 is your database at this point.
Line 3: You are reinserting this empty list back into the database. This is another no-operation.
The rest of this should work if you fix Lines 1 - 3. My recommendation, get rid of Line 2 and update Line 1 so that when you instantiate your BMCServiceDesk__Change_Request__c object it has all of the info you desire.