0

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.

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?

1 Answer 1

2

I think you want

 Account tempAccount = [Select Id from Account where Id =:Trigger.new[0].fromAccount__c]

and then your second option should work. I don't quite follow why you were trying to create an Account in the first approach?

Also - the Trigger.new collection is just that - a collection. You can't treat it like a single record. In this case you're assuming there's only one record, so you actually want

:Trigger.new[0].fromAccount__c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .