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 );
}
}
}
}
}
b.SOD_AUTO_KEY__c
andROI.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;
?