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
JimPDXJimPDX 

Trigger to update date account date from task

I have seen similar issues posted to this board but the answers have not yet answered my question. Getting this error:

 

Error: Compile Error: Illegal assignment from LIST<Task> to SOBJECT:Account at line 9 column 13

 

from this trigger code:

 

trigger Update_Attachment_Parent on Attachment (after insert, after update) {
    //Create a List to store the Ids of Attachments to be deleted
    List<Id> forDeletionIds = new List<Id>();
    for (Attachment a : trigger.new){
        //Check to see if the Attachment has a Task as the Parent Record
        String parentIdString = String.valueof(a.parentId);
        if (parentIdString.substring(0,3) == '00T'){
            //Select the AccountId from the Task
            Account parent = [SELECT AccountId FROM Task WHERE WhatId = :a.parentId];
            //Check to see if the Account exists
            if (parent.AccountID != null){
                //Select the Attachment body (it isn't in memory for an update)
                Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
                //Create a new Attachment, preserving as much as is possible
                Attachment newA = New Attachment(
                    Name = a.Name,
                    Body = body.Body,
                    ContentType = 'image/jpeg',
                    Description = 'Account Photo',
                    OwnerId = a.OwnerId,
                    ParentId = parent.AccountId
                );
                //Insert the new Attachment
                insert newA;
                //Add the now duplicate Attachment ID to a list
                forDeletionIds.add(a.Id);
            }
        }
    }
    //List and then delete all duplicate Attachments
    List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
    delete forDeletion;
}

 Any help would be appreciated. I'm sure it is a simple tweak that I am overlooking but I need to roll these updates into this org ASAP. Thanks!

souvik9086souvik9086

Try this. Changes are higlighted in Blue

 

trigger Update_Attachment_Parent on Attachment (after insert, after update) {
//Create a List to store the Ids of Attachments to be deleted
List<Id> forDeletionIds = new List<Id>();
List<Attachment> attachList = new List<Attachment>();
for (Attachment a : trigger.new){
//Check to see if the Attachment has a Task as the Parent Record
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
//Select the AccountId from the Task
List<task> parentList = new List<task>();
parentList = [SELECT AccountId FROM Task WHERE WhatId = :a.parentId];
//Check to see if the Account exists
for(Task parent : parentList){
if (parent.AccountID != null){
//Select the Attachment body (it isn't in memory for an update)
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
//Create a new Attachment, preserving as much as is possible
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
ContentType = 'image/jpeg',
Description = 'Account Photo',
OwnerId = a.OwnerId,
ParentId = parent.AccountId
);
//Insert the new Attachment
attachList.add(newA);
//Add the now duplicate Attachment ID to a list
forDeletionIds.add(a.Id);
}
}
if(attachList.size()>0){
insert attachList;
}
}
}
//List and then delete all duplicate Attachments
List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
delete forDeletion;
}

 

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

KaityKaity

 

Hi,

 

Please change your code to :

 

Task parent = [SELECT AccountId FROM Task WHERE WhatId = :a.parentId];

from

Account parent = [SELECT AccountId FROM Task WHERE WhatId = 
:a.parentId];
JimPDXJimPDX

@Kaity that did not work either :(

 

Error: Compile Error: Incorrect SObject type: Attachment should be Task at line 1 column 1
souvik9086souvik9086

Check my modified trigger

 

trigger Update_Attachment_Parent on Attachment (after insert, after update) {
//Create a List to store the Ids of Attachments to be deleted
List<Id> forDeletionIds = new List<Id>();
List<Attachment> attachList = new List<Attachment>();
for (Attachment a : trigger.new){
//Check to see if the Attachment has a Task as the Parent Record
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
//Select the AccountId from the Task
List<task> parentList = new List<task>();
parentList = [SELECT AccountId FROM Task WHERE Id = :a.parentId];
//Check to see if the Account exists
for(Task parent : parentList){
if (parent.AccountID != null){
//Select the Attachment body (it isn't in memory for an update)
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
//Create a new Attachment, preserving as much as is possible
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
ContentType = 'image/jpeg',
Description = 'Account Photo',
OwnerId = a.OwnerId,
ParentId = parent.AccountId
);
//Insert the new Attachment
attachList.add(newA);
//Add the now duplicate Attachment ID to a list
forDeletionIds.add(a.Id);
}
}

}
}
if(attachList.size()>0){
insert attachList;
}
//List and then delete all duplicate Attachments
List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
delete forDeletion;
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks