I am adding an after insert
trigger on lead, where if there is a value (id) in a custom field (fromAccount__c), that specific account with said id, will be deleted.
- this is in order to solve How to Convert a Contact back to lead - and delete the old contact and account
So, my trigger started like this, but I don't understand how to get the account inserted correctly into the accountsToDelete
- Should I actually create the Account Object?
trigger deleteAccountFromContact on Lead (after insert) {
////////////////////////////////////// // This trigger deletes the account that the contact // was converted from.
if (Trigger.new.fromAccount__c != null) { List<Account> accountsToDelete = new List<Account>(); Account tempAccount = new Account(id=Trigger.new.fromAccount__c); accountsToDelete.add( tempAccount ) ; delete accountsToDelete; } }
Another option to make this a more SOQL trigger will be to use :
Account tempAcc = [Select Account.id from Account where AccountId =:Trigger.new.fromAccount__c]
But I get the error:
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST at line 15 column 81
if (Trigger.new.fromAccount__c != null)
{ List<Account> accountsToDelete = new List<Account>(); Account tempAccount = [Select Account.id from Account where AccountId =:Trigger.new.fromAccount__c] accountsToDelete.add( tempAccount ) ; delete accountsToDelete; } }
- Can I create an account based on a text field of ID?
- should I make this a more SOQL valued trigger?
- else what's wrong with my code?