Too many SOQL queries: 101

I have a trigger which runs i will share a piece of code which is affecting the functionality

for(Pos_Item__c posItem:[SELECT Id,InProcess_Inventory_Stock__c FROM Pos_Item__c WHERE Id IN :posItemsWithInprocessInventory.keySet()])
            {
                if(posItem.InProcess_Inventory_Stock__c==null)
                posItem.InProcess_Inventory_Stock__c=0;
                posItem.InProcess_Inventory_Stock__c-=posItemsWithInprocessInventory.get(posItem.Id);
                posItems.add(posItem);
            }

I tried querying outside the for loop

List<Pos_Item__c> posItemsList = [SELECT Id, InProcess_Inventory_Stock__c FROM Pos_Item__c WHERE Id IN :posItemsWithInprocessInventory.keySet()];
            for(Pos_Item__c posItem:posItemsList)
            {
                if(posItem.InProcess_Inventory_Stock__c==null)
                posItem.InProcess_Inventory_Stock__c=0;
                posItem.InProcess_Inventory_Stock__c-=posItemsWithInprocessInventory.get(posItem.Id);
                posItems.add(posItem);
            }

But still the same issue please help me understand how to overcome this issue

If i comment this piece of code everything is working fine.
But this part is very important

Thanks in advance

Tried using Map and List as well

The code you posted looks good, the for(Pos_Item__c posItem:[SELECT syntax is legit. I think you have something outside this, another loop that calls this one over 100 times. Examine the debug log of your failed action, check if this SELECT... appears in the log over and over… Or post a bigger snippet of the code?

There are static analysis tools like PMD or salesforce code scanner that might help identifying the loop.

There’s also chance you have recursion here (before update trigger, updates something on “this” record, calls same trigger…), sometimes these are bit sneaky if you have trigger + flow for example.

If there’s no outer loop in this trigger / trigger handler itself… Generally what’s the outer code? This looks like some “mass reduce quantity on purchase order lines” – how is this called? 1 bulk update lineItems; or an update statement in a loop?

1

1.  Trigger Recursion: Is it possible that this trigger is firing more than once due to updates to the same records in the same transaction? If so, it might cause undesired changes to the InProcess_Inventory_Stock__c field, especially if the trigger is recursively updating Pos_Item__c records. You can add a recursion prevention mechanism to check if the trigger has already run for a particular record.
2.  Order of Execution: Ensure that there are no other triggers, workflows, or processes that might be modifying InProcess_Inventory_Stock__c at the same time. This could cause unintended behavior.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật