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
Swapnil WSwapnil W 

update lookup field with apex class and trigger

Hi All,
Really need help with this issue:  I am trying to populate field Version__c [Lookup(Product)] on QuotLine__c object, whenever New_Management_Version__c (checkbox field) on Product object is set to true. But don't know why Version__c is not getting populated.
Also would like to mention here, QuoteLine child object of Product parent object.
Here is my Apex Class:
public without sharing class QuoteLineManager{
public static void updateVersion(List<QuoteLine__c> lstQuoteLine){
        
        List<QuoteLine__c> softwareQlines = new List<QuoteLine__c>();
        List<QuoteLine__c> versionQlines = new List<QuoteLine__c>();
         
        for(QuoteLine__c ql:lstQuoteLine){
            if(ql.ProductFamily__c == 'Software'){
                softwareQlines.add(ql);
            }
                        if(ql.ProductFamily__c == 'Version'){
                versionQlines.add(ql);
            }
        }
        
        for(QuoteLine__c softQl:softwareQlines){
            for(QuoteLine__c versQl:versionQlines){
                if(versQl.Parent_Product_Name__c == softQl.Parent_Product_Name__c){
                     softQl.Version__c = versQl.SBQQ__Product__c;
            }
          }
        }        
    }
}
   
Apex Trigger:
trigger QuoteLineBeforeUpdate on QuoteLine__c (before update) {
    
    
    List<QuoteLine__c> listQuotelinesNewVersionManagementTrue =  new  List<QuoteLine__c>();
   
// need to verify if the New Version Management is true :
  for (QuoteLine__c ql : Trigger.new) {
         
        if (ql.ProductFamily__c == 'Software' &&  ql.Product__r.New_Version_Management__c == true ){
            //if is true I put it in the list
            listQuotelinesNewVersionManagementTrue.add(ql);
        } 
     }
   
  if (!listQuotelinesNewVersionManagementTrue.isEmpty()) {
           QuoteLineManager.updateVersion(listQuotelinesNewVersionManagementTrue);
      }
     
             
    }

Please let me know if you didn't get clear picture of issue. Looking forward for positive feedback. Thanks in advance.
Best Answer chosen by Swapnil W
Vinod Chokkula 3Vinod Chokkula 3
UPDATED CLASS

public without sharing class QuoteLineManager{
public static void updateVersion(List<QuoteLine__c> lstQuoteLine, List <String> lstparentProductName){
        
        List<QuoteLine__c> updatedQlines = new List<QuoteLine__c>();
        
        List<QuoteLine__c> versionQlines = [select Id, SBQQ__ProductFamily__c, SBQQ__Product__c, Parent_Product_Name__c from SBQQ__QuoteLine__c where SBQQ__ProductFamily__c = 'Version' and Parent_Product_Name__c =:lstparentProductName];
        
        for(QuoteLine__c softQl:lstQuoteLine){
            for(QuoteLine__c versQl:versionQlines){
                if(versQl.Parent_Product_Name__c == softQl.Parent_Product_Name__c){
                     softQl.Version__c = versQl.SBQQ__Product__c;
                     updatedQlines.add(softQl);
            }
          }
        }   
            update updatedQlines;
    }
}

All Answers

Vinod Chokkula 3Vinod Chokkula 3
In the trigger, you are adding quote lines with SOFTWARE product family records to the list, Whereas in the class you are trying to access VERSION quote lines (versionQlines), which is empty.
Swapnil WSwapnil W
Hi Vinod, Thanks for quick reply ! Is this what you want me to do in trigger: if (ql.SBQQ__ProductFamily__c == 'Software' && ql.SBQQ__ProductFamily__c == 'Version' && ql.SBQQ__Product__r.New_Version_Management__c == true ). If yes, Version__c is still not populating.
Vinod Chokkula 3Vinod Chokkula 3
No, one quoteline cannot have two product families at the same time, The list will always be empty.

try with this code

////Trigger

