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 

error in test class

hello 
i have write a test class class that shows the following error 

(System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, offer: execution of AfterInsert)
what is problem with my test class
here i show my trigger and test class

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

the above is my trigger
and
the below is my test class
test class:-
@istest
public class updateaccount 
{
     static testMethod void verifyProductUpdation()
    {
       
        Account a=new Account(Name='ABC');
        insert a;
        
        a=[Select Id,Name from Account where Id =:a.Id];
        System.assertEquals(null, a.Product_Name__c);
        
        Product2 p=new Product2(Name='XYZ');
        insert p;
        test.startTest();
        Subsc__c sub=new Subsc__c(Name='CBZ',Company_Name__c=a.Id,Product__c=p.Id);
        insert sub;
        a.Product_Name__c=p.Id;
        update a;
        a=[Select Id,Product_Name__c from Account where Id=:a.Id];
        
        System.assertEquals(p.Id,a.Product_Name__c);
        test.stopTest();
    }   
}
thanks
Devendra 
 
pconpcon
What line are you seeing this erorr occur on?  This could be because of the fact that you are running parallell tests and have a indexed or non-unique field that is the same.  For example, Product2 name should be unique.  If you do something like:
 
Product2 p = new Product2(Name = 'XYZ' + Datetime.now().getTime());

To make the name unique