
trigger CasetoAccountInsert on Case (before insert) {
set<String> emailSet = new set<String>();
map<String,Id> mapOpp = new map<String,Id>();
map<String,Id> mapAcc = new map<String,Id>();
for(Case record: Trigger.new) {
emailSet.add(record.SuppliedEmail);
}
for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY CreatedDate DESC]){
if(mapOpp.get(opp.Contact_Email__c) == null){
mapOpp.put(opp.Contact_Email__c, opp.Id);
mapAcc.put(opp.Contact_Email__c, opp.AccountId);
}
}
for(Case cs : Trigger.new){
cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);
cs.AccountId = mapAcc.get(cs.SuppliedEmail);
}
}
I notice that you added Limit 1 in query.
Please don't modify any piece of code above, else it will not work and just paste this code and try. If it still not work let me know