-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
2Replies
Null Pointer Exception using parent relationship reference.
I have a custom object (DM__C that is a child of Opportunity (lookup relationship). I am trying to update values on my custom object (DM__C) based on the user fields on the owner of the parent opportunity object. When I try using the opportunity relationship (DM_opportunity_ID__r.ownerid), I receive the following error message:
System.NullPointerException: Attempt to de-reference a null object: Trigger.NSPUserValuesUpdate: line 27, column 77
The line it is erroring on is: salesChannelFromUser = ownerMap.get(nsp.DM_Opportunity_ID__r.OwnerId).BMG_Sales_Channel__c;
How can I reference the parent relationship without getting this error?
trigger NSPUserValuesUpdate on DM__c (before insert, before update) {
if (Trigger.isInsert || Trigger.isUpdate) {
Set<Id> ownerIds = new Set<Id>();
for (dm__c nsp : Trigger.New) {
ownerIds.add(nsp.DM_Opportunity_ID__r.OwnerId);
}
System.debug('ownerIds: ' + ownerIds);
Map<Id, User> ownerMap = new Map<Id, User>([Select Id, BMG_Sales_Channel__c,vp__c,director__c from User where Id in :ownerIds]);
System.debug('ownerMap: ' + ownerMap);
String salesChannelFromUser;
String salesChannelFromUserVp;
String salesChannelFromUserDir;
for (dm__c nsp : Trigger.New) {
salesChannelFromUser = ownerMap.get(nsp.DM_Opportunity_ID__r.OwnerId).BMG_Sales_Channel__c;
salesChannelFromUserVp = ownerMap.get(nsp.OwnerId).vp__c;
salesChannelFromUserDir = ownerMap.get(nsp.OwnerId).director__c;
if (Trigger.isInsert) {
nsp.BMG_Sales_Channel__c = salesChannelFromUser;
nsp.vp__c = salesChannelFromUserVp;
nsp.director__c = salesChannelFromUserDir;
}
else if (Trigger.isUpdate) {
if (nsp.BMG_Sales_Channel__c != salesChannelFromUser
|| nsp.vp_test__c != salesChannelFromUserVp
|| nsp.director_test__c != salesChannelFromUserDir) {
nsp.BMG_Sales_Channel__c = salesChannelFromUser;
nsp.vp_test__c = salesChannelFromUserVp;
nsp.director_test__c = salesChannelFromUserDir;
}
}
}
}
}
Thanks, in advance, for your help.
- jasonrtaylor
- October 24, 2010
- Like
- 0
- Continue reading or reply
Trigger not updating
I am trying to update the parent opportunity record with a count of all child records that meet a specified criteria when a new child custom object is created or modified. I have checked the SOQL queries and they are returning the results I expected. The opportunity relationship field is present and required.
trigger UpdatePendingPOMRequests on DM__c (after insert, after update) {
//Find ID for 'POM' Record Type
RecordType rt = [Select r.Id from RecordType r WHERE r.name = 'POM' AND r.SobjectType = 'DM__c'];
for(DM__c dm : trigger.new){
if(dm.RecordTypeId == rt.id){
integer dmct = [Select Count()
FROM DM__c d
WHERE dm__c.dm_opportunity_id__c = :dm.dm_opportunity_id__c
AND dm__c.RecordTypeId = :rt.id
AND dm__c.POM__c = 'Pending'
];
Opportunity oid = [Select Num_Pending_NSP__c from Opportunity WHERE id =:dm.dm_opportunity_id__c ];
oid.Num_Pending_NSP__c = dmct;
Database.upsert(oid);
}
}
}
Thanks, in advance for your response.
- jasonrtaylor
- August 11, 2010
- Like
- 0
- Continue reading or reply
TOO Many SOQL Queries on Trigger
I receive the following error when trying to use the data loader to insert records into the database. I am inserting data into a cross-reference table, but creating the related customer and account records if they do not already exist then inserting the record, because the cross reference table is a child of customer_data and account_data. I believe my issue comes from the for loop, but I don't know how to structure the trigger otherwise.
Thanks for your help.
ERROR:
insertCustomerAccount: execution of BeforeInsert
caused by: System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertCustomerAccount: execution of BeforeInsert
caused by: System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertCustomerAccount: execution of BeforeInsert
caused by: System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertCustomerAccount: execution of BeforeInsert
caused by: System.Exception: Too many SOQL queries: 21
Trigger.insertCustomerAccount: line 119, column 34: []
Trigger.insertCustomerAccount: line 221, column 13: []
Trigger.insertCustomerAccount: line 221, column 13: []
Trigger.insertCustomerAccount: line 221, column 13
trigger insertCustomerAccount on Customer_Account_Link__c (before insert) { Customer_Data__c CustID; Account_Data__c AcctID; Customer_Account_Link__c CustAcctLinkID; Customer_Account_Link__c CustNum; Customer_Account_Link__c AcctNum; integer custMatch; integer acctMatch; string customerID; string customerNum; string accountID; Database.upsertResult CustResult; Database.upsertResult AcctResult; // Account_Data__c AcctID; for(Customer_Account_Link__c custAcctLink :Trigger.new) { //Check to see if a customer record already exists custMatch = [SELECT count() FROM Customer_Data__c c WHERE c.X_CUSTOMER_SOURCE_REF_ID__c =:custAcctLink.Customer_id__c]; // Get Customer ID if it exists if(custMatch > 0) { CustID = [SELECT c.id, c.X_CUSTOMER_SOURCE_REF_ID__C FROM Customer_Data__c c WHERE c.X_CUSTOMER_SOURCE_REF_ID__c=:custAcctLink.Customer_id__c LIMIT 1]; customerID = CustID.Id; customerNum = CustID.X_CUSTOMER_SOURCE_REF_ID__C; } else { CustId = new Customer_Data__c(); CustId.Account_Number__c = custAcctLink.account_number__c; CustId.Address_1__c = custAcctLink.Address_Line_1__c; CustId.Address_2__c = custAcctLink.Address_Line_2__c; CustId.City__c = custAcctLink.City__c; CustId.State__c = custAcctLink.State__c; CustId.X_Postal_Code__c = custAcctLink.Postal_Code__c; CustId.Last_Updated_Timestamp__c = custAcctLink.Last_Updated_Timestamp__c; CustId.Home_Phone__c = custAcctLink.Home_Phone__c; CustId.Work_Phone__c = custAcctLink.Work_Phone__c; CustId.Mobile_Phone__c = custAcctLink.Mobile_Phone__c; CustId.Fax__c = custAcctLink.Fax__c; CustId.Taxpayer_Identification_Number__c = custAcctLink.Taxpayer_Identification_Number__c; CustId.Fax__c = custAcctLink.Fax__c; CustId.X_CUSTOMER_SOURCE_REF_ID__C = custAcctLink.Customer_id__c; CustResult = Database.upsert(CustId); customerID = CustResult.Id; customerNum = CustId.X_CUSTOMER_SOURCE_REF_ID__C; } //Check to see whether the account record exists acctMatch = [SELECT count() FROM Account_Data__c c WHERE c.X_ACCOUNT_SOURCE_REF_ID__c =:custAcctLink.Account_Number__c]; //If account exists, obtain the ID from the record with the matching account number. if(acctMatch > 0) { AcctId = [SELECT a.id FROM Account_Data__c a WHERE a.X_ACCOUNT_SOURCE_REF_ID__c=:custAcctLink.ACCOUNT_NUMBER__c LIMIT 1]; accountID = AcctId.Id; } else { //Otherwise insert the account record and get the account id AcctId = new Account_Data__c(); if(custMatch > 0) { AcctId.Customer__c = customerID; AcctId.CustomerID__c = customerNum; } else { AcctId.Customer__c = CustResult.id; AcctId.CustomerID__c = CustId.X_CUSTOMER_SOURCE_REF_ID__C; } AcctId.X_ACCOUNT_SOURCE_REF_ID__c = custAcctLink.account_number__c; AcctId.Status__c = custAcctLink.Account_Status__c; // AcctId.Account_Open_Date__c = custAcctLink.Date_Established__c; AcctResult = Database.upsert(AcctId); accountID = AcctResult.Id; } // Upsert Customer Account Link Record // TODO: Parse out field into MM/DD/YYYY for string year = '2010'; string month = '04'; string day = '01'; string establishDate = (year + '-' + month + '-' + day); Customer_Account_Link__c uploadCAL = new Customer_Account_Link__c (); uploadCAL.Customer__c = customerID; uploadCAL.Customer_ID__c = customerNum; uploadCAL.Account__c = accountID; uploadCAL.Account_Number__c = custAcctLink.account_number__c; uploadCAL.Account_Status__c = custAcctLink.account_status__c; uploadCAL.Address_Line_1__c = custAcctLink.Address_Line_1__c; uploadCAL.Address_Line_2__c = custAcctLink.Address_Line_2__c; uploadCAL.City__c = custAcctLink.City__c; uploadCAL.State__c = custAcctLink.State__c; uploadCAL.Postal_Code__c = custAcctLink.Postal_Code__c; uploadCAL.Date_Established__c = custAcctLink.Date_Established__c; uploadCAL.Last_Updated_Timestamp__c = custAcctLink.Last_Updated_Timestamp__c; uploadCAL.Home_Phone__c = custAcctLink.Home_Phone__c; uploadCAL.Work_Phone__c = custAcctLink.Work_Phone__c; uploadCAL.Mobile_Phone__c = custAcctLink.Mobile_Phone__c; uploadCAL.Fax__c = custAcctLink.Fax__c; uploadCAL.Taxpayer_Identification_Number__c = custAcctLink.Taxpayer_Identification_Number__c; uploadCAL.Fax__c = custAcctLink.Fax__c; Database.upsert(uploadCAL); } }
- jasonrtaylor
- April 20, 2010
- Like
- 0
- Continue reading or reply
Trigger - Trying to trim leading characters
I am trying to trim leading characters from a string then using that string value in my where clause of my SQL query. I have found several references to a "TrimStart" function in java and .NET, but neither syntax appears to work in the trigger. See the commented code for the .NET "TrimStart" syntax. Does anyone have ideas how to trim leading characters and zeros.
Here is my code
trigger LookupBanker on Lead (before insert, before update) { Lead[] leads = Trigger.new; for(Lead lead:leads){ if (lead.employee_id__c!=null){ string origin = lead.employee_id__c; /* string originwoz = origin; originwoz = originwoz.TrimStart('0'); */ Account c = [Select Id, BID__pc FROM Account WHERE RecordTypeId = '012600000000000000' AND BID__pc = :originwoz][0]; lead.Banker_Name__c = c.Id; } } }
- jasonrtaylor
- November 16, 2009
- Like
- 0
- Continue reading or reply
Trigger not updating
I am trying to update the parent opportunity record with a count of all child records that meet a specified criteria when a new child custom object is created or modified. I have checked the SOQL queries and they are returning the results I expected. The opportunity relationship field is present and required.
trigger UpdatePendingPOMRequests on DM__c (after insert, after update) {
//Find ID for 'POM' Record Type
RecordType rt = [Select r.Id from RecordType r WHERE r.name = 'POM' AND r.SobjectType = 'DM__c'];
for(DM__c dm : trigger.new){
if(dm.RecordTypeId == rt.id){
integer dmct = [Select Count()
FROM DM__c d
WHERE dm__c.dm_opportunity_id__c = :dm.dm_opportunity_id__c
AND dm__c.RecordTypeId = :rt.id
AND dm__c.POM__c = 'Pending'
];
Opportunity oid = [Select Num_Pending_NSP__c from Opportunity WHERE id =:dm.dm_opportunity_id__c ];
oid.Num_Pending_NSP__c = dmct;
Database.upsert(oid);
}
}
}
Thanks, in advance for your response.
- jasonrtaylor
- August 11, 2010
- Like
- 0
- Continue reading or reply