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
Devendra Hirulkar 3Devendra Hirulkar 3 

write test class of below trigger

Hello
the following is my trigger
and i have no idea to how to write test class so please help me sir

trigger copypro  on Subsc__c  (after insert,after update) 
{
    //Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'User Group Membership'].Id;
    Id recordTypeId = Schema.SObjectType.Subsc__c.getRecordTypeInfosByName().get('User Group Membership').getRecordTypeId();

    List<Subsc__c> subscList = new List<Subsc__c>();

    for (Subsc__c s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            subscList.add(s);
    }

    Map<ID, Account> Acc = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
    List<Id> listIds = new List<Id>();        
    set<ID>cObjectID = new set<ID>();   //Making a set of Product ID's
    Map<ID, Account> updateMap = new Map<ID, Account>();
  
    for (Subsc__c s : subscList)
    {
        listIds.add(s.Company_Name__c);
      
        if(s.Product__c     != null)
        {
            cObjectID.add(s.Product__c    );//takes the Lookup Record &     Add that ID's in cObjectID set
        }
    }
    if(!cObjectID.isEmpty()){
        
        Map<ID,Product2> cObjectMap = new Map<ID,Product2>([select Id,Name from Product2 where Id IN: cObjectID]);
        Acc = new Map<Id, Account>([SELECT id, Product_Name__c,(SELECT ID,Product__c FROM Subscs__r) FROM Account WHERE ID IN :listIds]);
        
        for(Subsc__c s : subscList)
        {            
            if(cObjectMap.get(s.Product__c    ).Name != Null)
            {
                // fill the country name on Opportunity with Country Name on Country_Object__c
                String pro= cObjectMap.get(s.Product__c    ).Name;
                Account myacc = acc.get(s.Company_Name__c);
                if(myacc != null){ //always check for nulls to avoid null pointer exceptions
                    myacc.Product_Name__c =pro;
                    updateMap.put(myacc.Id,myacc);
                }
            }
        }
        update updateMap.values();
    }
   
}
please ans as soon possible
thanks 
devendra
KaranrajKaranraj
Hi Devendra - 
@isTest
private class copyproTest {
    @isTest static void mytestMethod() {
        //  Insert the Subsc__c object record with all mandatory fields and related records
    }
}

FYI - I strongly recommend you to go  through apex test class Trail head module, which will be useful for you to understand apex test class https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_testing/apex_testing_intro
Devendra Hirulkar 3Devendra Hirulkar 3
hello sir  i have writen this test class but there is one error show


(System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, offer: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddCSR: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.AddCSR: line 4, column 1: []

Trigger.offer: line 10, column 1: [])

how to solve this problem and what changes needed


@istest
public class updateaccount 
{
     static testMethod void veriflyStudentupdation()
    {
        Account a = new Account(Name='Sam');
        {
            insert a;

            Subsc__c s = new Subsc__c(Name = 'ram',Company_Name__c=a.Id,Product__c='xyz');

            test.startTest();

            insert s;
            System.debug('Test Started.,..');    
          
            test.stopTest();
            System.debug('Test Completed.,..');

            Account upd_a = [Select Id,Product_Name__c  from Account where id = :a.Id];                                                                                                                                                                                                                                                                    
            system.assertEquals(null, upd_a.Product_Name__c );
        }    

    }   
}
Nirmal ChristopherNirmal Christopher

hi  Devendra,try check the sobject variable or list for null check before insertion. This error commony occurs the variaiable you are trying to insert reurn null value. Do the check both in test class and trigger. 
Subsc__c s = new Subsc__c(Name = 'ram',Company_Name__c=a.Id,Product__c='xyz');

            test.startTest();

            insert s;
put a null check some thing like this 
 
Subsc__c s = new Subsc__c(Name = 'ram',Company_Name__c=a.Id,Product__c='xyz');
if(s!=null){
            test.startTest();
            insert s;
}