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
Shruthi NarsiShruthi Narsi 

Trigger to sync products on quotes on select of is syncing

trigger QuoteLineItemTrigger on Quotes__c (Before insert, Before update) 
{
    List<QuoteLineitem__c> quote = new List<QuoteLineitem__c>();
    Set<Id>productidSet =New Set<Id>();
    for(Quotes__c  child : trigger.new)
    {
        if(child.IsSyncing__c != true)
        {
            productidSet.add(child.Product2Id__c);   
            //quote.add(child.Product_Image__c = Product2Id.Product_Image__c);
        }      
    }
   Map<id,Product2__c>ProductMap=New Map<Id,Product2__c>([Select id,Name From Product2__c where id=:productidSet ]);
     for(Quotes__c child : trigger.new)
    {
        if(child.Product2__c != null && ProductMap.containskey(child.Product2Id))
        {
            child.Product2__c = ProductMap.get(child.Product2Id__c).Name;
        }      
    }
}

User-added image
 
Anthony McDougaldAnthony McDougald
Hello Shruthi,
Hope that your day is off to an amazing start. Is this a custom Quotes object or the standard one that comes with Salesforce? We looking forward to helping you. May God bless you abundantly.
Best Regards,
Anthony McDougald
Shruthi NarsiShruthi Narsi
@Anthony McDougald

I got the code corrected and ist wporking fine

I need your help in test class for the code

Below is the trigger

trigger QuoteLineItemTrigger on Quotes__c (after insert, after update)
{
    
    Map<Id,Id> quoteIdToOppId =new Map<Id,Id>();
    Map<Id,List<QuoteLineitem__c>> quoteIdToLineItems = new Map<Id,List<QuoteLineitem__c>>();
    List<OpportunityLineItem__c> oppLineItems = new List<OpportunityLineItem__c>();
    Map<Id,String> oppIdtoName = new Map<Id,String>();
    
    for(Quotes__c q : trigger.new )
    {
        if(q.IsSyncing__c && q.IsSyncing__c  != Trigger.oldMap.get(q.Id).IsSyncing__c ) quoteIdToOppId.put(q.id, q.OpportunityId__c);
    }
    System.debug('UAC: quoteIdToOppId ' + quoteIdToOppId);
    
    for( QuoteLineitem__c qli :[SELECT Id, Product2Id__c,Product2Id__r.name,Quantity__c,ServiceDate__c, QuotesId__c,ListPrice__c,Line_Item_Description__c,Total_Price__c,Product2Id__r.Product_Code__c,UnitPrice__c  FROM QuoteLineitem__c WHERE QuotesId__c =:quoteIdToOppId.keyset() ])
    {
        List<QuoteLineitem__c> tempList = quoteIdToLineItems.get(qli.QuotesId__c) ;
        if(tempList == null)
        {
            
            templist = new List<QuoteLineitem__c>();
            quoteIdToLineItems.put(qli.QuotesId__c, templist);
           
        }
        tempList.add(qli);
    }
    System.debug('UAC: quoteIdToLineItems ' + quoteIdToLineItems);
    
    for(Opportunities__c opp : [SELECT Id , Name  FROM Opportunities__c WHERE ID IN :quoteIdToOppId.values()] )
        


        
    {
        oppIdtoName.put(opp.id, opp.Name)   ;
    }
    System.debug('UAC: oppIdtoName ' + quoteIdToLineItems);
    
    for(Quotes__c q : Trigger.new)
    {
        if(q.IsSyncing__c && quoteIdToLineItems.containsKey(q.Id))
        {
            for(QuoteLineitem__c qli : quoteIdToLineItems.get(q.Id))
            {
                OpportunityLineItem__c oli = new OpportunityLineItem__c();
                oli.OpportunityId__c = quoteIdToOppId.get(qli.QuotesId__c); 
                System.debug(' qli.Product2Id__r.name ' +  qli.Product2Id__r.name);
                oli.Name = qli.Product2Id__r.name;
                System.debug(' qli.Product2Id__c ' +  qli.Product2Id__c);
                System.debug(' qli.ListPrice__c ' +  qli.ListPrice__c);
                System.debug(' qli.Line_Item_Description__c ' +  qli.Line_Item_Description__c);
                oli.Product2Id__c = qli.Product2Id__c; 
                oli.Quantity__c = qli.Quantity__c; 
                oli.ListPrice__c = integer.valueof(qli.ListPrice__c); 
                oli.Description__c = qli.Line_Item_Description__c;
                oli.ServiceDate__c = qli.ServiceDate__c;
                oli.Total_Price__c = qli.Total_Price__c;
                oli.ProductCode__c = qli.Product2Id__r.Product_Code__c;
                oli.Sales_Price__c =qli.UnitPrice__c;
                oppLineItems.add(oli);
            }
            
        }
        System.debug('UAC: oppLineItems ' + oppLineItems);
        if(oppLineItems.size() > 0) insert oppLineItems ;
        
    }
}

below is the test class

    
    @isTest
public class QuoteLineItemTestClass {

@TestSetup private static void setUpData(){

        Quotes__c a = New Quotes__c(Name='Test');
        insert a;

        Opportunities__c o = New Opportunities__c(
                StageName = 'test',
                CloseDate = date.today(), 
                Name = 'test', 
                QuotesId = a.id
        );

        insert o;

        //....Create Products, pricebookentries, etc needed for the QLI and the OLI

        OpportunityLineItem__c oli = New OpportunityLineItem__c(
                .....
        );

        insert oli;

        Quotes__c q = New Quotes__c(

                ......
        );

        insert q;

        QuoteLineitem__c     qli = New QuoteLineitem__c    (
                ......
        );

        insert qli;

    }

    static testMethod void  basicTest() {

         QuoteLineitem__c qli = [Select Id, ... From QuoteLineitem__c    ]; //We know we created one and only one

        qli.SyncCheck__c = 'check'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select IsSyncing__c From OpportunityLineItem__c];

        system.assertEquals('check',oli.IsSyncing__c, 'The field was not updated when it should have been');

    }

    static testMethod void  basicTest_NoProductMatch() {
        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New QuoteLineitem__c(.....);
        insert qli;

        qli.IsSyncing__c = 'check'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select IsSyncing__c From OpportunityLineItem__c];

        system.assertNotEquals('check',oli.IsSyncing__c,'The field was updated when the product did not match the updated QLI');

    }


}
     
User-added image

 
Shruthi NarsiShruthi Narsi
Im working on force.com and all the objects are custome which I have created