You need to sign in to do that
Don't have an account?
aswanijagadesh1.397398535585991E12
how to query related records(opportunity) of an account into trigger by using Trigger.OldMap?
hai friends
i want to query the related opportunity of account into trigger(after insert).i wrote a query by using trigger.new here i am getting newly inserted record also why because it is executing after insert.i want previous opportunity only into trigger.isafter.
i will explain with example
for account three opportunities are there.now i am adding fourth one (i want previous three opportunity records into trigger.isafter of trigger on opportunity not fourth one) can any one please help me (i come to know this is achieved by trigger.oldMap in trigger.isafter)
i want to query the related opportunity of account into trigger(after insert).i wrote a query by using trigger.new here i am getting newly inserted record also why because it is executing after insert.i want previous opportunity only into trigger.isafter.
i will explain with example
for account three opportunities are there.now i am adding fourth one (i want previous three opportunity records into trigger.isafter of trigger on opportunity not fourth one) can any one please help me (i come to know this is achieved by trigger.oldMap in trigger.isafter)
what ever the result you got in before trigger by using query ,same as it is i want in after trigger how can i?
Change this line from
Trigger relatedOpp on Opportunity(before insert,before update)
to
Trigger relatedOpp on Opportunity(after insert,after update)
for example account has three opportunites in related list ok.now i am inserting new one(fourth). them trigger will fire and it gives resualt those three plus newly inserted one (total four i am getting reson trigger fires after insert ok).
but i want first three only not newly inserted one
You can try a workaround :
Trigger relatedOpp on Opportunity(after insert,after update)
Opportunity opp = trigger.new; // get the opportunity for which trigger is firing
List<Id> accIds = new List<Id>();
//Populating the List with related Accounts
for(Opportunity opp:trigger.new)
{
if(opp.AccountId!=null)
{
accIds.add(opp.AccountId);
}
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select id,name from Opportunity where AccoountId in:accIds]); // get the map
oppMap.remove(opp.id);
Please mark this as resolved if it solves your problem.
oppMap.remove(opp.id); -----by using this id deletes the newly inserted record i don't want this why menas
i have to update the newly inserted opportunity with previous related list opportunity values .so i want previous related opportunity by query it self.
in detail
i have a number field on opportunity.i have to get max number field value from related pervious opportunity records .them that max value plus one add to newly inserted opportunity.
finally i want pervious related list of opportunity of account into after insert trigger
select id,name from Opportunity where AccoountId in:accIds
to
// ordering the query results based on CreatedDate
select id,name,CreatedDate from Opportunity where AccoountId in:accIds Order by CreatedDate
and then try this code to workround
// looping to the 2nd last record of the list and then adding to the new list
for(i=0;i<oppMap.values().size()-1;i++)
{
for(Opportunity o : oppMap.values())
{
oppIds.add(o.id);
}
}
If this helps,please mark it as best answer to help others :)
{
for(Opportunity o : oppMap.values())
{
oppIds.add(o.id);
}
}
by using this code i am not getting result exactly. means
i have 2 records in previously now i am adding 3 rd then i print oppIds by using system.debug i am getting 6 records same previous 2 plus new one and also its duplicates of these three again
List<opportunity> oppIds=new List<opportunity>();
Map<ID,opportunity> oppMap=new Map<ID,opportunity>([select id,name,AccountId,CreatedDate,open__c,progress__c,closed__c from Opportunity where AccountId=:opp.AccountId Order by CreatedDate]);
system.debug('111111111'+oppMap.size());--------gives 3
for(integer i=0;i<oppMap.values().size()-1;i++)
{
for(Opportunity o : oppMap.values())
{
oppIds.add(o);
}
}
system.debug('sssssssss'+oppIds);
For my solution , the new opp is deleted only from the oppMap. You can still reference from the opp variable.
Thanks,
Rahul
As you mentioned on the use case. Will it be better if we have a counter on Account which gets incremented with Opportunity addition and update the counter max value on opportunity too? It will be easier to pull then to pull all opportunities. You will save on SOQL too.
this is what i am excepting
thank you vinit and Onpursuit for your help today i done this once again thank you both of you.