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
Akash Garg 2Akash Garg 2 

want test class for trigger

Hi

I want to write a test class to increase code coverage for below mention trigger.
Can anyone help.
trigger paymenttermUpdate on Account (after update) {
    set<Id> acctIds = new set<Id>();
    map<Id, Account> mapAccount = new map<Id, Account>();
    list<Payment_Terms__c> listContact = new list<Payment_Terms__c>();
    
    for(Account acct : trigger.new) {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
    }
    
    listContact = [SELECT Name, Payment_Terms__c,Account__c FROM Payment_Terms__c WHERE Account__c IN : acctIds];
    
    if(listContact.size() > 0) {
        for(Payment_Terms__c con : listContact) {
            con.Name = mapAccount.get(con.Account__c).Payment_Term_on_account_picklist__c;
            con.Payment_Terms__c = mapAccount.get(con.Account__c).Payment_Term_on_account_picklist__c;
        }
        update listContact;
    }
}

 
Best Answer chosen by Akash Garg 2
DeveloperSudDeveloperSud
Hi,

Try something like this.Let us know of this works.
I considered the Payment_Terms__c field as a text field on Payment_Terms__c object.
@isTest
public class paymentTermUpdateTest {

    testmethod static void myUnitTest1(){
        
        // create account record
        Account a=new Account();
        a.Name='Test Account';
        insert a;
        // create a Payment_Terms__c record
        Payment_Terms__c pt=new Payment_Terms__c();
        pt.name='Test Payment Terms';
        pt.account__c=a.id;
        insert pt;
        
        Test.startTest();
        a.Payment_Term_on_account_picklist__c='Test pay term';
        update a;
        Test.stopTest();
        
        Payment_Terms__c ptNew=[select name,Payment_Terms__c from Payment_Terms__c where id=:pt.id];
        system.assertEquals('Test pay term', ptNew.name);
        system.assertEquals('Test pay term', ptNew.Payment_Terms__c);
        
    }
}

 

All Answers

DeveloperSudDeveloperSud
Hi,

Try something like this.Let us know of this works.
I considered the Payment_Terms__c field as a text field on Payment_Terms__c object.
@isTest
public class paymentTermUpdateTest {

    testmethod static void myUnitTest1(){
        
        // create account record
        Account a=new Account();
        a.Name='Test Account';
        insert a;
        // create a Payment_Terms__c record
        Payment_Terms__c pt=new Payment_Terms__c();
        pt.name='Test Payment Terms';
        pt.account__c=a.id;
        insert pt;
        
        Test.startTest();
        a.Payment_Term_on_account_picklist__c='Test pay term';
        update a;
        Test.stopTest();
        
        Payment_Terms__c ptNew=[select name,Payment_Terms__c from Payment_Terms__c where id=:pt.id];
        system.assertEquals('Test pay term', ptNew.name);
        system.assertEquals('Test pay term', ptNew.Payment_Terms__c);
        
    }
}

 
This was selected as the best answer
Akash Garg 2Akash Garg 2
Thanks you very much .
you are saviour.
It works perfect