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 

is somthing wrong in this code i getting null pointer exception while assigning member_c

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>();
        for(Order objOrder : newList){
            setOfPhysicianId.add(objOrder.implanting_physician__c ); 
        }
        Map<String, String> mapOrderType_Role= new Map<String, String>{'NROP'=>'Trialing Physician', 'NROP'=>'Implanting Physician',
            'NREP'=>'Replacement Physician', 'NREV'=>'Revision Physician'};
                if(setOfPhysicianId.isEmpty()){
                    for(User user : [SELECT Id, Name, ContactId FROM User WHERE ContactId =: setOfPhysicianId]){
                        mapOfUserId.put(user.ContactId, user);
                    }
                }
        for(Order objOrder : newList){
            if(objOrder.implanting_physician__c != null && mapOrderType_Role.containskey(objOrder.Type)){
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = mapOrderType_Role.get(objOrder.Type), 
                                                Status__c = 'Active'));
            }
            /*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'));
            }
            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;
        }
    }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Krishna,

For the below If condition just add another condition as below.
 
if(objOrder.implanting_physician__c != null && mapOrderType_Role.containskey(objOrder.Type) && mapOfUserId.containskey(objOrder.implanting_physician__c) ){

Let me know if you still face the issue and also the line of error you are getting

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

Thanks,
 
mukesh guptamukesh gupta
Hi Krishna,

Please use below code:-
 
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>();
        for(Order objOrder : newList){
            setOfPhysicianId.add(objOrder.implanting_physician__c ); 
        }
        Map<String, String> mapOrderType_Role= new Map<String, String>{'NROP'=>'Trialing Physician', 'NROP'=>'Implanting Physician',
            'NREP'=>'Replacement Physician', 'NREV'=>'Revision Physician'};
                if(!setOfPhysicianId.isEmpty()){
                    for(User user : [SELECT Id, Name, ContactId FROM User WHERE ContactId =: setOfPhysicianId]){
                        mapOfUserId.put(user.ContactId, user);
                    }
                }
        for(Order objOrder : newList){
            if(objOrder.implanting_physician__c != null && mapOrderType_Role.containskey(objOrder.Type) && mapOfUserId.size() > 0){
			  if(objOrder.implanting_physician__c != null){		
                mapCaseTeam.add(new CareTeam__c(Patient__c = objOrder.patient_account__c, 
                                                Member__c = mapOfUserId.get(objOrder.implanting_physician__c).Id,
                                                Roles__c = mapOrderType_Role.get(objOrder.Type), 
                                                Status__c = 'Active'));
				}								
            }
            /*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'));
            }
            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;
        }
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh