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
harsha vardhan vasa 9harsha vardhan vasa 9 

getting system.Dml Exception.Below is the test class and helper class for trigger. please help

Helper Class:
public void setMaintenanceDateAfterUpdate(){
        list<Id> assetIds =new list<Id>();
        list<sls_Asset__c> assetList = new List<sls_Asset__c>();
        for(sls_Asset__c a:newList){
            if((trgOldMap.get(a.Id).sls_StartDate__c == null && trgNewMap.get(a.Id).sls_StartDate__c !=null)||
                (trgOldMap.get(a.Id).sls_StartDate__c <> trgNewMap.get(a.Id).sls_StartDate__c) ){
                  assetIds.add(a.Id);
               }      
        }
        if(!assetIds.isEmpty()){
            for(sls_Asset__c a : [select id,sls_startdate__c,sls_MaintenanceDate__c,sls_CheckDate__c,sls_OrderProduct__c,sls_OrderProduct__r.BProduct2__c,sls_OrderProduct__r.BProduct2__r.name,sls_OrderProduct__r.BProduct2__r.sls_InspectionPeriodMonths__c,sls_OrderProduct__r.BProduct2__r.sls_UsefulLifeMonths__c from sls_Asset__c where id =: assetIds]){
               integer inspect  = Integer.valueOf( a.sls_OrderProduct__r.BProduct2__r.sls_InspectionPeriodMonths__c);
                integer useful = Integer.valueOf(a.sls_OrderProduct__r.BProduct2__r.sls_UsefulLifeMonths__c);
                a.sls_MaintenanceDate__c = trgNewMap.get(a.Id).sls_StartDate__c.addmonths(inspect);
                a.sls_CheckDate__c = trgNewMap.get(a.Id).sls_StartDate__c.addmonths(useful);
                 assetList.add(a);

             }
        }
        if(!assetList.isEmpty()){
             system.debug('assetList:'+assetList);
            update assetList;
        }
           
    }

test Class:
static testMethod void setMaintenanceDateTest01(){
         BCMNTestDataUtil.loadBProfitStandard();
         User user = BCMNTestDataUtil.newUser(false);
         user.Department = '営業統括部';
         insert user;
         Account acc = BCMNTestDataUtil.newAccount('account01', '01234', true);
         Product2 product = BCMNTestDataUtil.newProduct2('Product1', 'PRODUCT1', 'AEDオプション', false);
         product.sls_InspectionPeriodMonths__c = 12; //点検月数
         product.sls_UsefulLifeMonths__c = 24;       //耐用月数
         insert product;
         PricebookEntry pbe = BCMNTestDataUtil.newStandardPricebookEntry(product, 20000, 10000, true);
         Opportunity opp = BCMNTestDataUtil.newOpportunity('opp01', user.Id, acc.Id, pbe.Pricebook2Id, false);
         opp.BSalesForm__c = '常駐警備';
         insert opp;
         BOrder__c order = BCMNTestDataUtil.newBOrder(opp.Id, true);
         BOrderProduct__c orderProduct =BCMNTestDataUtil.newBOrderProduct(order.Id, false);
         orderProduct.BProduct2__c = product.id;
         orderProduct.BUnitPrice__c = 20000;
         orderProduct.BQuantity__c = 2;
          insert orderProduct;
         Test.startTest();
         sls_Asset__c asset1 = new sls_Asset__c();
         asset1.sls_OrderProduct__c = orderProduct.Id;
         asset1.sls_Status__c = '保証書受領待ち';
         asset1.sls_StartDate__c = system.today();
         sls_Asset__c asset2 = new sls_Asset__c();
         asset2.sls_OrderProduct__c = orderProduct.Id;
         asset2.sls_Status__c = '保証書受領待ち';
         asset2.sls_StartDate__c = system.today();
         insert new List<sls_Asset__c>{asset1, asset2};
         Test.stopTest();
         //交換したレコードのステータスが更新されていること
         asset1 = [SELECT Id, sls_Status__c, sls_StartDate__c, sls_MaintenanceDate__c, sls_CheckDate__c FROM sls_Asset__c WHERE Id =: asset1.Id];
         system.assertEquals(asset1.sls_StartDate__c.addMonths(integer.valueOf(product.sls_InspectionPeriodMonths__c)), asset1.sls_MaintenanceDate__c);   //交換期限
         system.assertEquals(asset1.sls_StartDate__c.addMonths(integer.valueOf(product.sls_UsefulLifeMonths__c)), asset1.sls_CheckDate__c);         //点検期限
         asset2 = [SELECT Id, sls_Status__c, sls_StartDate__c, sls_MaintenanceDate__c, sls_CheckDate__c FROM sls_Asset__c WHERE Id =: asset2.Id];
         system.assertEquals(asset2.sls_StartDate__c.addMonths(integer.valueOf(product.sls_InspectionPeriodMonths__c)), asset2.sls_MaintenanceDate__c);
         system.assertEquals(asset2.sls_StartDate__c.addMonths(integer.valueOf(product.sls_UsefulLifeMonths__c)), asset2.sls_CheckDate__c);
     }

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

caused by: System.NullPointerException: Argument cannot be null.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harsha,

Greetings to you!

All data is hidden from the tests. If you need the data, you can add @isTest(SeeAllData=true) to your method.

Please refer to the below links for more information.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
harsha vardhan vasa 9harsha vardhan vasa 9
@Khan Anas,
Hi Khan,
thanks for the reply. i jus tried that but getting same error.

regards,
vasa Harsha Vardhan.