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 

Hi friends i want writa a trigger to create a new team member but i getting a issue while i am changing the implating physician field value it not going right please help me on this

public static void PhysicianOnCareTeamAfterOrderStatusUpdate(map<Id, Order> newMap, map<Id, Order> oldMap){
        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 : newMap.values())
        {
            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 : newMap.values())
        {
            // for permanent type
            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'));
            }
        }
        if(!mapCaseTeam.isEmpty()){
            insert mapCaseTeam;
        }
    }
HARILAKSHMI M 8HARILAKSHMI M 8
Hi,
Can you provide additional details about the issue you are facing while implementing the trigger?
HARILAKSHMI M 8HARILAKSHMI M 8
I see while querying the user record, in where condition you used ContactId = :setOfPhysicianId (this will fetch only one record ). Instead try to implement it in below mentioned way and let me know if it works.
 List<User> userlist = [SELECT Id, Name, ContactId FROM User WHERE ContactId IN :setOfPhysicianId];

for(User user :userlist) {
            mapOfUserId.put(user.ContactId, user);
        }