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
JayDP123JayDP123 

Before Insert trigger

Hello, I have a trigger that is working on the Before Update, but is not working on Before Insert.

The idea is that when a Task is Inserted or Updated,

If the purpose__c = 'Sway Card, and Owner.Userrole contains 'BDM',

then set account.type = 'Suspect'

 

So as you can see it updates the Account Object, not sure if that has anything to do with it. Also purpose__c is a multi-picklist field. 

 

Anybody can help me out?

 

My code is here:  

 

trigger Task_SWayCard on Task (before insert, before update) {

List<ID> accID = new List<ID>(); //AccountIDs in the trigger
List<ID> ownerID = new List<ID>(); //Task Owners in the Trigger
for (task t : trigger.new)
{
If ( t.purpose__c == 'Sway Cards' )
{accID.add(t.AccountID);}
ownerID.add(t.ownerID);
}

If (accID != null)
{
list<Account> acc = [select id, name, ownerID, type from Account where Id =: accID]; //Accounts from the AccID list
List<User> owner = [select id, name, userrole.name from User where Id =: ownerID]; //Users from the ownerID list
List<User> bdmOwner = [select id, name, userrole.name from user where Id =: owner //Users where name like BDM
and userrole.name like '%BDM'];

List<Account> accToUpdate = [select id, name, type, ownerID from account where ownerID =:bdmOwner
and id =:acc];  //Selects the accounts we want to update

for (Account a : acc)
{
a.type = 'Suspect - Qualified';
update a;
system.debug(a.type);
}
}
}

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

1) You have a dml in a loop. Don't do that. Simple update all "a" records, then call update on acc after the loop.

2) AccountId is probably still "empty" in "before insert", because the database hasn't had a chance to do anything to that record yet (it's not been committed to the database level). Try using "after insert" and "after update", which is when this trigger should fire anyways (it's not a validation or data load, but a related-record-update).

All Answers

sfdcfoxsfdcfox

1) You have a dml in a loop. Don't do that. Simple update all "a" records, then call update on acc after the loop.

2) AccountId is probably still "empty" in "before insert", because the database hasn't had a chance to do anything to that record yet (it's not been committed to the database level). Try using "after insert" and "after update", which is when this trigger should fire anyways (it's not a validation or data load, but a related-record-update).

This was selected as the best answer
Janet GuoJanet Guo
I completely agree with sfdcfox on item number 1 -- don't do DML operations in a loop. Since you already have a list of Accounts you're going through, just put the update statement outside and after the loop.

However, I don't agree with sfdcfox's second point. It doesn't look like you're inserting Accounts anywhere, so the Account (and therefore it's ID) must already exist when you attempt to create a Task; no need to use an "after update" on the Task.
I suspect the problem may be that the record you are trying to insert might have any or all of the Purpose__c, the Account Lookup, and the Owner lookup fields blank. You might want to either put checks in your code for blank fields, or give the fields a default value, or make the fields required.
JayDP123JayDP123

Thanks to sfdcfox and Janet for your help! I took the update out of the loop, and just switched from a Before Insert to an After Insert and it seems to be working now! Thankeee

sfdcfoxsfdcfox
AccountId is loaded when the record is loaded from the database, it's a type of special formula field that brings in the account id from a related record (e.g. opportunity, quote, case, etc). It won't be available until after there's something to load, so it is rightfully blank on "before insert", but not "after insert", "before update", or "after update." The database hasn't been queried at all in a before-insert trigger (basically). Kind of hard for me to explain, I guess, but there you have it.