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
ruchika Nayyarruchika Nayyar 

Once an opportunity line item will create update Opportunity's Account with the total number of Product Quantities.

trigger OPPTRIGGER on OpportunityLineItem (after update) {
    Set<Id> OLI= new set<Id>();
    for(OpportunityLineItem obj: TriggerNew)
    {
        SetOLI.add(obj.Id);
        
    }
    Map<ID, OpportunityLineItem> map1= New Map<ID, OpportunityLineItem>();
    for(OpportunityLineItem obj:[Select Id,Name,quantity,opportunity,opportunity.AccountId,Opportunity.Account.myruchika__Total_sales_quantities__c from OpportunityLineItem where ID in :Set OLI])
    {
        if(obj.opportunity!=null && obj.Opportunity.AccountId!= null)
        {
            if(map1.get(obj.Opportunity.AccountId)== null)
            {
                map1.put(obj.Opportunity.AccountId, New list<OLI> New OpportunityLineItem)
                {
                    map1.get(obj.Opportunity.AccountId).Add(obj);
                }
            
            }
        }
    }
    list<Account> lstacc= New list<Account>();
    for(Account obj:[Select Id,myruchika__Total_sales_quantities__c from account where ID in:Map.keySet()])
    {
        if(map1.get(obj.Id)!=null)
        {
            decimal total quantities=0;
        }
    }
       for(OpportunityLineItem objLI:Map1.get(obj.Id)) 
       {
           totalquantity+=objLI.quantity
       }
       if(obj.myruchika__Total_sales_quantities__c==null) 
       {
           obj.myruchika__Total_sales_quantities__c=0;
       }
      obj.myruchika__Total_sales_quantities__c+=totalquantity;
    lstacc.add(obj);
}
updateOpportunityLineItem.size() >0)
        update updateOpportunityLineItem;
}
}

error is coming in this code
please help me for this code 
pconpcon
I will start by saying that your code isn't valid and won't even compile.  Second, let's take a step back and talk about what you are trying to do with this code.  Are you trying to get a "rollup" the total amounts from your Opportunity Line Items to your accounts?  Can you please provide some sample data and what should be stored where after your insert occurs?

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
ruchika Nayyarruchika Nayyar
i want to create this code Once an opportunity line item will create update Opportunity's Account with the total number of Product Quantities. Create a field first on Account "Total sales quantities". i am getting error
pconpcon
I would suggest that you use an aggregate query [1] instead.
 
trigger ProductCount on OpportunityLineItem(after update) {
    Set<Id> opportunityIds = new Set<Id>();

    for (OpportunityLineItem oli : Trigger.new) {
        opportunityIds.add(oli.OpportunityId);  
    }

    opportunityIds.remove(null);

    if (!opportunityIds.isEmpty()) {
        Set<Id> accountIds = new Set<Id> accountIds;

        for (Opportunity opp : [
            select AccountId
            from Opportunity
            where Id in :opportunityIds
        ]) {
            accountIds.add(opp.AccountId);
        }   

        if (!accountIds.isEmpty()) {
            List<Account> accountsToUpdate = new List<Account>();

            for (AggregateResult ar : [
                select SUM(Quantity),
                    Opportunity.AccountId
                from OpportunityLineItem
                where Opportunity.AccountId in :accountIds
            ]) {
                Decimal total = Decimal.valueOf(ar.get('expr0'));

                accountsToUpdate.add(new Account(
                    Id = ar.Opportunity.AccountId,
                    myruchika__Total_sales_quantities__c = total
                ));
            }   

            if (!accountsToUpdate.isEmpty()) {
                update accountsToUpdate;
            }
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm
Rahul Gupta 137Rahul Gupta 137
can you explain line number 30 whathave you done here in line num 30 @pcon
pconpcon
That is pulling the SUM value out of the Aggregate Result query.  If you had multiple aggregate functions you would use expr0 expr1 expr2 etc.
Lizitha sri KodaliLizitha sri Kodali
Hi, I want to write trigger on opplineitem, whenever product is added or deleted or updated , it should get all product names associated with that opp on account custom field? Can you please help