function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vikas KVikas K 

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!
Best Answer chosen by Vikas K
mritzimritzi
Please use this as guidance, change field/object api names. Hope this works for you
List<QuoteLineItem> qliList = new List<QuoteLineItem>([
 Select Id, Product__c, Product__r.name, QuoteId, <Other fields>
 From QuoteLineItem
 Where Product__c = '<ProductI d>'  OR Product__r.Name = '<some name>'
 // any other criteria referencing product fields
]);
//Map<Quote Id, QuoteLineItem record>
Map<Id, List<QuoteLineItem>> quoteIdQliMap = new Map<Id, List<QuoteLineItem>>();
// used to get quote Ids from QuoteLineItem records
Set<Id> quoteIdSet = new Set<Id>();
for(QuoteLineItem qli : qliList){
    quoteIdSet.add(qli.QuoteId);
    if(!quoteIdQliMap.containsKey(qli.QuoteId))
        quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>());
    quoteIdQliMap.get(qli.QuoteId).add(qli);
}

List<Quote> quoteList = new List<Quote>([
    Select Id, Name, <Some other fields>
    From Quote
    Where Id IN: quoteIdSet
]);

for(Quote quote : quoteList){
    quote.Name = '<some value>'
    //some more field updates 
    //if you want to copy values from Prod
    for(QuoteLineItem qli : quoteIdQliMap.get(quote.Id)){
        quote.<some field> = qli.Product__r.<some field>
    }
}

update quoteList;

In case this helps resolve your problem, please mark this as BEST ANSWER.
 

All Answers

Сергей Жугин 7Сергей Жугин 7
Hi, i think your querry will look something like this [SELECT Id, (SELECT quotes.yourfield FROM quotelineitems__r) FROM product], adjust this qurry for your task
mritzimritzi
Please use this as guidance, change field/object api names. Hope this works for you
List<QuoteLineItem> qliList = new List<QuoteLineItem>([
 Select Id, Product__c, Product__r.name, QuoteId, <Other fields>
 From QuoteLineItem
 Where Product__c = '<ProductI d>'  OR Product__r.Name = '<some name>'
 // any other criteria referencing product fields
]);
//Map<Quote Id, QuoteLineItem record>
Map<Id, List<QuoteLineItem>> quoteIdQliMap = new Map<Id, List<QuoteLineItem>>();
// used to get quote Ids from QuoteLineItem records
Set<Id> quoteIdSet = new Set<Id>();
for(QuoteLineItem qli : qliList){
    quoteIdSet.add(qli.QuoteId);
    if(!quoteIdQliMap.containsKey(qli.QuoteId))
        quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>());
    quoteIdQliMap.get(qli.QuoteId).add(qli);
}

List<Quote> quoteList = new List<Quote>([
    Select Id, Name, <Some other fields>
    From Quote
    Where Id IN: quoteIdSet
]);

for(Quote quote : quoteList){
    quote.Name = '<some value>'
    //some more field updates 
    //if you want to copy values from Prod
    for(QuoteLineItem qli : quoteIdQliMap.get(quote.Id)){
        quote.<some field> = qli.Product__r.<some field>
    }
}

update quoteList;

In case this helps resolve your problem, please mark this as BEST ANSWER.
 
This was selected as the best answer
Vikas KVikas K
hallo mritzi....thanx for ur response....i got u but if possible can u be more generous and brief about whats happening in the code as a whole or atleast particularly between the lines 8:16 and 24:30 as i am bit new to salesforce
mritzimritzi
8-16
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.
Vikas KVikas K
hallo rizwan i appreciate your consistent help
  • taking ur code as reference i have written the code as but i am struggling to give my requirement in last part of the code

//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 for 

if  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 objects 
list<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!


                                               
mritzimritzi
following line should be written after end of if block
if(!quoteIdQliMap.containsKey(qli.QuoteId)){
    quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>());
 }
 quoteIdQliMap.get(qli.QuoteId).add(qli);

Add Recordtype.Name in your query, and any other field you need in if conditions
 
quotelist = [select id,Name,QuoteNumber,Angebotsinhalt__c, RecordTypeId, RecordType.Name from quote Where Id IN: quoteIdSet];

//and use it with varable name
for(quote quote:quotelist)     
 {
   // determine which record of qliList do you want to compare with, you cant access a field from list
   if(quote.recordtype.name == 'angebot' && qliList[index variable here].product2.Gegenkonto__c == '84000')
       {
         quote.Angebotsinhalt__c = qliList.product2.name;
       }
}
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.
Vikas KVikas K
  • Hallo Rizwan,after all changes total code looks like this
 
trigger test2 on Quote (before insert) 
{
    if(trigger.isinsert && trigger.isbefore)
    {
        for(set<quote> q:trigger.new)
        {
            list<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'];

            map<id,list<quotelineitem>> quoteIdQliMap = new map<id,list<quotelineitem>>();

            Set<Id> quoteIdSet = new Set<Id>();

            for(QuoteLineItem qli : qliList)
            {
                quoteIdSet.add(qli.QuoteId);
                if(!quoteIdQliMap.containsKey(qli.QuoteId))
                {
                    quoteIdQliMap.put(qli.QuoteId, new List<QuoteLineItem>());
                    
                }
                quoteIdQliMap.get(qli.QuoteId).add(qli);
            }

            list<quote> quotelist = new list<quote>();
            quotelist = [select id,Name,QuoteNumber,Angebotsinhalt__c,recordtypeid,recordtype.name from quote Where Id IN: quoteIdSet];

            for(quote quote:quotelist)     
            {
                if(quote.recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84000')
                {
                    quote.Angebotsinhalt__c = qliList.product2.name;
                }
                else if(quote.recordtype.name == 'angebot' && qliList.product2.Gegenkonto__c == '84001')
                {
                    quote.Angebotsinhalt__c = 'produktion';
                }
                else if(quote.recordtype.name == 'angebot')
                {
                    quote.Angebotsinhalt__c = 'sonstiges';
                }
                update quoteList;
            }
        }
    }
}
  • and the errors are 
User-added image
User-added image
  • 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!