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
A GunaleA Gunale 

how to write Test class for the below apex class

public static void createRecords(Map<Id, AccountTeamMember> accountMp) {
        
        Map<Id, Account> accountMap;
        List<xyz__c> Records = new List<xyz__c>();
        if(!accountMp.isEmpty()) {
            accountMap = new Map<Id, Account> ([SELECT Id, RecordType.Name, 
                                                        (Select Id, Status,AccountId 
                                                         FROM abc__c 
                                                         )
                                               FROM Account 
                                               WHERE Id IN: accountMp.keySet()]);
                                              
                                               
                                                   
                                                 
            for(Id accId : accountMp.keySet()) {
                Account accObj = accountMap.get(accId);
                Id userId = accountMp.get(accId).UserId;
                xyz__c  xyz = new xyz__c();
                xyz.Record__c = accObj.Id;
                xyz.Record1__c = userId;
                xyz.Record2__c = 'Account';
                xyz.External_Id__c = userId+ '_'+accObj.Id;
                Records.add(xyz);
                
                for(abc__c abc: accObj.abc__c){
                    xyz__c  xyz1 = new xyz__c();
                    xyz1.Rcordxyz__c = abc.Id;
                    xyz1.Rcordxyz1__c = userId;
                    xyz1.Rcordxyz2__c = 'abc';
                    xyz1.External_Id__c = userId+ '_'+abc.Id;
                    Records.add(xyz1);
                }                
            }  
                                                
            if(!Records.isEmpty()) {
                try{
                    insert Records;
                } 
                catch(Exception e){
                }
            }
        }  
PriyaPriya (Salesforce Developers) 
Hey,

Have you created any test class already? If yes, please provide and specify the error so that we can help you with it.

Note :- 

The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question.




Thanks!

Salesforce Support
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

If you face any specific issue while attempting do post that here.

Thanks!