3

I'm kind of new in Apex development and I'm writing a before insert, before update trigger to a custom object that is going to be upserted through an API integration.

Is very basic code but I don't see what I'm doing wrong, I need an extra couple of eyes.

Here the code:

    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

    Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

    for (RO_Item__c ROI :Trigger.New){

        system.debug('<---New Repair Order Item--->');

        if (ROI.SOD_AUTO_KEY__c!=Null){


                    m.put(ROI.SOD_AUTO_KEY__c, ROI);
                    system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


        }


    }

    //Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
    List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
                                 FROM SO_Item__c
                                 WHERE SOD_AUTO_KEY__c in :m.keySet()  ];

    //Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
    for(SO_Item__c b :soilst){

        if (b.SOD_AUTO_KEY__c!=Null){

                    m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
                    system.debug('SO ID to be inserted:'+ b.SOId__c );

        }


    }

}

I've tried debugging it and I'm getting the NullPointerException on the line containing: m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;

What am I doing wrong?

Thanks in advance.

Edit:

So thanks to the comments below I saw the light and was able to get this working. Basically I changed the last part of the code because I re-analyzed my use case and something was broke in the logic. I had to double check the info coming over to make sure that nothing was null (and nothing was coming null) but the error was how I was extracting the information from the lists.

I know it may not be the most efficient code ever but I'm going to be improving this in later on.

I'm going to post the new code below but I want to explain the use case if anyone needs this kind of triggers in the future (for beginners).

In my use case I have Repair Orders which are coming from an ERP system and I need them in SFDC to create dashboards, report over that information and basically have that information related to our customers and on the go for our employees.

Each Repair Order Item (generic to a Quote Line Item) may have a Sales Order Item associated. This Sales Order Item has a unique key on the database and I have that as a field in a different object as an external key. This code checks whenever a Repair Order Item is inserted/updated if the Sales Order Detail (generic to Item), if any, exists in Salesforce and retrieves the Sales Order Header ID so we can have a report of Related Repair Order Items with Sales Order.

The code is the following:

    trigger ROIDUpdate on RO_Item__c (before insert, before update) {


//Setting the mapping

    Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();

    for (RO_Item__c ROI :Trigger.New){

        system.debug('<---New Repair Order Item--->');

        if (ROI.SOD_AUTO_KEY__c!=Null){


                    m.put(ROI.SOD_AUTO_KEY__c, ROI);
                    system.debug('Map key:'+ m.get(ROI.SOD_AUTO_KEY__c) );


        }


    }

    //Looking for the Sales Order Item that matches the Repair Order SOD Auto Key
    List<SO_Item__c> soilst = [SELECT SOId__c, SOD_AUTO_KEY__c
                                 FROM SO_Item__c
                                 WHERE SOD_AUTO_KEY__c in :m.keySet()  ];

    //Write the ID of the Sales Order into the Repair Order. Field SO_ID__c under RO_Item__c
    for(SO_Item__c b :soilst){

        for(RO_Item__c ROIitem:Trigger.New){

            if (b.SOD_AUTO_KEY__c!=Null){

                if (ROIitem.SOD_AUTO_KEY__c==b.SOD_AUTO_KEY__c){

                    ROIitem.SO_ID__c= b.SOId__c;
                    system.debug('SO ID to be inserted:'+ b.SOId__c );

                }



            }

        }


    }

}
  • Are you sure that the values of b.SOD_AUTO_KEY__c and ROI.SOD_AUTO_KEY__c will be always same? Are you getting exception in this line -- m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;? – Jayant Das Dec 13 '18 at 20:41
  • Yes, they are always going to be the same. They are the database keys for the same record. – Alejandro Flores Dec 15 '18 at 15:21
5

Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE.

When debugging NPEs, I like to keep in mind the famous saying of Sir Arthur Conan Doyle's Sherlock Holmes

when you eliminate the impossible, whatever remains, however improbable, must be the truth

An NPE occurs when you try to do something like null.method() or null.variable.
Something in your indicated statement is null. We need to suss out what that something is.

Given the statement

m.get(b.SOD_AUTO_KEY__c).SO_ID__c = b.SOId__c;

The things that might possibly be null are:

  • Your map m
  • Your variable b
  • Whatever you get as a result of m.get(b.SOD_AUTO_KEY__c)

As a side note, these variable names should probably be re-evaluated. Naming the variables in a way that describes what they are/do will help other people understand your code (and will help you understand your code, once enough time has passed that you forget exactly what you did).

Something like m => autoKeyToSOItemMap and b => soItem

Getting back on topic, m cannot possibly be null, because you initialize this variable in the same line on which it is declared. Map<Decimal,RO_Item__c> m = new Map<Decimal,RO_Item__c>();. It can be an empty map, but empty and null are different things.

Likewise, b cannot possibly be null. This is the loop variable of your for loop. b is fed from soilist, which itself is populated with results from a query. A List<SO_Item__c> can contain null records, but the results of a query will never return a null record. Further evidence for this is that you execute b.SOD_AUTO_KEY__c != Null on the line above the one that your error is indicated on.

So, that leaves us with one possibility. Whatever the value of b.SOD_AUTO_KEY__c in the iteration of your loop that you're failing on, this value is not contained in your map m. This causes m.get(b.SOD_AUTO_KEY__c) to evaluate to null, and null.SO_ID__c is what throws your exception.

  • Thank you, this helped a lot checking and debugging the code to check for failure sin the logic. Detailed and thorough explanation. I edited the original post with the final solution. – Alejandro Flores Dec 18 '18 at 15:40
4

If you cannot guarantee that a value is not null, you typically add a null check:

MyObject__c record = myMap.get(someKey);
if (record != null)
{
    record.MyField__c = someValue;
}
  • You just need some light and hopefully obvious renaming to fit this approach into your current code. – Adrian Larson Dec 13 '18 at 20:45
  • Thank you, I'm going to review everything again and I'll get back with my findings. – Alejandro Flores Dec 15 '18 at 15:22
1

Your issue seems to be on this line:

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;

In your code, you have added entries in your map as below:

m.put(ROI.SOD_AUTO_KEY__c, ROI);

Now, when you are iterating over soilst and trying to fetch the key as:

m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;

The b.SOD_AUTO_KEY__c may not be present in the Map, and thus it would return null, causing an exception when you try to fetch SO_ID__c from a null value.

You should instead try something as (change as required, but the intent is to check for null value before performing any operation on it):

if(m.get(b.SOD_AUTO_KEY__c) != null) {
    m.get(b.SOD_AUTO_KEY__c).SO_ID__c= b.SOId__c;
 }
  • 1
    You should avoid inefficient (duplicative) m.get calls. Cache the result before checking for null. – Adrian Larson Dec 13 '18 at 20:45
  • That's correct, this was direct intended to avoid the exception, not going into best practices. I would rather fetch the record and perform operations on it. – Jayant Das Dec 13 '18 at 20:46
-1

Check for

if((m.get(b.SOD_AUTO_KEY__c)) != null){

}

Try to debug m and inspect the keys.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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