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
shobana shobanashobana shobana 

Test class failed ?

Hello everyone
I am new to salesforce
Actually i wrote a test class it showing me error can anyone help to solve this problem.
Apex class

public with sharing class UpdateMileStone{

    //public Mile_Stone__c mStone {get; set;}
    public Case__c project;
    public List<MSList> newMSList{get; set;}

    public UpdateMileStone(ApexPages.StandardController stdController){
        project = (Case__c)stdController.getRecord();
        
        project = [Select Id, (Select Id, Name, Date_Received__c, Description__c, Date_Required_By__c From Required_Documents__r)
                from Case__c  where Id =:project.Id];
              
        newMSList = new List<MSList>(); 

        for(Required_Documents__c m: project.Required_Documents__r){
            MSList msListTemp = new MSList();             
            msListTemp.mStone= m;
                newMSList.add(msListTemp);            
        }                                 
    }
    //Updating Milestone    
    public PageReference updateDocumentRecords(){
        List<Required_Documents__c> MSUpdateList = new List<Required_Documents__c>();
        for(MSList mTemp: newMSList){
            if(mTemp.selectMS){
              MSUpdateList.add(mTemp.mStone);
            }
        }
        if(MSUpdateList.size()>0){
          update MSUpdateList;
        }else{
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please select one/multiple Required Documents to update!'));
          return null;
        }
        return new PageReference('/'+project.Id);
    }
    
       
    // Inner Class to create Payment Schedule with Checkbox   
    public class MSList{
        public Boolean selectMS {get; set;}
        public Required_Documents__c mStone {get; set;}
        
        public MSList(){
            selectMS=false;
        }
    }

Test class

  @IsTest 
  Public class updatemilestone{
    static testMethod void testUpdateMileStone(){

       Test.startTest();
        User usr = new User(Id=UserInfo.getUserId());
        System.runAs(usr){
        
            Case__c p1=new Case__c(Name='test1');
            insert p1;
            
            Required_Documents__c m1=new Required_Documents__c(Name='test2');
            m1.Date_Received__c = System.today();
            insert m1;
            
            ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(p1);
           UpdateMileStone ums = new UpdateMileStone(stdcontroller);
           
            ums.updateMileStoneRecords();
           Required_Documents__c k=new Required_Documents__c();
                k.selectMS= true;
               
            }
           
            ums.updateDocumentRecords(); 
            
            }
       Test.stopTest();
     
    }

Thank you in advance
 
Rahul Sangwan7341Rahul Sangwan7341
Hi Shobana,

1 thing which i am seeing is you are not assigning case to Required documents.
And can you please tell the error which you are getting.
 
shobana shobanashobana shobana
Hi Rahul 
Thank you for your replay.
I have changed my code.
@IsTest 
 Public class updatemilestone{
 static testMethod void testUpdateMileStone(){
    User usr = new User(Id=UserInfo.getUserId());
    System.runAs(usr){
        
    Case__c p1=new Case__c(Name='test1');
    insert p1;
            
    Required_Documents__c m1=new Required_Documents__c(Name='test2');
    m1.Date_Received__c = System.today();
    insert m1;
        Test.startTest();
            ApexPages.StandardController stdcontroller = new ApexPages.Standardcontroller(p1);
            UpdateMileStone cls = new UpdateMileStone(stdcontroller);
            cls.newMSList[0].checked = true;
            cls.updateDocumentRecords();
        Test.StopTest();

          /* ums.updateMileStoneRecords();
           Required_Documents__c k=new Required_Documents__c();
                k.selectMS= true; */
               
            }
           
           // ums.updateDocumentRecords(); 
            
            }
       //Test.stopTest();
     
    }


Am getting error "Constructor not defined: [updatemilestone].<Constructor>(ApexPages.StandardController) "
Rahul Sangwan7341Rahul Sangwan7341
hey you are taking same name of your Class and Test Class.
shobana shobanashobana shobana
Thank you for your replay.Now its not showing any error but test is not pass beacuse in that object 
workflow is used.
Can you give me a guidelines to solve this issue.
It's showing the error "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, A workflow or approval field update caused an error when saving this record. Contact your administrator to resolve it.
Required fields are missing: [Name]: []"
Rahul Sangwan7341Rahul Sangwan7341
Check all the fields which are required and after that also if same error is missing check from debug log if you able to get workflow name where it is coming and did you inserted case reference in required documents.
SF AdminSF Admin
Hi Shobana,

While inserting the record, you are not adding required field in the code. please add required fields also.
Required_Documents__c m1=new Required_Documents__c(Name='test2');
    m1.Date_Received__c = System.today(); 
  //You are adding single field.
  // Add all the required fields here in the code. and you will be able to insert the record successfully.


    insert m1;
Regards,