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
ramamohanreddyramamohanreddy 

Too many SOQL errors 101 :

Too many SOQL errors 101 :

trigger OfferDefaults on bid__c (before insert) {

 
    for(bid__c vBid:Trigger.new){
        vBid.Status__c='Open';
        List<Auction__c> vList= [select Id, Name, OwnerId, Seller__c, Seller__r.Id, Seller__r.Name, asset__c, asset__r.Id, qtyAvail__c, minimum_Quantity__c from Auction__c where auction__c.Id=:vBid.auction__c];
       
        if(vList.size()>0){
            Auction__c vAuction= (Auction__c) vList[0];
       
            if(vBid.quantity__c <= 0) {
                vBid.quantity__c.AddError('Invalid input');
            }
            if(vBid.quantity__c > vAuction.qtyAvail__c) {
                vBid.quantity__c.AddError('There are only ' + vAuction.qtyAvail__c + ' items available');
            }
            else if(vBid.quantity__c > vAuction.minimum_quantity__c) {
                vBid.quantity__c.AddError('A minimum of ' + vAuction.minimum_quantity__c + 'items is required');   
            }
            else {
                //Set the Seller
                vBid.Seller__c=vAuction.Seller__c;
                //Set the Asset
                vBid.Asset_ST__c=vAuction.asset__c;
                //Change the OwnerID so that the Seller can see the Offer
                vBid.OwnerId=vAuction.OwnerId;
            }

        }
    }
}

 This trigger for loop is inside the loop. thats why this error occurs. how can I use the query in this trigger?

Suresh RaghuramSuresh Raghuram

HI ramamohan,

 

List<Auction__c> vList= [select Id, Name, OwnerId, Seller__c, Seller__r.Id, Seller__r.Name, asset__c, asset__r.Id, qtyAvail__c, minimum_Quantity__c from Auction__c where auction__c.Id=:vBid.auction__c];

write the above statement out of the for loop.

 

good practise never write the soql and DML statements with in the for loop.

 

If this answers your question make this as asolution.

 

Thanks,

Suresh.

ramamohanreddyramamohanreddy

 Hi suree,

     Thanks for giving me idea, but here one problem happens, That is,

  How could i related this statement

  

   List<Auction__c> vList= [select Id, Name, OwnerId, Seller__c, Seller__r.Id, Seller__r.Name, asset__c, asset__r.Id, qtyAvail__c, minimum_Quantity__c from Auction__c where auction__c.Id=:vBid.auction__c];

 

  to below statement when we write outside

  vBid.Status__c='Open';

 

Thanks

ram

Suresh RaghuramSuresh Raghuram

 vBid.Status__c ='open'

 

you just assigned vBid.Status ='open' but you did not used it any where in the query or in the remaining part of the program.

 

what us Auction__C is it a object.