0

I make a class to check identical value, it will be caled in after update trigger and i already test it and it work perfectly. But, when i tried to create test class, the test class cannot update the value, but when i tried to check with assert its updated. i tried to check the log on system.debug, the value from oldTrigger is same like newtrigger (as if its not updated), so the value of IdPM is null and cannot continue the process.

Do you guys have any idea of it? thank you This is my apex class

public class ProjectManagement_Manager {
         
    public void postProcessingForUpdate(List<Project_Management__c> lstPM){
        if(ProjectManagement_Handler.isFirstTime){
            ProjectManagement_Handler.isFirstTime = false;
            Set<ID> idPM = new Set<ID>();
            for(Project_Management__c pmlist: lstPM){
                Project_Management__c oldPM = (Project_Management__c) Trigger.oldMap.get(pmlist.Id);
                System.debug('NAME PM : ' + pmlist.Project_Name__c);
                System.debug('Handler :' + pmlist.Bowheer_2__c + ' ' + oldPM.Bowheer_2__c + ' ' +  pmlist.Project__c + ' ' + oldPM.Project__c + ' ' +pmlist.Project_Plant__c + ' ' + oldPM.Project_Plant__c);
                if((pmlist.Bowheer_2__c != oldPM.Bowheer_2__c || pmlist.Project__c !=oldPM.Project__c || pmlist.Project_Plant__c != oldPM.Project_Plant__c)){
                    idPM.add(pmlist.Id);
                }
            }
            
            if(idPM.size()> 0){
                System.debug('idPM :' + idPM);
                identicalPostPM(idPM);
            }
        }
    }
    
    public void identicalPostPM(Set<ID> lstID){
        Set<ID> PMIDs = new Set<ID>();
        List<Project_Management__c> listpmVal = new List<Project_Management__c>();
        
        for(Project_Management__c pmlist: [SELECT Id,Name, Bowheer_2__c, project__c, project_plant__c from Project_Management__c WHERE ID in:lstID ]){
            System.debug('PM Manager After: ' + pmlist.Name);
            Map<Id,Project_Management__c> pmVal = new Map<Id,Project_Management__c>([SELECT id,Name, Bowheer_2__c, project__c, project_plant__c from Project_Management__c 
                     WHERE Bowheer_2__c =:pmlist.Bowheer_2__c AND project__c =: pmlist.Project__c AND project_plant__c =: pmlist.Project_Plant__c 
                     AND id !=: pmlist.Id]);
            
            if(pmVal.size() > 0){
                listpmVal.addAll(pmVal.values());
                pmlist.Identical__c = true;
                system.debug('Update Current PM Val True ' +pmlist.Name );
            }else{
                pmlist.Identical__c = false;
                system.debug('Update Current PM Val False '+pmlist.Name);
            }
        update pmlist;
        }
        
        
        System.debug('listpmVal Size :' + listpmVal.size() +' Value '+ listpmVal);
        if(listpmVal.size() > 0){
            for(Project_Management__c identicPM : listpmVal){
                identicPM.Identical__c = true;
                system.debug('Update Identical PM Val ' + +identicPM.Name);
            }
            update listpmVal;
        }
        
        for(Project_Management__c pm: [SELECT Id,Name, Bowheer_2__c, project__c, project_plant__c from Project_Management__c WHERE ID in:lstID]){
            Project_Management__c oldPM = (Project_Management__c) Trigger.oldMap.get(pm.Id);
            if(oldPM.Bowheer_2__c != NULL && oldPM.Project__c != NULL && oldPM.Project_Plant__c != NULL && (pm.Bowheer_2__c != oldPM.Bowheer_2__c || pm.Project__c !=oldPM.Project__c || pm.Project_Plant__c != oldPM.Project_Plant__c)){
                checkIdentical(oldPM);  // check if update identical to be unique 
            } 
        }  
    }
    
    public void checkIdentical(Project_Management__c oldPM){
        Map<Id,Project_Management__c> lstpmVal = new Map<Id,Project_Management__c>([SELECT id, Bowheer_2__c, project__c, project_plant__c from Project_Management__c 
                                                                                    WHERE Bowheer_2__c =:oldPM.Bowheer_2__c AND project__c =: oldPM.Project__c
                                                                                    AND project_plant__c =: oldPM.Project_Plant__c AND id !=: oldPM.Id]);
        System.debug('lst PM will be uniq : '+ lstpmVal);
        List<Project_Management__c> contactForUpdate = new List<Project_Management__c>();
        if(lstpmVal.size() == 1){
            for(Project_Management__c key : lstpmVal.values()){ 
                Project_Management__c contactVal = new Project_Management__c(Id = key.id);
                contactVal.Identical__c = false; 
                contactForUpdate.add(contactVal);
                update contactForUpdate;
            }
        }     
    }
}

