function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cpo87cpo87 

Access field values from object that caused trigger to fire in order to create new lead

I am trying to create a new lead and task when a custom object called TPI_Supplier_Tracker__c is entered into the database.  I am getting null value errors and I think this is because the data hasn't been committed to the DB yet.  Here is the error I'm getting...

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TPIEventCreateLeadTask caused an unexpected exception, contact your administrator: TPIEventCreateLeadTask: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: Trigger.TPIEventCreateLeadTask: line 23, column 13

 

Here is my code...

 

 

trigger TPIEventCreateLeadTask on TPI_Supplier_Tracker__c (before insert) {

//On entry of a new TPI_Supplier_Tracker__c record create a lead, associate it to a campaign, and assign a task
//TPI_Supplier_Tracker__c records will be created in mass once a week

List<TPI_Supplier_Tracker__c> tpiList = new List<TPI_Supplier_Tracker__c>();

for(TPI_Supplier_Tracker__c tpi :Trigger.new){
tpiList.add(tpi);

Lead lead = new Lead(
LastName = '[Unknown]',
Company = tpi.Account__r.Name,
OwnerId = tpi.Account__r.OwnerId,
Status = 'Open',
LeadSource = 'Other',
ERP_Accounting_System__c = 'Unknown',
Existing_EDI_Application__c = 'Unknown',
Potential_Trading_Partners__c = tpi.Related_to_Account__r.Name);
// tpi.addError('Error - ' + tpi);
insert lead;
}
}

 

If I uncomment the addError it shows that there is data...

 

Error - TPI_Supplier_Tracker__c:{Account__c=0016000000NpaBiAAJ, IsDeleted=false, Last_Product_Activity__c=2010-03-15 00:00:00, Related_to_Account__c=0016000000NpaBhAAJ, Data_Being_Put_Into_TPI__c=true}

 

I want to assign the Lead.Name and Lead.OwnerId using information from the account related to the TPI_Supplier_Tracker__c record.  Is this possible and how can I access that information when the system is saying that they are blank?

 

Thanks,

 

Christian

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

You are welcome.  For clarification, I meant Related object, not related list (which I said).  I did some more research on that, and unfortunately, the related object data is not available in the trigger.new collection.

You will have to requery for those fields and then generate your lead list. Your best bet would be to make this an after trigger so the object already exists, then you can requery for the object with the related fields you need.

 

 

trigger TPIEventCreateLeadTask on TPI_Supplier_Tracker__c (after insert) { //On entry of a new TPI_Supplier_Tracker__c record create a lead, associate it to a campaign, and assign a task //TPI_Supplier_Tracker__c records will be created in mass once a week List<Lead> tpiLeads = new List<Lead>(); //Get the ids of the newly created TPI Supplier Tracker entries Set<ID> TPI_IDs = new Set<ID>(Trigger.newMap.keySet()); //Get the recently inserted items with the related fields needed to create new lead records List<TPI_Supplier_Tracker__c> newTPIs = new List<TPI_Supplier_Tracker__c>([select id, Account.Name,Account.OwnerID,Related_to_Account.Name from TPI_Supplier_Tracker__c where id in :TPI_IDs]); for(TPI_Supplier_Tracker__c tpi :newTPIs){ Lead tpilead = new Lead( LastName = '[Unknown]', Company = tpi.Account__r.Name, OwnerId = tpi.Account__r.OwnerId, Status = 'Open', LeadSource = 'Other', ERP_Accounting_System__c = 'Unknown', Existing_EDI_Application__c = 'Unknown', Potential_Trading_Partners__c = tpi.Related_to_Account__r.Name); // tpilead .addError('Error - ' + tpilead ); tpileads.add(tpilead ); } insert tpileads; }

 

 

 

All Answers

JimRaeJimRae

Change your trigger to an "after insert" trigger instead. Then the id will exist and the record would be committed to the database.  Also, you don't need to create a secondary list, unless you are going to do other work on the trigger data, but you should create a list of the leads to insert, so you don't have governer limit issues with your DML.  I also wouldn't create a variable called "lead" you are liable to run into conflicts.

Last issue, you are trying to pull a field from a related list, this might not work, you will have to test and see.

Hopefully, this gets you closer.

 

 

trigger TPIEventCreateLeadTask on TPI_Supplier_Tracker__c (before insert) { //On entry of a new TPI_Supplier_Tracker__c record create a lead, associate it to a campaign, and assign a task //TPI_Supplier_Tracker__c records will be created in mass once a week List<Lead> tpiLeads = new List<Lead>(); for(TPI_Supplier_Tracker__c tpi :Trigger.new){ Lead tpilead = new Lead( LastName = '[Unknown]', Company = tpi.Account__r.Name, OwnerId = tpi.Account__r.OwnerId, Status = 'Open', LeadSource = 'Other', ERP_Accounting_System__c = 'Unknown', Existing_EDI_Application__c = 'Unknown', Potential_Trading_Partners__c = tpi.Related_to_Account__r.Name); // tpilead .addError('Error - ' + tpilead ); tpileads.add(tpilead ); } insert tpileads; }

 

 

 

 

 

cpo87cpo87

Hi Jim,

 

Thanks for you help.  Your suggestions make a lot of sense and have definitely helped me to understand what my code is doing.

 

What I don't understand still is what you mean when you say I am pulling a field from a related list?  Are you referring to the fields like tpi.Account__r.Name, tpi.Account__r.OwnerId, and tpi.Related_to_Account__r.Name?  I am really hoping that I can get to these fields since they are critical to making sure I am assigning the proper values to my new leads.

 

Thanks,

 

Christian

JimRaeJimRae

You are welcome.  For clarification, I meant Related object, not related list (which I said).  I did some more research on that, and unfortunately, the related object data is not available in the trigger.new collection.

You will have to requery for those fields and then generate your lead list. Your best bet would be to make this an after trigger so the object already exists, then you can requery for the object with the related fields you need.

 

 

trigger TPIEventCreateLeadTask on TPI_Supplier_Tracker__c (after insert) { //On entry of a new TPI_Supplier_Tracker__c record create a lead, associate it to a campaign, and assign a task //TPI_Supplier_Tracker__c records will be created in mass once a week List<Lead> tpiLeads = new List<Lead>(); //Get the ids of the newly created TPI Supplier Tracker entries Set<ID> TPI_IDs = new Set<ID>(Trigger.newMap.keySet()); //Get the recently inserted items with the related fields needed to create new lead records List<TPI_Supplier_Tracker__c> newTPIs = new List<TPI_Supplier_Tracker__c>([select id, Account.Name,Account.OwnerID,Related_to_Account.Name from TPI_Supplier_Tracker__c where id in :TPI_IDs]); for(TPI_Supplier_Tracker__c tpi :newTPIs){ Lead tpilead = new Lead( LastName = '[Unknown]', Company = tpi.Account__r.Name, OwnerId = tpi.Account__r.OwnerId, Status = 'Open', LeadSource = 'Other', ERP_Accounting_System__c = 'Unknown', Existing_EDI_Application__c = 'Unknown', Potential_Trading_Partners__c = tpi.Related_to_Account__r.Name); // tpilead .addError('Error - ' + tpilead ); tpileads.add(tpilead ); } insert tpileads; }

 

 

 

This was selected as the best answer
cpo87cpo87

Thanks again Jim!  That worked, the only change I had to make was to add the __r in the query. 

 

Much appreciated!

 

Christian