You need to sign in to do that
Don't have an account?
Alessandro Patricelli
Better way to write this code ?
Hi everyone.
I just wrote this trigger that helps me to update some fields based on records of my custom metadata types.
My friend is telling me that my code would hit SOQL Query limits... so it's bad and i must try to find a new way to write this code.
I just checked on help.salesforce.com
--> https://help.salesforce.com/articleView?id=custommetadatatypes_limits.htm&type=5
and it says :
SOQL queries per Apex transaction Unlimited
This is my code:
So, what do you think guys ? Will this code Hit SOQL Query Limits or not?
P.S: Inside my custom metadata type we have only 25 records but maybe in future this number will increase.
Thanks in advance
I just wrote this trigger that helps me to update some fields based on records of my custom metadata types.
My friend is telling me that my code would hit SOQL Query limits... so it's bad and i must try to find a new way to write this code.
I just checked on help.salesforce.com
--> https://help.salesforce.com/articleView?id=custommetadatatypes_limits.htm&type=5
and it says :
SOQL queries per Apex transaction Unlimited
This is my code:
trigger ProfitOfferInsert on Quote (before update, before insert) { EX__mdt[] metaList = [SELECT Business_Unit__c, OfferType__c, ProfitCenter__c, Soluzione__c, Tipo__c FROM EX__mdt]; for(Quote q : Trigger.new) { for(EX__mdt e : metaList) { if(q.Business_Unit__c == e.Business_Unit__c && q.Tipo__c == e.Tipo__c && q.Soluzione__c == e.Soluzione__c) { q.Profit_Center__c = e.ProfitCenter__c; q.OfferType__c = e.ProfitCenter__c; } } } }
So, what do you think guys ? Will this code Hit SOQL Query Limits or not?
P.S: Inside my custom metadata type we have only 25 records but maybe in future this number will increase.
Thanks in advance
* Please use below code it is as per best practices:
Trigger--->
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
All Answers
your code will not hit governer limits, coz u r using SOQL out side of the loop. and try use trigger context variable to make sure this trigger excutes in proper events.
sample code:
hope this helps.
thanks
karthik
I had the same thought about that SOQL Query becuase its outside the for loop.
What about the two nested loop ? Is it ok or it wil have some kind of limitations ?
For example: if I have 100k records inside EX_mdt that for loop will run for 100k records, is that a problem ?
nested loop that not an issue. 100K chance are there for heap size is exceeded error.
tyr use below query and let me know its works or not?
hope this helps.
Thanks
karthik
I tried the query as well it doesnt work
Thanks for you reply and your help...
I dont know if it can be usefull or not, but i was going to change my code like that... just to take one record from my metaList... what do you think ?
try below code
hope this will work for you.
Thanks
karthik
Can i ask few questions ?
1- Why are you trying to put the SOQL query inside the for loop and not anymore outside?
2- line 10-11, why you update metalist? e variable doesnt exist.. i should update Quote q not e or metaList
I tried your code doesnt work because e variable doesnt exist.
Thanks for your answer
* Please use below code it is as per best practices:
Trigger--->
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
thx for you answer but I must update Quote Q not another EX_md (line 17-21).
Thanks anyway
try this
Doesnt work, variable Set id doesnt exist
now i got error on line 15 missing ; at for
put semi colon for list ending stmt.
I have through your question. The code will not hit the governor limit but this is not the best way to write the trigger.
The best practices to write the trigger is:--
To know more about governor limit please visit the link for reference:-
https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com