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
Juliver AnoosJuliver Anoos 

am i coding the wrong way? APEX TRIGGER

trigger AS_checkprimaryproduct on OpportunityLineItem (after insert) {

 
    Set<Id> oppId = new Set<Id>();
    List<OpportunityLineItem> queryAllSavedOPL = new List<OpportunityLineItem>();
    List<OpportunityLineItem> primaryProList = new List<OpportunityLineItem>();

    //get OpportunityId to where the product is created
    for(OpportunityLineItem newOLI : trigger.new)
    {
        oppId.add(newOLI.OpportunityId);
    }

    //fiLter Products FROM the Opportunity to get one record (with the oLdest CreatedDate)
    queryAllSavedOPL = [SELECT Id, Name, CreatedDate, AS_Primary_Item__c, OpportunityId 
                        FROM OpportunityLineItem
                        WHERE OpportunityId in:oppId
                        ORDER BY CreatedDate ASC LIMIT 1];

    System.debug('Opp Line Item'+queryAllSavedOPL);

    //set the queried Product to primary by setting AS_Primary_Item__c to true
    for(OpportunityLineItem primaryItem : queryAllSavedOPL)
    {
        primaryItem.AS_Primary_Item__c = true;
        primaryProList.add(primaryItem);
    }
    update primaryProList; 
    System.debug('Primary OppLineItem Product: '+ primaryProList);

}

this trigger will check the primary product that has the oldest createddate. it worked but someone toLd me this needs to be optimized..
Mandalapu BrahmanaiduMandalapu Brahmanaidu
Hi

 Please find the code below.
Trigger AS_checkprimaryproduct on OpportunityLineItem(before insert)
{
    Map<Id,OpportunityLineItem> mapoppLineItem=new Map<Id,OpportunityLineItem>();
    List<OpportunityLineItem> queryAllSavedOPL = new List<OpportunityLineItem>();

    for(OpportunityLineItem objnewOppLineItem:Trigger.new)
    {
        if(objnewOppLineItem.OpportunityId!=null)
        {
            mapoppLineItem.put(objnewOppLineItem.OpportunityId);
        }
        
    }
    if(mapoppLineItem.size()>0)
    {
        for(OpportunityLineItem  primaryItem:[SELECT Id, Name, CreatedDate, AS_Primary_Item__c, OpportunityId  FROM OpportunityLineItem where OpportunityId in:mapoppLineItem.keyset()   ORDER BY CreatedDate ASC LIMIT 1])
        {
            queryAllSavedOPL.add(primaryItem);
        }
    
    }
    if(queryAllSavedOPL.size()>0 && !queryAllSavedOPL.isEmpty())
    {
        for(OpportunityLineItem oppLItem:queryAllSavedOPL)
        {
            primaryItem.AS_Primary_Item__c = true;
            
        }
        update queryAllSavedOPL;
        
    }

}

If you find it's helpful then please mark it as best answer
Thanks&Regards
Brahmanaidu


 
Ajay K DubediAjay K Dubedi
Hi Juliver,
Try to follow this code, as the best practices says when in your trigger have too many lines then you should write trigger and handler class of trigger separately as given below:
trigger:
trigger AS_checkprimaryproduct on OpportunityLineItem (after insert) {

    if(trigger.isInsert && trigger.isAfter) {
        AS_checkprimaryproduct_handler.checkprimaryproduct(trigger.new);
    }
}
handler:
public class AS_checkprimaryproduct_handler {

    public static void checkprimaryproduct(List<OpportunityLineItem> oliList) {
        try {
            Set<Id> opportunityIdSet = new Set<Id>();
            List<OpportunityLineItem> queryAllSavedOPL = new List<OpportunityLineItem>();
            for(OpportunityLineItem obj : oliList)
            {
                opportunityIdSet.add(obj.OpportunityId);
            }
            queryAllSavedOPL = [SELECT Id, Name, CreatedDate, AS_Primary_Item__c, OpportunityId
                                FROM OpportunityLineItem
                                WHERE OpportunityId IN : opportunityIdSet
                                ORDER BY CreatedDate ASC LIMIT 10000];
            
            for(OpportunityLineItem primaryItem : queryAllSavedOPL)
            {
                primaryItem.AS_Primary_Item__c = true;
            }
            update queryAllSavedOPL;
            
        } catch(Exception ex) {
            system.debug('Exception---ofLine--->'+ex.getLineNumber());
            system.debug('Exception---Message--->'+ex.getMessage());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Juliver AnoosJuliver Anoos
@Mandalapo thanks i wiLL try this one. 
Juliver AnoosJuliver Anoos
@Ajay thank you..




i will let u know if it works
Juliver AnoosJuliver Anoos
trigger AS_checkprimaryproduct on OpportunityLineItem (after insert) {

 
    Set<Id> oppId = new Set<Id>();
    List<OpportunityLineItem> queryAllSavedOPL = new List<OpportunityLineItem>();
    List<OpportunityLineItem> primaryProList = new List<OpportunityLineItem>();

    //get OpportunityId to where the product is created
    for(OpportunityLineItem newOLI : trigger.new)
    {
        oppId.add(newOLI.OpportunityId);
    }

    //fiLter Products FROM the Opportunity to get first record (with the oLdest CreatedDate)
    queryAllSavedOPL = [SELECT Id, Name, CreatedDate, AS_Primary_Item__c, OpportunityId 
                        FROM OpportunityLineItem
                        WHERE OpportunityId in:oppId
                        ORDER BY CreatedDate ASC];

    System.debug('Opp Line Item'+queryAllSavedOPL);

    Map<Id, OpportunityLineItem> mapOppIdWithOLI = new Map<Id, OpportunityLineItem>();

    for(OpportunityLineItem OppLineItem : queryAllSavedOPL)
    {
        if(!mapOppIdWithOLI.containsKey(OppLineItem.OpportunityId)) mapOppIdWithOLI.put(OppLineItem.OpportunityId, OppLineItem);
    }
    
    for( OpportunityLineItem opportLineItem : mapOppIdWithOLI.values() ){
        System.debug( opportLineItem.OpportunityId + ' Products are  ' + opportLineItem);
        if(opportLineItem.AS_Primary_Item__c == false ){
            opportLineItem.AS_Primary_Item__c = true;
            primaryProList.add(opportLineItem);
        }
    }
    if(primaryProList.size() > 0) update primaryProList;
     
    //System.debug('Primary OppLineItem Product: '+ primaryProList);

}

this works!