You need to sign in to do that
Don't have an account?
Ralf Wittenberger
Trigger to move Attachment from Task to Lead
Hi, I would like to post this question again, maybe somebody has an idea, what am i missing or where i failed.
I have a trigger, which can duplicate a attachment, which is linked to an Task to an account with the following trigger
trigger CheckforAttachement on Attachment (before insert) {
//Check to see if the Attachment has a Task as the Parent Record
for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
//Select the AccountId from the Task
task 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,
OwnerId = a.OwnerId,
ParentId = parent.AccountId
);
//Insert the new Attachment
insert newA;
}}
}
}
When I tried to adopt this to Leads, it doesn´t work and I have no clue, is there someone, that can maybe help me out here?
When I tried to replace AccountId with LeadId, console tells me "No such column 'LeadID' on entity 'Task'
Therefore, I have added a formular field "LeadID__c" to the Task, which pulls the LeadId from the field WhoId, which is the Name Field (Lookup) for Leads in Tasks.
This is the trigger, i adopted and it doesn´t work
trigger MoveAttToLead on Attachment (after insert, after update) {
for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
task parent = [SELECT LeadID__c FROM Task WHERE WhoId = :a.parentId];
if (parent.LeadID__c != null){
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
OwnerId = a.OwnerId,
ParentId = parent.LeadID__c
);
insert newA;
}}}}
I have a trigger, which can duplicate a attachment, which is linked to an Task to an account with the following trigger
trigger CheckforAttachement on Attachment (before insert) {
//Check to see if the Attachment has a Task as the Parent Record
for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
//Select the AccountId from the Task
task 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,
OwnerId = a.OwnerId,
ParentId = parent.AccountId
);
//Insert the new Attachment
insert newA;
}}
}
}
When I tried to adopt this to Leads, it doesn´t work and I have no clue, is there someone, that can maybe help me out here?
When I tried to replace AccountId with LeadId, console tells me "No such column 'LeadID' on entity 'Task'
Therefore, I have added a formular field "LeadID__c" to the Task, which pulls the LeadId from the field WhoId, which is the Name Field (Lookup) for Leads in Tasks.
This is the trigger, i adopted and it doesn´t work
trigger MoveAttToLead on Attachment (after insert, after update) {
for (Attachment a : trigger.new){
String parentIdString = String.valueof(a.parentId);
if (parentIdString.substring(0,3) == '00T'){
task parent = [SELECT LeadID__c FROM Task WHERE WhoId = :a.parentId];
if (parent.LeadID__c != null){
Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
Attachment newA = New Attachment(
Name = a.Name,
Body = body.Body,
OwnerId = a.OwnerId,
ParentId = parent.LeadID__c
);
insert newA;
}}}}
Try with below code it wil work .
All Answers
Replace Lead Id __c with WhatId it will work .
tried it, but unfortunately, it didn´t work. WhatId is only lookup field for Account, Opportunities etc but not or Lead. For Lead, the lookupfield is called WhoId.
Failure Message is after update: List has no rows for assignment to SObject: Trigger.MoveAttToLead: line 5, column 1
I already tried it this way, unfortunately, it didn´t work either.
There must be some other issue to the lookup or?
You have to write trigger in the task object.
Instead of writing in attachment object.
Thanks,
Try with below code it wil work .
this works great, but unfortunately only on updating an attachment, not for new attached. What could be missed here?
Thanks a lot!
Ralf
Use the below trigger. It will work when the task is created itself
Thanks.
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<Id> forDeletionIds = new List<Id>();
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;
}}