function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa BunchMelissa Bunch 

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

Suraj Tripathi 47Suraj Tripathi 47

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

@isTest

public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  pricebookId ;
	o.Unlock_Order__c ='fill your data';//fill according to datatype
    
    insert o;

     o.Name='Update Data';
     update o;
	}
	}

Please mark it as the Best Answer if it helps you.

Thank You