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
sean.gorman@ipc.comsean.gorman@ipc.com 

Updating Opportunity from Opportunity Line Item choice

Hi,

 

I need to set a flag on an Opp if a certain type of product is selected... I have code that works.. but I am pretty sure that it isn't as good as it could be and that it will not work on batch. Can anyone give me pointers please?

 

trigger NewDataPrdct on OpportunityLineItem (after insert, after update) {

	public boolean bData;
    Set<String> PBEIdSet = new Set<String>();
    Set<String> Prod2IdSet = new Set<String>();
    list<Id> OppyIDs = new list<Id>();	
	Map<ID, ID> OppMap = New Map<ID, ID>();
	
//get all DATA products
	list<Product2> DataProds = [ Select prod.id from Product2 prod where prod.Oracle_Product_Family__c like '%data%' and IsActive = TRUE ];

// list through all OLIs to build list of oppIDs
    for (OpportunityLineItem updatedLineItem : System.Trigger.new)   
    {
    	System.debug('Line1');
    	OppMap.put(updatedLineItem.OpportunityId, updatedLineItem.PricebookEntryId);
		OppyIDs.add(updatedLineItem.OpportunityId);

        if (updatedLineItem.PricebookEntryId  != null) 
        {
            PBEIdSet.add(updatedLineItem.PricebookEntryId );
        }
    }

	list<Opportunity> Oppys = [select ID, NS_Data_Opp__c, Business_Type__c from Opportunity where id in :OppyIDs];

    list<PricebookEntry> ParentPricebookEntry = [SELECT Product2Id from PricebookEntry where id in :PBEIdSet];

    for(PricebookEntry pbes: ParentPricebookEntry) 
    {           
        if (pbes.Product2Id != null) 
        {
            Prod2IdSet.add(pbes.Product2Id);
        }
    }

    list<Product2> ParentProd = [SELECT ProductCode from Product2 where id in :Prod2IdSet];

    for(Product2 prods: ParentProd) 
    {
        for(Product2 CheckProds : DataProds)
		{
			if(CheckProds.id == prods.id) 
			{
        	   	bData = true;
        	    break;
       		}
		}
    }
	System.debug('bData = ' + bData);
	if(OppMap.size()>0 && bData)
    {
		System.debug('oppmap size > 0 = ' + OppMap); 
		for(Opportunity O : Oppys)
		{
		System.debug('OOOO ' + O); 
			O.NS_Data_Opp__c = TRUE;
			update O;
		}
	}
}

 

 

vriavmvriavm

Loop through trigger.new and make a list of ids

select opportunityid from opportunitylineitem where pricebookentry.product2.Oracle_Product_Family__c like '%data%' and pricebookentry.product2.IsActive = TRUE and NS_Data_Opp__c != true where id in:<idlist>

make a set (to get unique ids of opportunity) of opportunity and update them accordingly after setting NS_Data_Opp__c = true