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
PRADEEP YADAV 5PRADEEP YADAV 5 

Write a program to get the total number of records present in an object(Object should be passed as an argument)

Write a program to get the total number of records present in an object(Object should be passed as an argument).Problem dML
Best Answer chosen by PRADEEP YADAV 5
chanchal_:)chanchal_:)
Hi Pradeep,
You can write this code and can pass any object name to get number of total records. 
getTotalRecords.getObjectName('Contact');

public class getTotalRecords {
    public static integer getObjectName(string objName){
        string sQuery = 'Select Id from ' + objName;
        List<Sobject> lstObject = database.query(sQuery);
        return lstObject.size();
    }
}

 

All Answers

chanchal_:)chanchal_:)
Hi Pradeep,
You can write this code and can pass any object name to get number of total records. 
getTotalRecords.getObjectName('Contact');

public class getTotalRecords {
    public static integer getObjectName(string objName){
        string sQuery = 'Select Id from ' + objName;
        List<Sobject> lstObject = database.query(sQuery);
        return lstObject.size();
    }
}

 
This was selected as the best answer
PRADEEP YADAV 5PRADEEP YADAV 5
Thanks Sir