You need to sign in to do that
Don't have an account?

Trigger to insert new records with default name
We have a custom object NRProducts__c that is made up of all of our products. We want to be able to import a file update (via import wizard) to update inventory levels. The import file will not be based on the Name record, but rather an external number (Model_Number__c). Our goal is that if a new Model_Number exist in the import file that a new record is inserted with the default name "NEWPRODUCT". Below is the trigger that was created, but when we run an import with a new Model Number and Name as null/blank we get an email saying the record already exist (see below)
trigger ProductTrigger on NRProducts__c (before update, before insert) {
for(NRProducts__c r : Trigger.new){
if(r.Name == null && r.Name.length() <= 0){
r.Name = 'NEWPRODUCT';
}
}
}
Email from SF-
Force.com Sandbox
Alert: All information in the import file was already in salesforce.com.
Result: No data was created or modified in salesforce.com.
If you encounter any problems or have any questions, please contact us by clicking Help & Training at the top right of any salesforce.com page and choosing the My Cases tab.
Thank you!
Customer Support
salesforce.com
Any ideas how to make this work?
As far as I can see, using the SalesForce's Import Custom Objects does not work as expected.
I did a quick test with an object as follows:
Product Name (Name)
External Product Id (External Id, string, unique)
Current Inventory (Number)
I created a single record as follows:
Product Name: Product 1
External Product Id: Ext1
Current Inventory: 20
The test csv:
Using the Import Custom Objects (Setup-Data Management-Import Custom Objects)
'Yes - prevent duplicate records from being created. Note: You must select this option if you want to update existing records.'
'External Product Id (External ID)'
'Update existing records and insert new records'
I get the resulting email that 1 record was updated and nothing else. I check the record and it is indeed updated to have a current inventory of 15. No other record is created.
So I tried the Apex Data Loader (v21 in my case). First I changed the existing record's Current Inventory back to 20 to verify it will update.
I did upsert, selected my object and csv, mapped the fields and ran it. It processed 2 records and when I check SalesForce, the existing record is updated to 15 and a new record was created as follows:
Product Name: <SF Id>
External Product Id: Ext2
Current Inventory: 11
So, my conclusion:
SalesForce's Import Custom Objects does not work as expected and/or does not work for upserting when the Name field is not supplied.
Use the Apex Data Loader if possible.
All Answers
Not sure if this will fix your issue, but your conditions should be an OR
Otherwise (I think but not 100% sure) calling length on a null string will throw an exception and fail the name update.
Alex,
Thank you for your help. I tried the OR in the past and had the same result. I just now changed it to:
trigger ProductTrigger on NRProducts__c (before update, before insert) {
for(NRProducts__c r : Trigger.new){
if(r.Name == null){
r.Name = 'NEWPRODUCT';
}
}
}
And still get the same result. Not sure why it is says that it already exist and does not insert the new record?
Hi CRNR,
I'm not sure what the Import Wizard is and how it works. But a tip to help you get further is to add debug statements to your trigger. This way you can look into the debug logs and see what's heppening.
You're writing to the debug-log with the
System.debug('Text goes here');
method.
You will find the debug logs by going to
Setup -> Monitoring (Under "Administration Setup") -> Debug Logs
You need to add a user to the "Monitored Users" prior to running the code.
Cheers!
Martin
As far as I can see, using the SalesForce's Import Custom Objects does not work as expected.
I did a quick test with an object as follows:
Product Name (Name)
External Product Id (External Id, string, unique)
Current Inventory (Number)
I created a single record as follows:
Product Name: Product 1
External Product Id: Ext1
Current Inventory: 20
The test csv:
Using the Import Custom Objects (Setup-Data Management-Import Custom Objects)
'Yes - prevent duplicate records from being created. Note: You must select this option if you want to update existing records.'
'External Product Id (External ID)'
'Update existing records and insert new records'
I get the resulting email that 1 record was updated and nothing else. I check the record and it is indeed updated to have a current inventory of 15. No other record is created.
So I tried the Apex Data Loader (v21 in my case). First I changed the existing record's Current Inventory back to 20 to verify it will update.
I did upsert, selected my object and csv, mapped the fields and ran it. It processed 2 records and when I check SalesForce, the existing record is updated to 15 and a new record was created as follows:
Product Name: <SF Id>
External Product Id: Ext2
Current Inventory: 11
So, my conclusion:
SalesForce's Import Custom Objects does not work as expected and/or does not work for upserting when the Name field is not supplied.
Use the Apex Data Loader if possible.
This solution worked perfect. It is the import wizard that causes issues. Using the dataloader on an upsert everything worked perfect!
Thank you!