trigger QuoteLineBeforeUpdate on QuoteLine__c (before update) {
    
    
    List<QuoteLine__c> listQuotelinesNewVersionManagementTrue =  new  List<QuoteLine__c>();
   
   //Extra list to capture quoteline Parent_Product_Name__c
   List<String> quotelineParentProductName= new List<String>();
   
// need to verify if the New Version Management is true :
  for (QuoteLine__c ql : Trigger.new) {
         
        if (ql.ProductFamily__c == 'Software' &&  ql.Product__r.New_Version_Management__c == true ){
            //if is true I put it in the list
            listQuotelinesNewVersionManagementTrue.add(ql);
            quotelineParentProductName.add(ql.Parent_Product_Name__c);
        } 
     }
   
  if (!listQuotelinesNewVersionManagementTrue.isEmpty()) {
           QuoteLineManager.updateVersion(listQuotelinesNewVersionManagementTrue,quotelineParentProductName);
      }
     
             
    }

////CLASS

public without sharing class QuoteLineManager{
public static void updateVersion(List<QuoteLine__c> lstQuoteLine, List <String> lstparentProductName){
        
        //List<QuoteLine__c> softwareQlines = new List<QuoteLine__c>();
        
        List<QuoteLine__c> versionQlines = [select ProductFamily__c,SBQQ__Product__c,Parent_Product_Name__c from QuoteLine__c where ProductFamily__c == 'Version' and ProductFamily__c :lstparentProductName ];
         
        
        for(QuoteLine__c softQl:lstQuoteLine){
            for(QuoteLine__c versQl:versionQlines){
                if(versQl.Parent_Product_Name__c == softQl.Parent_Product_Name__c){
                     softQl.Version__c = versQl.SBQQ__Product__c;
            }
          }
        }        
    }
}
 
Swapnil WSwapnil W
Hi Vinod,
 I tried your code and for line
List<QuoteLine__c> versionQlines = [Select Id, SBQQ__ProductFamily__c, SBQQ__Product__c, Parent_Product_Name__c FROM SBQQ__QuoteLine__c WHERE SBQQ__ProductFamily__c == 'Version' AND SBQQ__ProductFamily__c:lstparentProductName] in apex class
its giving error as 'Expression cannot be a statement'. 
Swapnil WSwapnil W
its just that soql line
 List<QuoteLine__c> versionQlines = [select Id, SBQQ__ProductFamily__c, SBQQ__Product__c, Parent_Product_Name__c from SBQQ__QuoteLine__c where SBQQ__ProductFamily__c == 'Version' and SBQQ__ProductFamily__c:lstparentProductName];
is giving error as:

User-added image
Vinod Chokkula 3Vinod Chokkula 3
Try with this query

 List<QuoteLine__c> versionQlines = [select Id, SBQQ__ProductFamily__c, SBQQ__Product__c, Parent_Product_Name__c from SBQQ__QuoteLine__c where SBQQ__ProductFamily__c = 'Version' and Parent_Product_Name__c =:lstparentProductName];
Vinod Chokkula 3Vinod Chokkula 3
UPDATED CLASS

public without sharing class QuoteLineManager{
public static void updateVersion(List<QuoteLine__c> lstQuoteLine, List <String> lstparentProductName){
        
        List<QuoteLine__c> updatedQlines = new List<QuoteLine__c>();
        
        List<QuoteLine__c> versionQlines = [select Id, SBQQ__ProductFamily__c, SBQQ__Product__c, Parent_Product_Name__c from SBQQ__QuoteLine__c where SBQQ__ProductFamily__c = 'Version' and Parent_Product_Name__c =:lstparentProductName];
        
        for(QuoteLine__c softQl:lstQuoteLine){
            for(QuoteLine__c versQl:versionQlines){
                if(versQl.Parent_Product_Name__c == softQl.Parent_Product_Name__c){
                     softQl.Version__c = versQl.SBQQ__Product__c;
                     updatedQlines.add(softQl);
            }
          }
        }   
            update updatedQlines;
    }
}
This was selected as the best answer
Swapnil WSwapnil W
Thank you so much Vinod !