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
SFDC-DMGSFDC-DMG 

Too many SOQL Queries Errors

Hello,

 

I created the below trigger to automatically update the discount on the opportunity product to reflect the discount calculated on the opportunity level based on a number of field selections. It is working great until we try and make any kind of mass update, and then I get the following error: System.LimitException: Too many SOQL queries.

 

Below is my code - is anyone able to help me fix it to avoid the Too Many SOQL Queries errors that I'm running into??

 

Thank you!!!!

 

trigger Opp_Line_Item_Update on Opportunity (after update) {
for( Opportunity opp: Trigger.new)
{

List<OpportunityLineItem> lineitem = [ SELECT Id, Opportunity.ID, Discount from
OpportunityLineItem where opportunity.isclosed = false and Opportunity.id = :opp.Id];

List<OpportunityLineItem> lineitemToUpdate = new List<OpportunityLineItem>();

for(OpportunityLineItem thislineitem: lineitem )
{
   if( thislineitem.discount !=  opp.total_extended_discount__c)
   {
       thislineitem.discount =   opp.total_extended_discount__c;
      lineitemToUpdate.add(thislineitem);
   }
}

if( lineitemToUpdate!=null)
{
   update lineitemToUpdate;
}

}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Hi,

 

First Error - The correct field name is OpportunityId not Opportunity__c

 

Second Error - Try to change the map from 

map<id,decimal>oppmap=newmap<id,decimal>();

to

map<id,Double>oppmap=newmap<id,Double>();


and also change from

decimal opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;

 

to

Double opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;

 

Let me know what happened.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

All Answers

Rahul_sgRahul_sg
you need to take the SOQL query out of the for loop.

create a map with <Id,OpportunityLineItem> and then use that map within your for loop to fetch the lineitem using opp.Id
sushant sussushant sus

try this

trigger Opp_Line_Item_Update on Opportunity (after update)
{
set<id> ids=newlist<id>();
map<id,string>oppmap=newmap<id,string>();
for( Opportunity opp: Trigger.new)
{
ids.add(opp.id);
oppmap.put(opp.id,opp.total_extended_discount__c)
}

List<OpportunityLineItem> lineitem = [ SELECT Id, Opportunity.ID, Discount from
OpportunityLineItem where opportunity.isclosed = false and Opportunity.id in = :ids];

List<OpportunityLineItem> lineitemToUpdate = new List<OpportunityLineItem>();

for(OpportunityLineItem thislineitem: lineitem )
{
string opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;
if( thislineitem.discount != opptotaldiscount)
{
thislineitem.discount = opptotaldiscount;
lineitemToUpdate.add(thislineitem);
}
}

if( lineitemToUpdate!=null)
{
update lineitemToUpdate;
}

}
it will work on bulk data i guess....
please check

SFDC-DMGSFDC-DMG

This looks like it will be a great solution, thank you!!

 

I'm running into an error when I copy your code into Salesforce, and it's the following - Error: Compile Error: unexpected token: '(' at line 3 column 23.

 

I'm not sure why it won't accept that because it looks right to me, and I am playing around with it, but not able to get it yet. Any thoughts??

 

Thank you so much for your help!

sushant sussushant sus
semicon is missing in

oppmap.put(opp.id,opp.total_extended_discount__c)
souvik9086souvik9086

Hi

 

The code seems to be fine.

just fix this small change.

 

set<id> ids=new list<id>();

 

Just give a space after new.

 

And also as sushant sus mentioned fix this

semicon is missing in

oppmap.put(opp.id,opp.total_extended_discount__c)

 

Please Give the semicolon

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

SFDC-DMGSFDC-DMG

Ahh!

 

Thank you guys for your help. It keeps running into errors and I'm fixing them as I got, but I haven't been able to even get it to save yet because of the errors. I'm getting a range of messages, but I predominantly keep running into one along the lines of "Incompatible key type Decimal (or string) for map<ID, Decimal (or string)>. The () are because it wasn't accepting the string value for the discount field, so I tried changing it to a decimal.

 

I seem to keep running into issues around this line now and I can't seem to get past them:

 

string opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;
if( thislineitem.discount != opptotaldiscount)

 

I'm still a semi-newbie to apex code, so I've been trying to figure this out, but I'm getting to the point of just banging my head against the wall, so any kind of further help/guidance would be so very much appreciated.

 

Thanks!

 

sushant sussushant sus
change this line in my code

map<id,string>oppmap=newmap<id,string>();

to

map<id,decimal>oppmap=newmap<id,decimal>();

and

for(OpportunityLineItem thislineitem: lineitem )
{
string opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;
//here string to decimal ..
SFDC-DMGSFDC-DMG

Thank you!

 

Yes, I had tried doing that and had made both of those changes actually, but am still having issues.

 

When I use the following:

 

 

for(OpportunityLineItem thislineitem: lineitem )
{
decimal opptotaldiscount=oppmap.get(thislineitem.opportunity_c);

 

 

I get two errors. The first error message that I get is "Invalid field opportunity__c for SObject OpportunityLineItem." Then, everytime that I try to change the opportunity__c field, I end up with the "Incompatible key type for MAP<ID, Decimal>

 

Thank you for your patience and consistent help. I am sorry that I am struggling so much with this, but I really appreciate all of your input!

 

Thanks!

 

souvik9086souvik9086

Hi,

 

First Error - The correct field name is OpportunityId not Opportunity__c

 

Second Error - Try to change the map from 

map<id,decimal>oppmap=newmap<id,decimal>();

to

map<id,Double>oppmap=newmap<id,Double>();


and also change from

decimal opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;

 

to

Double opptotaldiscount=oppmap.get(thislineitem.opportunity__c) ;

 

Let me know what happened.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks

 

This was selected as the best answer
sushant sussushant sus
try with
map<id,double>oppmap=newmap<id,double>();


double opptotaldiscount=oppmap.get(thislineitem.opportunity__c);