You need to sign in to do that
Don't have an account?

Assistance with Test Class - 40% Code Coverage
Hello!
I'm not a developer, so I really don't know anything about code.
I managed to write an Apex Trigger and Test Class, but my Test Class only has 40% code coverage and is preventing me from deploying to Production.
I've read multiple articles and posts, but can't seem to interpret other solutions and apply them to my own.
Can anyone give any advice on how to increase this?
Thank you!
Here is my Trigger & Class:
TRIGGER:
trigger Unlock_Order_Pending_Approval on Order (after update) {
//Get records to unlock
List<order> orderList = [SELECT Id From Order WHERE CreatedDate = TODAY AND Unlock_Order__c != Null ];
//Check locked records
List<order> orderLockList = new List<Order>();
for(Order c :orderList){
if(Approval.isLocked(c.id)){
orderLockList.add(c);
}
}
//Unlock record
if(!orderLockList.isEmpty()){
//Unlock records
List<Approval.UnlockResult> ulrList = Approval.unlock(orderLockList, false);
// Iterate through each returned result
for(Approval.UnlockResult ulr : ulrList) {
if (ulr.isSuccess()) {
//Operation was successful, so get the ID of the record that was processed
System.debug('Successfully locked account with ID: ' + ulr.getId());
}
else {
//Operation failed, so get all errors
for(Database.Error err : ulr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Order fields that affected this error: ' + err.getFields());
}
}
}
}
}
TEST CLASS
@isTest
public class Test_Unlock_Order_Pending_Approval {
@isTest
public static void Test_Unlock_Order_Pending_Approval() {
//Create Account, create Order with that Account, submit into approval process, change field on order, unlock record
Account Acc=new Account();
Acc.Name='test';
Acc.Type='A&D Firm';
Acc.Industry='A&D Firm';
insert Acc;
Opportunity Opp=new Opportunity();
Opp.Name='new opp';
Opp.CloseDate=System.today();
Opp.StageName='Verified Project';
Opp.AccountId='Acc.id';
insert Opp;
delete Opp;
Order OrdObj=new Order();
OrdObj.Name='123456';
OrdObj.EffectiveDate=System.today();
OrdObj.Order_Review_Status__c='Pending Approval';
OrdObj.Delivery_Block__c='YB';
OrdObj.Assigned_Approver__c = '0051300000BiXKz';
OrdObj.Status = 'Draft';
OrdObj.AccountId=Acc.id;
insert OrdObj;
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(OrdObj.id);
Approval.ProcessResult result = Approval.process(app);
//Get records to unlock
List<order> orderList = [SELECT Id From Order WHERE Name = '123456' ];
//Check locked records
List<order> orderLockList = new List<Order>();
for(Order c :orderList){
if(Approval.isLocked(c.id)){
orderLockList.add(c);
}
}
//Unlock record
if(!orderLockList.isEmpty()){
//Unlock records
List<Approval.UnlockResult> ulrList = Approval.unlock(orderLockList, false);
// Iterate through each returned result
for(Approval.UnlockResult ulr : ulrList) {
if (ulr.isSuccess()) {
//Operation was successful, so get the ID of the record that was processed
System.debug('Successfully locked account with ID: ' + ulr.getId());
}
else {
//Operation failed, so get all errors
for(Database.Error err : ulr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Order fields that affected this error: ' + err.getFields());
}
}
}
}
}
}
Hi,
Please use this code and do some needful changes according to your code.
You are querying Unlock_Order__c and you are inserting this field to order
Please mark it as the Best Answer if it helps you.
Thank You