and this is my test class

@isTest
public class UT_USP_ProjMan_IdenticalTest {
    static testMethod void PM_Creation_1(){
      
        PM_Customer__c addpmc = new PM_Customer__c();
        addpmc.Name = 'Pama Persada';
        insert addpmc;
        
        PM_Customer__c pmc = [SELECT Id FROM PM_Customer__c LIMIT 1];
        
        Plant__c addPlant = new Plant__c();
        addPlant.Name = 'JKT';
        addPlant.Description__c = 'Test Create Plant';
        insert addPlant;
        
        Project_Management__c pm = new Project_Management__c();
        pm.Project_Name__c = 'Test Class PM 1';
        pm.Bowheer_2__c = pmc.id;
        pm.Sector__c = 'Construction';
        pm.Project__c = 'General Contractor';
        pm.Project_Location__c = 'Jakarta';
        pm.Status__c = 'New';
        pm.Owner_Type__c = 'Private';
        pm.Project_Type__c = 'New Project';
        pm.Project_Plant__c = [SELECT Id FROM Plant__c LIMIT 1].Id;
        pm.Project_Start__c = System.Date.today();
        pm.Project_End__c = System.Date.today() + 14;
        pm.Project_Value__c = Double.valueOf(1000000000);
        pm.Project_Stage__c = 'Concept/Feasibility Study';        
        pm.Panjang_KM__c = Double.valueOf(10);
        pm.Lebar_KM__c = Double.valueOf(10);
        pm.Volume__c = Double.valueOf(10);
        pm.Unit__c = 'LCM';
        pm.Project_Duration_Month__c = 6;
        insert pm;         
        
        ProjectManagement_Handler.isFirstTime = true;
        
        Project_Management__c pm2 = new Project_Management__c();
        pm2.Project_Name__c = 'Test Class PM 2';
        pm2.Bowheer_2__c = pmc.id;
        pm2.Sector__c = 'Construction';
        pm2.Project__c = 'General Contractor';
        pm2.Project_Location__c = 'Jakarta';
        pm2.Status__c = 'New';
        pm2.Owner_Type__c = 'Private';
        pm2.Project_Type__c = 'New Project';
        pm2.Project_Plant__c = [SELECT Id FROM Plant__c LIMIT 1].id;
        pm2.Project_Start__c = System.Date.today();
        pm2.Project_End__c = System.Date.today() + 14;
        pm2.Project_Value__c = Double.valueOf(1000000000);
        pm2.Project_Stage__c = 'Concept/Feasibility Study';        
        pm2.Panjang_KM__c = Double.valueOf(10);
        pm2.Lebar_KM__c = Double.valueOf(10);
        pm2.Volume__c = Double.valueOf(10);
        pm2.Unit__c = 'LCM';
        pm2.Project_Duration_Month__c = 6;
        insert pm2;
        pm2.Project__c = 'Rent';
        update pm2;
        
        pm2 = [SELECT Project__c, LastModifiedDate FROM Project_Management__c WHERE Id = :pm2.id];
        system.assertNotEquals(null, pm2.LastModifiedDate, 'This record has been modified');
        
        //call batch class
        UT_USP_Batch_IdenticalPM batchMethod = new UT_USP_Batch_IdenticalPM();
        Database.executeBatch(batchMethod, 50);
        
    }
    
    static testMethod void IdenticalTest(){
       List<Project_Management__c> pm = [SELECT Id, Name, Bowheer_2__c, project__c, project_plant__c,LastModifiedDate from Project_Management__c];
       for(Project_Management__c pmlist: pm ){
            
          pmlist.Project__c = 'Rent';
          update pmlist;
          System.assertEquals('Rent', pmlist.Project__c);
       }
    }
    
}
1
  • Note that the testmethod keyword is deprecated. You should be using the @isTest annotation instead (so you end up using it for the class itself, as well as all of the test methods inside of the class).
    – Derek F
    Commented Jan 5, 2023 at 15:29

1 Answer 1

0

I think you have to give the Id of Project_Management__c (pm) to the pm2 , get the Idea from the below code:-

Project_Management__c pm = new Project_Management__c();
        pm.Project_Name__c = 'Test Class PM 1';
        pm.Bowheer_2__c = pmc.id;
        pm.Sector__c = 'Construction';
        /* further code */


Project_Management__c pm2 = new Project_Management__c();
        pm2.id = pm.Id; // Here we have to give the id of the record for update
        pm2.Project_Name__c = 'Test Class PM 2';
        pm2.Bowheer_2__c = pmc.id;
        pm2.Sector__c = 'Construction';
        pm2.Project__c = 'General Contractor';
        /* Further Code */

I hope you got the answer, if not feel free to ask.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .