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
Siddharth LakhotiaSiddharth Lakhotia 

Need help in writting Test class ; ( To cover for trigger error message)

Hi,

I need help in getting 100% code coverage for my trigger. Currently , I am able to acheve 75% code coverage.

Trigger:

trigger preventChangeOfAccountName on Order (before insert) {
    
    Map<String, String> mapSourceOrderIdToAccount = new Map<String, String>();

for (Order obj : trigger.new){

        mapSourceOrderIdToAccount.put(obj.getCloneSourceId(), null);

    }

    for(Order objOrder : [Select id, AccountId from Order where id in : mapSourceOrderIdToAccount.keySet() Limit 5000]){

        mapSourceOrderIdToAccount.put(objOrder.id, objOrder.AccountId);

    }

     

    for (Order o : trigger.new){

       If  (o.isClone() && mapSourceOrderIdToAccount.get(o.getCloneSourceId()) <> o.AccountId)

       {

           o.addError('Account Name for Cloned records cant be changed');

       }

    }

}


Test Class : 

@isTest(SeeAllData = false)
public class preventChangeOfAccountNameTestClass {

    static testmethod void preventAccNameChange(){

Account acc = new Account(name='TestAccount1');
        insert acc;
        Order ord = new Order(AccountId = acc.Id,
                              EffectiveDate = System.Today(),
                              EndDate = System.Today()+5,
                                 Status = 'NA',
                              Offer_Status__c = 'Open'
                              );
        insert ord;
        try{
            update ord;
        }
        catch(DMLexception e){
           System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.ERROR));
    }
}
}


Test class is not able to cover for these 2 lines,

mapSourceOrderIdToAccount.put(objOrder.id, objOrder.AccountId);

o.addError('Account Name for Cloned records cant be changed');

can anybody tell me , how to do so...
 
 
Siddharth LakhotiaSiddharth Lakhotia
Any help on this ??/?
AnudeepAnudeep (Salesforce Developers) 
Hi Siddharth - You have to clone the order record in your test class

See example code here. Also, you should populate mapSourceOrderIdToAccount