You need to sign in to do that
Don't have an account?
Juliver Anoos
this trigger will check the primary product that has the oldest createddate. it worked but someone toLd me this needs to be optimized..
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..
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
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
i will let u know if it works
this works!