[SalesForce] Apex Trigger – Move Attachments from Task to Account

I have a user which attached JPGs to the wrong object (Task vs Account). I wrote this trigger thinking I could quickly fix it but I am getting this error:

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

Help! 😀

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;
}

Best Answer

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

i think it is

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

There is a illegal assignment from task to account

Related Topic