I need to write a Trigger in which relationship goes like this... opportunity is the grandparent it has a child Invoice and further Invoice has a child Invoice Line which is grandChild.. what I need to do is create a field on Opportunity of text type. Now if I have an Opportunity named 'Smith' and it has related quotes named 'Q' and further quote have related QuoteLineItems named 'OLI1' and 'QLI2'... In opportunity field the name of 'Quote' should come with ':' and after that its related quoteLineItems
Smith : Qli1 , QLI2
trigger QliReport on QuoteLineItem (after insert, after update, after delete)
{
List<Opportunity> oppList = new List<Opportunity>();
List<Quote> quoteList = new List<Quote>();
Set<Id> setId = new Set<Id>();
if(Trigger.isInsert && Trigger.isUpdate)
{
for(QuoteLineItem qli : trigger.new)
{
setId.add(qli.quoteId);
}
}
if(Trigger.isDelete)
{
for(QuoteLineItem qli : trigger.old)
{
setId.add(qli.quoteId);
}
}
map<Id,Quote> quoteMap = new map<Id,Quote>
([Select id, opportunityId, quote.opportunity.name,
quote.opportunity.CloseDate, quote.opportunity.StageName,
quote.opportunity.Id,(Select Quantity, QuoteId, Product2Id
From QuoteLineItems) From Quote Where Id IN : setId]);
for(quote qo : quoteMap.values())
{
Opportunity opp = new Opportunity();
opp.Id = quoteMap.get(qo.Id).opportunityId
for(Quote q : opp.quotes)
{
opp.Child_Details__c = opp.Child_Details__c + ':' + q.name;
for(QuoteLineItem qli : q.QuoteLineItems )
{
opp.Child_Details__c = opp.Child_Details__c + ':' + qli.Quantity;
}
}
oppList.add(opp);
}
upsert oppList;
}
quoteMap.get(qo.opportunityId).id
this line is wrong. this should bequoteMap.get(qo.Id).opportunityId