I need to have attachments attached to opportunity to the related account.i tired following code,no errors but attachments are not getting on account object..pls help:
Trigger opportunity on Attachment (after insert) {
if(CheckRecursive.firstRun){
CheckRecursive.firstRun = false;
set<id> Oppids=new set<id>(); Map<id, attachment> OpportunityAttachments = new Map<id, attachment>(); Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>(); for(attachment an:trigger.new) { if(an.ParentId.getSobjectType() == Opportunity.SobjectType) OpportunityAttachments.put(an.ParentId, an); //Oppids.add(an.ParentId); } // not required, as using soql // list<Opportunity> OppAccmap =new list<Opportunity>([Select Account.id,Account.name from Opportunity where Id In:Oppids]); list<attachment> accatt=new list<attachment>(); //for(Opportunity Ot:OppAccmap){ // read "Explanation" why I have replaced Soql For loop with your approach if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced. { OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]); // for (attachment am :[select id, name,parentId, body from Attachment where ParentId in :Oppids]) // soql inside for loop is bad, bad thing. for (attachment am: OpportunityAttachments.values() ) { if (OpportunitiesWithAccount.containsKey(am.ParentId) ) { Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id); accatt.add(newFile); } } } insert accatt;
}
}
after this i created an opportunity and added an file,then i checked in tha account that related to that particular opportunity,no files are available..
i made trigger on attachments..but i am uploaded in files..in opportunity in related list files portion i am uploading the files,i want the same file to be shown inthe opportunity related account..
Trigger opportunity on Attachment (after insert)
{
if(CheckRecursive.firstRun){
CheckRecursive.firstRun = false;
set<id> Oppids=new set<id>();
Map<id, attachment> OpportunityAttachments = new Map<id, attachment>();
Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>();
for(attachment an:trigger.new)
{
if(an.ParentId.getSobjectType() == Opportunity.SobjectType) OpportunityAttachments.put(an.ParentId, an);
//Oppids.add(an.ParentId);
}
// not required, as using soql
// list<Opportunity> OppAccmap =new list<Opportunity>([Select Account.id,Account.name from Opportunity where Id In:Oppids]);
list<attachment> accatt=new list<attachment>();
//for(Opportunity Ot:OppAccmap){
// read "Explanation" why I have replaced Soql For loop with your approach
if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced.
{
OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]);
// for (attachment am :[select id, name,parentId, body from Attachment where ParentId in :Oppids]) // soql inside for loop is bad, bad thing.
for (attachment am: OpportunityAttachments.values() )
{
if (OpportunitiesWithAccount.containsKey(am.ParentId) )
{
Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id);
accatt.add(newFile);
}
}
}
insert accatt;
}
}
after this i created an opportunity and added an file,then i checked in tha account that related to that particular opportunity,no files are available..
https://douglascayers.com/2015/10/10/salesforce-convert-attachments-to-chatter-files/
Trigger opportunityfiles on ContentDocumentLink (after insert)
{
System.debug('atta');
set<Id> LinkedEntityId=new set<id>();
Map<id, ContentDocumentLink> OpportunityAttachments = new Map<id, ContentDocumentLink>();
Map<id, Opportunity> OpportunitiesWithAccount = new Map<id, Opportunity>();
for(ContentDocumentLink an:trigger.new)
{
if(an.LinkedEntityId.getSobjectType() == Opportunity.SobjectType) {
System.debug('-- Going 1 ');
OpportunityAttachments.put(an.LinkedEntityId, an);
}
}
list<ContentDocumentLink> accatt=new list<ContentDocumentLink>();
if (OpportunityAttachments.keySet().size() > 0) // Read "Explanation" why this check is introduced.
{
OpportunitiesWithAccount = new Map<Id, Opportunity>([SELECT Id, Account.id FROM Opportunity WHERE Id IN :OpportunityAttachments.keySet()]);
for (ContentDocumentLink am: OpportunityAttachments.values() )
{
if (OpportunitiesWithAccount.containsKey(am.LinkedEntityId) )
{
ContentDocumentLink newFile = New ContentDocumentLink(LinkedEntityId = OpportunitiesWithAccount.get(am.LinkedEntityId).Account.id);
accatt.add(newFile);
}
}
}
insert accatt;
}
i am getting error:
Variable does not exist: att at line 23
Invalid type: Ids at line 7..
I am not able to find the where is the error..can you help in this.. i made trigger on ContentDocumentLink