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
Brian k.ax1387Brian k.ax1387 

Trigger to update date account date from task.

I have requirment to update a field on the account page when a salesperson creates a certain type of task.

 

I have it working fine when I don't have the code checking to ensure that the task creator is the account owner. But I get an error that the Account owner is not a variable when trying to compare. See red text.

 

Any thoughts?  

 

trigger AccountVisit on Task (after Insert) {

      Set<id> accountid = new set<id>();

             for (Task tsk : trigger.new){

                        if  (tsk.Type == 'Meeting'){

                            accountid.add(tsk.whatID);

              }

              }

                  List<account> acctlist = [SELECT id

                                           FROM account

                                           Where ID in :accountid];

For (account acc : acctlist && acc.Owner = tsk.CreatedBy)

       {               acc.Last_Formal_Business_Review__c = system.today();

       }           update acctlist

;  }

 

Best Answer chosen by Admin (Salesforce Developers) 
Shailesh DeshpandeShailesh Deshpande
MAP<String, String> maptaskcreators = New Map<string, string>() // key = accIds, value = task Ids

For(Task t : trigger.new)
{
Maptaskcreators.put(t.whatId, t.CreatedbyId)
}

Issue your query for account as you did. Also query the OwnerId field.

For(account acc: accList )
{
If(maptaskcreators.containskey(acc.Id) && acc.ownerId == maptaskcreators.get(acc.Id))
{
Proceed further.
}
}

In your code, the variable Tsk is not available. It is local to the for loop. Hence you were not able to comapre.

NOTE: What I have pasted, I havent tested..but should be good enough to take you further. You might face issues if there are multiple tasks asscociated with the same account. In that case I think, you can use a Map<Id, List<tasks>>

All Answers

ItswasItswas
Hi Brian,
Please try the below code .Hope this will help you.
trigger AccountVisit on Task (after Insert)
{
Set<id> accountid = new set<id>();
Map<Id,Id> objMapAccIdTskCreatedId = new Map<Id,Id>();
List<Account> objAcc = new List<Account>();
for (Task tsk : trigger.new)
{ if (tsk.Type == 'Meeting')
{ accountid.add(tsk.whatID);
objMapAccIdTskCreatedId.put(tsk.whatId,tsk.CreatedById);
} }
for(account acc : [SELECT id FROM account Where ID in :accountid] )
{ if(acc.OwnerId = objMapAccIdTskCreatedId.get(acc.Id)){
acc.Last_Formal_Business_Review__c = system.today();
objAcc.add(acc);} }
if(!objAcc.IsEmpty())
update objAcc;
}
Let me know if you face any futher issue.

Regards,
Itsaws
Shailesh DeshpandeShailesh Deshpande
MAP<String, String> maptaskcreators = New Map<string, string>() // key = accIds, value = task Ids

For(Task t : trigger.new)
{
Maptaskcreators.put(t.whatId, t.CreatedbyId)
}

Issue your query for account as you did. Also query the OwnerId field.

For(account acc: accList )
{
If(maptaskcreators.containskey(acc.Id) && acc.ownerId == maptaskcreators.get(acc.Id))
{
Proceed further.
}
}

In your code, the variable Tsk is not available. It is local to the for loop. Hence you were not able to comapre.

NOTE: What I have pasted, I havent tested..but should be good enough to take you further. You might face issues if there are multiple tasks asscociated with the same account. In that case I think, you can use a Map<Id, List<tasks>>
This was selected as the best answer
jbroquistjbroquist

Shailesh's response above is exactly what I would as well. I just wanted to really point out that system generated User lookup fields always end in Id. (e.g. OwnerId, LastModifiedById, CreatedById, etc.)

Brian k.ax1387Brian k.ax1387

Thanks all,

I don't do much coding so it was very helpfull.

I have updated the code with your suggestions and it is working as I expected. But I suspect I have left some extra code in it. If you have a few minutes to take a peek I would appreciate it.

 

trigger AccountVisit on Task (after Insert) {

Set<id> accountid = new set<id>();
MAP<String, String> maptaskcreators = New Map<string, string>();

for (Task tsk : trigger.new){
        Maptaskcreators.put(tsk.whatId, tsk.CreatedbyId);
        if  (tsk.Type == 'Meeting'){
                      accountid.add(tsk.whatID);
              }
              }
                  List<account> acctlist = [SELECT id, ownerId
                                           FROM account
                                           Where ID in :accountid];
For (account acc: acctlist)
If(maptaskcreators.containskey(acc.Id) && acc.ownerId == maptaskcreators.get(acc.Id))
       {       
       acc.Last_Formal_Business_Review__c = system.today();
       }   
       update acctlist;
}