You need to sign in to do that
Don't have an account?
kshannon
Apex Trigger de-reference null object when created by API, but not with data import/native ui
I have a trigger that is called before insert on "Revenue__c" object. Simply put it is checking the newly entered "Canvas_Id__c" field to match within a custom setting and then take the value from the custom setting and apply it to the Canvas__c field. Pretty straight forward and works with dataloader and native creation, but when running from API call, it seems that it is not able to reference the custom setting details/
Running on command line API I receive error of:
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.RevenueCanvasName: line 6, column 1
Is there a difference referencing custom settings via the API? This has me stumped...
trigger RevenueCanvasName on Revenue__c (before insert, before update) { public static List<Revenue__c> updateRevenueList; for (Integer i = 0; i < Trigger.new.size(); i++){ if(Trigger.new[i].Canvas_Id__c != null || Trigger.new[i].Canvas_Id__c != Trigger.old[i].Canvas_Id__c){ String cid = String.valueOf(Trigger.new[i].Canvas_Id__c); Trigger.new[i].Canvas__c = Canvas__c.getInstance(''+cid+'').Canvas__c; } } }
Running on command line API I receive error of:
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.RevenueCanvasName: line 6, column 1
Is there a difference referencing custom settings via the API? This has me stumped...
String cid = String.valueOf(Trigger.new[i].Canvas_Id__c.intvalue());
This way it ensures that the referenced number is not a decimal and will in fact find the appropriate associated custom setting. Appreciate the help again on this! Canvas_Id__c is a number with no decimal places, but apparently when referencing it in the trigger via API call it's coming out as a decimal.
All Answers
Please use the below code:
Please do let me know if it helps you.
Regards,
Mahesh
Revenue__c.create({RecordTypeId: "xxxxxxxx", Canvas_Id__c: 3, Account__c: "xxxxxx"}) has the issue where the trigger sees Canvas_Id__c as null only when calling from API for some reason. Do you know if the trigger doesn't function the same if you call from a rest API? Very strange as the dataloader is using the API call I would assume.
Option 1:
Please make sure that API is sending the information for Canvas_Id__c field.
Option 2:
Please make sure that the corresponding Custom Settings is available in the Org.
Option 3:
Please make sure that the Interface user is having permission for Canvas_Id__c field.
Regards,
Mahesh
The Custom Setting is properly setup
Permissions are all fine, as I said I was able to load the same data by just using Data Loader. I'll have to dig in and see if it's an issue with the API call itself..
Please take the below code and provide the debug messages:
If the Canvas_Id__c is an SFDC Id then it may be sending 15 digits vs 18 digits is one of the reason not getting the Custom Settings.
Please do let me know if it helps you.
Regards,
Mahesh
String cid = String.valueOf(Trigger.new[i].Canvas_Id__c.intvalue());
This way it ensures that the referenced number is not a decimal and will in fact find the appropriate associated custom setting. Appreciate the help again on this! Canvas_Id__c is a number with no decimal places, but apparently when referencing it in the trigger via API call it's coming out as a decimal.