I have a trigger that is being tripped every time a Order is changed from draft to active and vise versa. I have a conditional inside my trigger that runs a query and saves the output into a Map. When I do a system debug on the Map the value being returned seems to be the foreign key.

trigger OrderActive on Order (after insert, after update){

    for(Order ord : Trigger.New)
        If(ord.Status == 'Activated')
            Map<String, Order> m = new Map<String, Order>([
                select Account.Name from Order WHERE Id IN :Trigger.new




I keep getting the output in the image above

USER_DEBUG [12]|DEBUG|{8018A0000002fJYQAY=Order:{AccountId=0018A00000KvRm8QAF, Id=8018A0000002fJYQAY}}

When I run the same query in the Query Editor I get the actual name of the account instead of 0019A00000KvRm9QBF. How do I get my query in my trigger to pump out the name instead of the foreign key? Would a query in a trigger affect the query output?

  • Are you trying to figure out how to use this information later or just how to get it into your logs? – Adrian Larson Nov 25 '19 at 19:28
  • System.debug() doesn't show values on related objects. Your logic is fine. – David Reed Nov 25 '19 at 19:37
  • @Adrian Larson I'm trying to store the information to pass it in a JSON wrapper. I'm trying to get the name of the account not the AccountId. I've tried different variations in the query editor such as "SELECT Account.Name FROM Order" in the query editor and in my code. The output from the query editor gives the actual name of the account. The output from my code gives me the the numerical values above. – Richard Nov 25 '19 at 19:37
  • Within your code itself, you can just pull the value for a specific Order.Account.Name. But it is not clear what your end goal is, so it is difficult to help you achieve that. – Adrian Larson Nov 25 '19 at 19:50
  • This code is highly problematic; it includes an SOQL query within a loop so this will fail if more than 99 orders get inserted or updated in one transaction (or quicker if other queries occur elsewhere in the same transaction). – Phil W Nov 25 '19 at 19:52

The trigger needs to collect the details differently. Try:

trigger OrderActive on Order (after insert, after update) {
    List<Id> activatedOrderIds = new List<Id>();
    List<String> accountNames = new List<String>();

    // Get all the inserted/updated activated order's IDs
    for (Order order : Trigger.new) {
        if (order.Status == 'Activated') {

    // If there were some then get the related account names
    if (!activatedOrderIds.isEmpty()) {
        for (Order order : [SELECT Account.Name FROM Order WHERE Id IN :activatedOrderIds]) {
            // Adding each to a set, to get unique names

    // At this point accountNames contains the set of unique account names

NB: If you want to only handle those orders that have been created as activated or when the order status CHANGES to activated you will need to handle that in the code too. Right now this collects details even for previously activated orders.

  • @Richard the query remains but you need to bulkify it like shown here. The latter part of extracting account names may not be what you want, but still you must bulkify... – Phil W Nov 25 '19 at 20:16
  • I actually want the account name. The main thing I want is to be able to see the account name along with a few more key fields and be able serialize them so I can send them over. Let me play around with your above code but I think this is what I needed. – Richard Nov 25 '19 at 20:44

If you serialize the information, it will be there in the output string. Note that the information is stored on the Order variable in memory, but when you just debug your Map<String, Order>, it uses implicit type coercion to String and what you see is not necessarily what you get.

// {..., "Account": {..., "Name": "Some Account Name", ...}, ...}
  • Is there an alternative way to view the values in the variable? – Richard Nov 25 '19 at 19:50
  • @rich For a particular record, you could system.debug(record.Account.Name). Surely your end goal is not just to view the data, though? – Adrian Larson Nov 25 '19 at 19:51
  • My end goal is to send the data through web services to another system as a JSON structure. I just want to see the Account name coming through. I guess I could shoot the structure over and see it populate on the other end. – Richard Nov 25 '19 at 20:03
  • If you serialize this Map, the data will be there. I put in my code snippet a comment illustrating the structure. – Adrian Larson Nov 25 '19 at 20:09

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.