You need to sign in to do that
Don't have an account?

want to update a field in object1 based on condition in object2 (object1 and object2 doesnt have any direct relationship)
Hi All,
i have masterdetail relationship between quotes(parent) and quotelineitems(child)
and
i also have lookup relationship between product(parent) and quotelineitems(child)
NOTE:there is no direct relationship between quotes and the product
REQUIREMENT:now i want to update a field in quotes object based on some condition in products object using Apex Trigger.
can anyone please let me know how can i proceed with this?
your help is much appreciated!
i have masterdetail relationship between quotes(parent) and quotelineitems(child)
and
i also have lookup relationship between product(parent) and quotelineitems(child)
NOTE:there is no direct relationship between quotes and the product
REQUIREMENT:now i want to update a field in quotes object based on some condition in products object using Apex Trigger.
can anyone please let me know how can i proceed with this?
your help is much appreciated!
In case this helps resolve your problem, please mark this as BEST ANSWER.
All Answers
In case this helps resolve your problem, please mark this as BEST ANSWER.
Iterates over Quoteline item records matching product criteria, saves quote Ids from QuoteLIneItem in a set to fetch their parent Quotes later.
It also prepares a map which has Quote Id as Key and list of All quote line items, as there can be multiple QLI for each Quote.
24-30
Iterating over Quotes fetched from databsaed and updating its field values. Inner loop populated values in Quote from Product records.
//qlilist contains data from all the three objects
list<quotelineitem> qlilist = [select id,quote.id,quote.Angebotsinhalt__c,product2.Name,product2.Gegenkonto__c from quotelineitem where product2.Gegenkonto__c = '84000' or product2.Gegenkonto__c = '84001'];
//i created a quoteIdQliMap to store the data of the quotelineitems
map<id,list<quotelineitem>> quoteIdQliMap = new map<id,list<quotelineitem>>();
//now i am creating the another variable quoteidset to store the quoteid from qlilist
Set<Id> quoteIdSet = new Set<Id>();
for(QuoteLineItem qli : qliList)//go through every element of the qlilist
{
quoteIdSet.add(qli.QuoteId); //and add quote id to quoteIdSet
if(!quoteIdQliMap.containsKey(qli.QuoteId)) //if the map doesnt contain quoteid then
{
quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>()); //add quoteid into the map
quoteIdQliMap.get(qli.QuoteId).add(qli);
}
}
//creating a list to store the data of the quotes object whose ids matches with the ids in quoteIdSet
list<quote> quotelist = new list<quote>();
quotelist = [select id,Name,QuoteNumber,Angebotsinhalt__c from quote Where Id IN: quoteIdSet];
QUOTE fields (parent)< -------masterdetail------------------ > QUOTELINEITEMS fields(child)<--lookup-- >PRODUCT fields(parent)
========== =================== ==============
Name Name
Angebotsinhalt__c Gegenkonto__c
- my requirement is :
i am creating a trigger forif recordtype of quoteobject is 'angebot' and Gegenkonto__c of product object='84000' then
Angebotsinhalt__c (in quote object) should get product name of the record whose gegenkonto is 84000
else if recordtype of quoteobject is 'angebot' and Gegenkonto__c of product object='84001' then
Angebotsinhalt__c (in quote object) should get 'produktion'
else Angebotsinhalt__c(recordtype is angebot) = 'sonstiges'
- to achieve the above requirement i have written the condition like
for(quote quote:quotelist){
if(recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84000')
{
quote.Angebotsinhalt__c = qliList.product2.name;
}
else if(recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84001')
{
quote.Angebotsinhalt__c = 'produktion';
}
else if(recordtype.name == 'angebot)
{
quote.Angebotsinhalt__c = 'sonstiges';
}
}
update quoteList;
- when i incorporate the above piece of code in the total code the complete code will look like
//qlilist contains data from all the three objectslist<quotelineitem> qlilist = [select id,quote.id,quote.name,quote.Angebotsinhalt__c,product2.Name,product2.Gegenkonto__c from quotelineitem where product2.Gegenkonto__c = '84000' or product2.Gegenkonto__c = '84001'];
//i created a quoteIdQliMap to store the data of the quotelineitems
map<id,list<quotelineitem>> quoteIdQliMap = new map<id,list<quotelineitem>>();
//now i am creating the another variable quoteidset to store the quoteid from qlilist
Set<Id> quoteIdSet = new Set<Id>();
for(QuoteLineItem qli : qliList) //go through every element of the qlilist
{
quoteIdSet.add(qli.QuoteId);//and add quote id to quoteIdSet
if(!quoteIdQliMap.containsKey(qli.QuoteId)) //if the map doesnt contain quoteid then
{
quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>()); //add quoteid into the map
quoteIdQliMap.get(qli.QuoteId).add(qli);
}
}
//creating a list to store the data of the quotes object whose ids matches with the ids in quoteIdSet
list<quote> quotelist = new list<quote>();
quotelist = [select id,Name,QuoteNumber,Angebotsinhalt__c from quote Where Id IN: quoteIdSet];
for(quote quote:quotelist)
{
if(recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84000')
{
quote.Angebotsinhalt__c = qliList.product2.name;
}
else if(recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84001')
{
quote.Angebotsinhalt__c = 'produktion';
}
else if(recordtype.name == 'angebot)
{
quote.Angebotsinhalt__c = 'sonstiges';
}
}
update quoteList;
but when i implement it i am getting errors....can u plz rectify me where i am wrong
Thanks!
Add Recordtype.Name in your query, and any other field you need in if conditions
If you dont query field, you can't use it in any logic.
Hope this helps.
By the way, you didnt mention what error you got.
Learn how to share code in a proper readable format, the way I do, it will help others to be able to help you better.
- product object field productname(API name is name) has both read and edit access but even then i am getting the variable doesnt exist error.
can u please help me out with this?Thanks in advance!