You need to sign in to do that
Don't have an account?
Akshay Mense
Avoid Soql in for loop query with below example
Hello, I want to remove SOQL from for loop below is trigger. how to do it?
trigger UpdateOwner on Suggestion__c (before insert) {
for (Suggestion__c sugg : Trigger.new)
{
if (sugg.Rep_Email__c != null )
{
User usrLst = [SELECT Id, Name, Email FROM User WHERE Email =: sugg.Rep_Email__c];
if(usrLst != null){
sugg.OwnerId = usrLst.Id;
}
}
}
}
trigger UpdateOwner on Suggestion__c (before insert) {
for (Suggestion__c sugg : Trigger.new)
{
if (sugg.Rep_Email__c != null )
{
User usrLst = [SELECT Id, Name, Email FROM User WHERE Email =: sugg.Rep_Email__c];
if(usrLst != null){
sugg.OwnerId = usrLst.Id;
}
}
}
}
You can try the below snippet and modify it accordingly.
Please do note that this is a sample snippet and you need to modify it as needed.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.