You need to sign in to do that
Don't have an account?
ANITHA BEEMU 2
Hi,can anyone help on this,i need to have all attachments of opportunity in their respective accounts too..
I tired following code :
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
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
Try
Compile Error: Trigger name, CopyopportunityAttachtoAccount, exists on different SObject type: Opportunity at line -1 column -1
i am placing the trigger in opportunity object..
pls help..
Trigger opportunityon 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=am.ParentId;
accatt.add(newFile);
}
}
}
insert accatt;
}
1.Field does not exist :OpportunitieswithAccount on Attachment
2.missing '=' at '('
both errors i am getting on this one.
Attachment newFile = New Attachment(Name = am.name, body = am.body, OpportunitiesWithAccount(am.ParentId).Account.id);
Method does not exist or incorrect signature:void Opportunitieswithaccount(Id) from the type opportunity
Attachment newFile = New Attachment(Name = am.name, body = am.body, ParentId = OpportunitiesWithAccount.get(am.ParentId).Account.id);
Create a new class, see below
Then in the trigger
i fired on attachments ,when i am saving attachment which is having account,its not showing on that 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..