Hi i tired this code but getting error..can anyone help:
Trigger CopyopportunityAttachtoAccount on Attachment (after insert) { 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(am.ParentId); accatt.add(newFile); } } } insert accatt; }
GETTING ERROE OVER HERE:
Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId=OpportunitiesWithAccount(am.ParentId); accatt.add(newFile);
Check the Parent Id of contect document if its started with 006 then create a new contect document and assign it to the account
Trigger CopyopportunityAttachtoAccount on Attachment (after insert)
{
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(am.ParentId);
accatt.add(newFile);
}
}
}
insert accatt;
}
GETTING ERROE OVER HERE:
Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId=OpportunitiesWithAccount(am.ParentId);
accatt.add(newFile);
AND Incorrect SObject type ..PLS DO HELP