+ Start a Discussion
aswanijagadesh1.397398535585991E12aswanijagadesh1.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)
Vinit_KumarVinit_Kumar
Why are you using After Trigger,you can get the same in your before Trigger.Try below sample code :-
Trigger relatedOpp on Opportunity(before insert,before update)

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);
	}
}

//Querying the older Opps based on Account
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select id,name from Opportunity where AccoountId in:accIds]);
If this helps,please mark it as best answer to help others :)
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
i want to use list of related opportunies in trigger.isafter . the values in isbefore trigger are not coming into trigger.isafter .so i want to query the related list of opportuniies into trigger.isafter(not recenlty inserted one because it is working after insert)  
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
hai vinit

what ever the result you got in before trigger by using query ,same  as it is i want in after trigger how can i?
Vinit_KumarVinit_Kumar
Just change the event from Before to After and you are good to go :)

Change this line from

Trigger relatedOpp on Opportunity(before insert,before update)

to

Trigger relatedOpp on Opportunity(after insert,after update)
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
ok , i wrote query in after insert fine.
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 

OnpursuitOnpursuit
Hi,

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.
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
hai onpursuit
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
Vinit_KumarVinit_Kumar
Change the query from

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 :)
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
for(i=0;i<oppMap.values().size()-1;i++)
{
        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
Vinit_KumarVinit_Kumar
Not sure how you are doing,I will have to see the whole code 
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
opportunity opp=[select id,open__c,progress__c,closed__c,AccountId from opportunity where id=:trigger.new[0].id];
       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);
OnpursuitOnpursuit
Hi ,

For my solution , the new opp is deleted only from the oppMap. You can still reference from the opp variable.

Thanks,
Rahul
OnpursuitOnpursuit
Hi,

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.
Vinit_KumarVinit_Kumar
Are you populting oppIds somehwhere else coz the way I am looking at it,oppIds should only contain 3 records not 6......
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
AggregateResult opp4=[select Max(open__c)av,Max(closed__c)cs,Max(progress__c)pr from opportunity where AccountId=:trigger.new[0].AccountId and id!=:trigger.new[0].id];

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.