0

Here i am trying to get owner name value from child object incident , OwnerId is populating correctly but when i tried with Owner.Name, value is coming null in debug log.

Please find my piece of code below and suggest me how to get incident Owner Name in case record Incident_Owner__c field.(Parent : case Child : Incident)

trigger Updateincident on BMCServiceDesk__Incident__c (after insert, after update) 
{ 
  //You want it on update too, right?
  Map<ID, Case> parentCases = new Map<ID, Case>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (BMCServiceDesk__Incident__c childObj : Trigger.new) {
       listIds.add(childObj.Cases__c);
       System.debug('@@@@@@@@@@@@'+listIds);
  }

  parentCases = new Map<Id, Case>([SELECT id,Helpdesk_Case_Status__c,Helpdesk_Case_Resolution__c,(SELECT ID,Name, BMCServiceDesk__FKStatus__c,Owner.Name,BMCServiceDesk__Status_ID__c,BMCServiceDesk__incidentResolution__c FROM Incidents__r) FROM Case WHERE ID IN :listIds]);
      System.debug('@@@@@@@@@@@@'+parentCases );

  for(BMCServiceDesk__Incident__c inc: Trigger.new){
       Case myParentCse = parentCases .get(inc.Cases__c);
       myParentCse.Helpdesk_Case_Resolution__c = inc.BMCServiceDesk__incidentResolution__c;
       myParentCse.Helpdesk_Case_Status__c = inc.BMCServiceDesk__Status_ID__c;
       myParentCse.Incident_Owner__c = inc.Owner.Name;
        System.debug('@@@@@@@@@@@@'+myParentCse.Incident_Owner__c);
       myParentCse.Incident_Number__c = inc.Name;

       System.debug('@@@@@@@@@@@@'+myParentCse.Helpdesk_Case_Resolution__c);
  }
     update parentCases.values();
     System.debug('@@@@@@@@@@@@'+parentCases.values());
}
  • Your Cases can have many BMCService Incidents how do you know which BMCService incident data should be on the case? – EricSSH Apr 24 '18 at 14:11
0

This is one of those questions I would want to debug out myself and test a little bit but I believe you can do this, you have the Incidents in your parent case map, so if you loop through your incidents__r that are inside your parent cases you have access to Owner.Name. So battery.cords answer is helpful I believe you could do it this way also

trigger Updateincident on BMCServiceDesk__Incident__c (after insert, after update) 
{ 
    Map<ID, Case> parentCases = new Map<ID, Case>(); //Making it a map instead of list for easier lookup
    Set<ID> bmsdID = new Set<ID>();

    for(BMCServiceDesk__Incident__c childObj : Trigger.new) 
    {
        bmsdID.add(childObj.Cases__c);
    }

    parentCases = new Map<Id, Case>([SELECT id,Helpdesk_Case_Status__c,Helpdesk_Case_Resolution__c,
        (SELECT ID,Name, BMCServiceDesk__FKStatus__c,Owner.Name,BMCServiceDesk__Status_ID__c,BMCServiceDesk__incidentResolution__c FROM Incidents__r) FROM Case WHERE ID IN :bmsdID]);

    for(ID caseID : parentCases.keySet())
    {
        for(Incidents__r incidents : parentCases.get(caseID).Incidents__r)
        {
            parentCases.get(caseID).Helpdesk_Case_Resolution__c = incidents.BMCServiceDesk__incidentResolution__c;
            parentCases.get(caseID).Helpdesk_Case_Resolution__c = incidents.BMCServiceDesk__incidentResolution__c;
            parentCases.get(caseID).Helpdesk_Case_Status__c = incidents.BMCServiceDesk__Status_ID__c;
            parentCases.get(caseID).Incident_Owner__c = incidents.Owner.Name;
            parentCases.get(caseID).Incident_Number__c = incidents.Name;
        }
    }

    update parentCases.values();
}
| improve this answer | |
3

Related fields in trigger context are null unless queried explicitly. Your options are to either:

  1. Query your users based on the values from OwnerId
  2. Create a formula field Owner_Name__c, which returns the text of Owner.Name, allowing to you use the formula in your trigger.

I usually prefer a formula over the query, since its cheaper, and you can re-use it on search layouts or related lists.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.