1

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! :D

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

3 Answers 3

1
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

1

now, after lots of different testings etc... i finally made the following trigger, which works totally great. feel free to share.

let me quickly summarize the background of this, I am activating email to salesforce, so if customers sending emails in, they will automatically attached to either relevant Lead or Account. Unfortunately, the attachment is usually saved to the Task but this doesn´t help our complaint and customer service center, as they want the attachments saved to the parent object, such as Lead or Account, so with the following trigger, it´s doing totally, what I expected, as well as deleting the attachment from task to avoid double attachments.

trigger MoveAtt on Attachment (after insert) { List forDeletionIds = new List();

for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
    System.debug(a.parentId);
    if(Task.WhatId != null){
        task parent1 = [SELECT Id,WhatId  FROM Task WHERE Id = :a.parentId];
    if (parent1.WhatId  != null){
        Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
        Attachment newA = New Attachment(
            Name = a.Name,
            Body = body.Body,
            Description = 'Email Attachment from ' + date.today(),
            OwnerId = a.OwnerId,
            ParentId = parent1.WhatId
        );
    }
    task parent = [SELECT Id,WhoId  FROM Task WHERE Id = :a.parentId];
    if (parent.WhoId  != null){
        Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
        Attachment newA = New Attachment(
            Name = a.Name,
            Body = body.Body,
            Description = 'Email Attachment from ' + date.today(),
            OwnerId = a.OwnerId,
            ParentId = parent.WhoId
        );
        insert newA;
        forDeletionIds.add(a.Id);
    }}}List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];  delete forDeletion;
}}
0

Try using

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

UPDATE

You are trying to assign a Task record into an account, which is not possible. Instead you can save the id into ID variable

 ID parent = [SELECT AccountId FROM Task WHERE WhatId = :a.parentId LIMIT 1].Id;

and use this id instead

3
  • Prady same error :( Commented Jul 24, 2013 at 6:04
  • Oh!! you are trying to assign a task record into an account.
    – Prady
    Commented Jul 24, 2013 at 6:33
  • I think what @venkatesh suggested would be best suited
    – Prady
    Commented Jul 24, 2013 at 6:56

You must log in to answer this question.

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