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
domdickdomdick 

Help required for @TestClass

Hi,

i have this trigger (worked fine) - but i cant figure out how to create a testclass to go with it. Can anyone help me please?

this trigger is update Account object (custom lookup field - Related_Contract_Number__c) with the contractnumber from the Contract object when few criteria will change.

 

Here is my trigger (worked fine) - it does seems to work but i cant get the testmethod.

 

trigger ContractNumUpdate on Contract (after update) {

    List<ID> AccIds = New List<ID>();
    for(Contract c : Trigger.new)
    {
        if(c.Status == 'Act' && c.Type__c == 'MS' && c.ContractNumber !=null)
        {
            AccIds.add(c.AccountId);
        }
    }
   
    List<Account> accList = [Select Id, Related_Contract_Number__c from Account where Id in :AccIds];
    Map<id,Contract>accContract = New Map<id,Contract>();
    for (Contract con : Trigger.new)
    {
        accContract.put(con.AccountId,con);
    }
   
   
    for(integer i=0; i < accList.size(); i++)
    {
        accList[i].Related_Contract_Number__c = accContract.get(accList[i].Id).Id;
    }
    update accList;
}

nishwalnishwal

can u plz tell that wat prob u r facing?

domdickdomdick

My prob is how to write a test class for above trigger. Any help would be appriciated!

nishwalnishwal

do u know how to write test classes?

domdickdomdick

I dont. Any help

domdickdomdick

Thanks for this info. I will give it a go but on initial stage it's bit difficulte for me to implement the test class for above trigger as i am a newbie. Can you help for the soluation at this stage?

 

Thanks for all your help!

ChizChiz

Create simple test class. in test method create Contact and then update it. trigger will fire so tested.

You don't know how to write tests in general?

ChizChiz

Write next code in test method of test class:

 

Contract testContract = new Contract(
 // some data goes here
);
insert testContact;

Test.startTest();

testContract.Status = 'Act';
testContract.Type__c = 'MS';
testContract.ContractNumber = '100500';

update testContract;

Test.stopTest();

 

 

 



domdickdomdick

Thanks Chiz,

 

Do i alos need to create Account?

ChizChiz

When you will run this particular test it would fire exception with text. This text will contain all necessary fields for Contract. Sorry, I don't know must fields for Contract sObject. It will be your challenge ;)

domdickdomdick

I am having a hard time to write a test class for this trigger.

here is a trigger (works fine)

trigger ContractNumUpdate on Contract (after update) {
    List<ID> AccIds = New List<ID>();
    for(Contract c : Trigger.new)
    {
        if(c.Status == 'Act' && c.Type__c == 'MS')
        {
            AccIds.add(c.AccountId);
        }
    }
    List<Account> accList = [Select Id, Related_Contract_Number__c from Account where Id in :AccIds];
    Map<id,Contract>accContract = New Map<id,Contract>();
    for (Contract con : Trigger.new)
    {
        accContract.put(con.AccountId,con);
    }
    for(integer i=0; i < accList.size(); i++)
    {
        accList[i].Related_Contract_Number__c = accContract.get(accList[i].Id).Id;
    }
    update accList;
}

 

 I have started writing a test class but stuck at end of the code and dont know how to evaluate futher??? can someone help me here to complete the test class?

 

@isTest
private class testUpdateContNumV1 {

    static testMethod void testContNum() {
        //Test.startTest();
        
        // set up account
        //List<Account> lstAcc = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Update Contract Account Test';
        acc.Country__c = 'United Kingdom';
        acc.CurrencyIsoCode = 'GBP';
        acc.Industry = 'Other';
        acc.Billing_Entity__c = 'BT';
        //lstAcc.add(acc);
        
        insert acc;
        
        // set up contract
        //List<Contract> lstCon = new List<Contract>();
        Contract con = new Contract();
        con.Type__c = 'SOW';
        con.AccountID = acc.Id;
        con.StartDate = Date.Today();
        con.ContractTerm = 1;
        con.CurrencyIsoCode = 'GBP';
        con.Status = 'Draft';
        //lstCon.add(con)    ;
        
        insert con;
        
        con.Type__c = 'MS';
        con.Status = 'Act';
               
        update con;
        
        Account udpateAccount = [Select Id, Name, Related_Contract_Number__c from Account a where a.Id = :con.AccountId LIMIT 1];
        
        //Test.stopTest();
        //now how can i evaluate futher from here..
    }
}

 

thanks in advaced!

ChizChiz
@isTest
private class testUpdateContNumV1 {

    static testMethod void testContNum() {
        
        
        // set up account
        //List<Account> lstAcc = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Update Contract Account Test';
        acc.Country__c = 'United Kingdom';
        acc.CurrencyIsoCode = 'GBP';
        acc.Industry = 'Other';
        acc.Billing_Entity__c = 'BT';
        //lstAcc.add(acc);
        
        insert acc;
        
        // set up contract
        //List<Contract> lstCon = new List<Contract>();
        Contract con = new Contract();
        con.Type__c = 'SOW';
        con.AccountID = acc.Id;
        con.StartDate = Date.Today();
        con.ContractTerm = 1;
        con.CurrencyIsoCode = 'GBP';
        con.Status = 'Draft';
        //lstCon.add(con)    ;
        
        insert con;
        
        con.Type__c = 'MS';
        con.Status = 'Act';


        Test.startTest();

        update con;
                
        Test.stopTest();

    }
}

 There is no need to assert something. Trigger will fire and you will have code coverage.