How to avoid "For loop inside loop" for the below code
For Each Account Conataines multiple Specifications, for the Specifications contains "Region" need to insert the name in Region field on Account
There is No direct relation b/w both objcets, based on the Offer__Id__c on account we are fetching the list of specifications
Trigger AccountUpdate on Account(Before Insert){
List<String> Region = new List<String>();
List<String> SubRegion = new List<String>();
List<Id>OfferIds= new List<Specification__c>();
for(Account A : Trigger.New){
OfferIds.add(A.Offer__Id__c)
}
List<Specification__c> OfferSpec=[Select id ,Name,Level__c,Offer__Id__c from Specification__c where Offer__Id__c IN : OfferIds ]
// OfferSpec Conatines list List of Specifications
//For Each Account Conaties multiple Specifications, for the Specifications contains "Region" need to insert the name in Region field on Account
///For Each Account Conaties multiple Specifications, for the Specifications contains "SubRegion" need to insert the name in SubRegion field on Account
for(Account Ac: Trigger.New){
for(Specification__c Off : OfferSpec){
if(Ac.Offer__Id__c == Offsp.Offer__Id__c && Off.Level__c != null && Off.Level__c.contains('Region')){
Region.add(Off.Name);
}
else if (Ac.Offer__Id__c == Off.Offer__Id__c && Off.Level__c != null && Off.Level__c.contains('SubRegion')){
SubRegion.add(Off.Name);
}
}
string allRegions = string.join(Region,',');
string allSubRegions = string.join(SubRegion,',');
Ac.Region__c=allRegions;
Ac.SubRegion__c=allSubRegions;
}
}