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
Krishna Sahu 1Krishna Sahu 1 

Please help me to write test class on that

public static void populatePhysicianOnCareTeam(List<Order> newList){
        List<CareTeam__c> mapCaseTeam = new List<CareTeam__c>();
        Set<Id> setOfPhysicianId = new Set<Id>();
        Map<Id, User> mapOfUserId = new Map<Id, User>();
        System.debug('Outside the Loop');
        for(Order objOrder : newList){
            setOfPhysicianId.add(objOrder.implanting_physician__c ); 
        }
        for(User user : [SELECT Id, Name, ContactId FROM User WHERE ContactId =: setOfPhysicianId]){
            mapOfUserId.put(user.ContactId, user);
        }
        for(Order objOrder : newList){
            // for trail
            if(objOrder.implanting_physician__c != null && objOrder.Type == 'NROT'){
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = 'Trialing Physician', 
                                                Status__c = 'Active'));
            }
            // for permanent
           else if(objOrder.implanting_physician__c != null && objOrder.Type == 'NROP'){
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = 'Implanting Physician', 
                                                Status__c = 'Active'));
            }
            // for replacement physician
            else if(objOrder.implanting_physician__c != null && objOrder.Type == 'NREP'){
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = 'Replacement Physician', 
                                                Status__c = 'Active'));
            }
            else if(objOrder.implanting_physician__c != null && objOrder.Type == 'NREV'){
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = 'Revision Physician', 
                                                Status__c = 'Active'));
            }
        }
        if(!mapCaseTeam.isEmpty()){
            insert mapCaseTeam;
        }
    }
Best Answer chosen by Krishna Sahu 1
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class which gives you 100% coverage.
 
@istest
public class populatePhysicianOnCareTeam123Test {

     static testMethod void myTest() {
         
         UserRole userrole = [Select Id, DeveloperName From UserRole Where DeveloperName = 'CEO' Limit 1];

User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' Limit 1];

adminUser.UserRoleId = userRole.Id;
update adminUser;
         System.runAs(adminUser){
         Id pr = [select id from profile where name='Partner Community User'].id;
          Account a = new Account(Name='Test Account Name');
    insert a;

    Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
    insert c;

    User user = new User();
    user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
    user.EmailEncodingKey = 'ISO-8859-1';
    user.LanguageLocaleKey = 'en_US';
    user.TimeZoneSidKey = 'America/New_York';
    user.LocaleSidKey = 'en_US';
    user.FirstName = 'first';
    user.LastName = 'last';
    user.Username = 'test@uniquedomain.com';
    user.CommunityNickname = 'testUser123';
    user.Alias = 't1';
    user.Email = 'no@email.com';
    user.IsActive = true;
    user.ContactId = c.Id;

    insert user; 
         

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Patient_Account__c=a.id;
    o.Implanting_physician__c=c.id;
    o.Pricebook2Id =  pricebookId ;
             o.Type='NROT';
    
    insert o;
                 Order o1 = new Order();
    o1.Name = 'Test Order ';
    o1.Status = 'Draft';
    o1.EffectiveDate = system.today();
    o1.EndDate = system.today() + 4;
    o1.AccountId = a.id;
    o1.Patient_Account__c=a.id;
    o1.Implanting_physician__c=c.id;
    o1.Pricebook2Id =  pricebookId ;
             o1.Type='NROP';
    
    insert o1;
                 Order o2 = new Order();
    o2.Name = 'Test Order ';
    o2.Status = 'Draft';
    o2.EffectiveDate = system.today();
    o2.EndDate = system.today() + 4;
    o2.AccountId = a.id;
    o2.Patient_Account__c=a.id;
    o2.Implanting_physician__c=c.id;
    o2.Pricebook2Id =  pricebookId ;
             o2.Type='NREP';
    
    insert o2;
                 Order o3 = new Order();
    o3.Name = 'Test Order ';
    o3.Status = 'Draft';
    o3.EffectiveDate = system.today();
    o3.EndDate = system.today() + 4;
    o3.AccountId = a.id;
    o3.Patient_Account__c=a.id;
    o3.Implanting_physician__c=c.id;
    o3.Pricebook2Id =  pricebookId ;
             o3.Type='NREV';
    
    insert o3;
     }
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class which gives you 100% coverage.
 
@istest
public class populatePhysicianOnCareTeam123Test {

     static testMethod void myTest() {
         
         UserRole userrole = [Select Id, DeveloperName From UserRole Where DeveloperName = 'CEO' Limit 1];

User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' Limit 1];

adminUser.UserRoleId = userRole.Id;
update adminUser;
         System.runAs(adminUser){
         Id pr = [select id from profile where name='Partner Community User'].id;
          Account a = new Account(Name='Test Account Name');
    insert a;

    Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
    insert c;

    User user = new User();
    user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
    user.EmailEncodingKey = 'ISO-8859-1';
    user.LanguageLocaleKey = 'en_US';
    user.TimeZoneSidKey = 'America/New_York';
    user.LocaleSidKey = 'en_US';
    user.FirstName = 'first';
    user.LastName = 'last';
    user.Username = 'test@uniquedomain.com';
    user.CommunityNickname = 'testUser123';
    user.Alias = 't1';
    user.Email = 'no@email.com';
    user.IsActive = true;
    user.ContactId = c.Id;

    insert user; 
         

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Patient_Account__c=a.id;
    o.Implanting_physician__c=c.id;
    o.Pricebook2Id =  pricebookId ;
             o.Type='NROT';
    
    insert o;
                 Order o1 = new Order();
    o1.Name = 'Test Order ';
    o1.Status = 'Draft';
    o1.EffectiveDate = system.today();
    o1.EndDate = system.today() + 4;
    o1.AccountId = a.id;
    o1.Patient_Account__c=a.id;
    o1.Implanting_physician__c=c.id;
    o1.Pricebook2Id =  pricebookId ;
             o1.Type='NROP';
    
    insert o1;
                 Order o2 = new Order();
    o2.Name = 'Test Order ';
    o2.Status = 'Draft';
    o2.EffectiveDate = system.today();
    o2.EndDate = system.today() + 4;
    o2.AccountId = a.id;
    o2.Patient_Account__c=a.id;
    o2.Implanting_physician__c=c.id;
    o2.Pricebook2Id =  pricebookId ;
             o2.Type='NREP';
    
    insert o2;
                 Order o3 = new Order();
    o3.Name = 'Test Order ';
    o3.Status = 'Draft';
    o3.EffectiveDate = system.today();
    o3.EndDate = system.today() + 4;
    o3.AccountId = a.id;
    o3.Patient_Account__c=a.id;
    o3.Implanting_physician__c=c.id;
    o3.Pricebook2Id =  pricebookId ;
             o3.Type='NREV';
    
    insert o3;
     }
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Krishna Sahu 1Krishna Sahu 1
Hi Praveen
It is working
Thank you