I am getting the expected output, but I would like to get optimized and cleaned code. I am using for inside for in accounttriggerhandler. Since it is not a good practice, I always avoid it.
I tried using the map which would avoid a limit exception, but due to duplicate keys, I am facing some issue.
Here is my objective. I will get the domain name which I should check in metadata and if the domain is found, get the regular expression.
Here is the code I tried using MAP:
for(Website_URL_Redirect_Exception__mdt mdtRecrod:[SELECT Domain__c,Matching_Rule__c,Website_URL_Expression__c
FROM Website_URL_Redirect_Exception__mdt])
{
domainNameByMetadata.put(mdtRecrod.Domain__c,mdtRecrod); //storing metadata to map
}
for( Account a : newAccounts )
{
if( oldAccountMap == NULL || ( oldAccountMap != NULL && a.Website != oldAccountMap.get( a.Id ).Website ) )
{
if( String.isNotBlank( a.Website ) )
{
//link.com/fgf
System.Url websiteURL = new System.Url( ( a.Website.startsWithIgnoreCase( 'http://' ) || a.Website.startsWithIgnoreCase( 'https://' ) ? '' : 'http://' ) + a.Website );//SFDC-2751
String hostURL = websiteURL.getHost();
//Strip out www.
String websiteDomain = hostURL.toLowerCase().remove( 'www.' );//getting here test.com
if(domainNameByMetadata.containsKey(websiteDomain)){//checking if mdt has domain
String matchingPattern = domainNameByMetadata.get(websiteDomain).Matching_Rule__c;
String completeUrl = websiteURL.toExternalForm();
completeUrl = completeUrl.toLowerCase().remove( 'www.' );
Pattern p = Pattern.compile(matchingPattern);
Matcher pm = p.matcher(completeUrl);
if (pm.matches()){
String webSiteUrlPattern = domainNameByMetadata.get(websiteDomain).Website_URL_Expression__c;
Pattern p1 = Pattern.compile(webSiteUrlPattern);
Matcher pm1 = p1.matcher(completeUrl);
if(pm1.matches()){
a.website = pm1.group();
a.Website_Domain__c = websiteDomain;
}else{
a.website=websiteDomain;
}
}else{
a.website=websiteDomain;
}
}
else{
a.Website_Domain__c = websiteDomain;
}
}
else
a.Website_Domain__c = '';
}
}
Here is how the metadata looks like:
I was using the map where the key is domain, and the value is metadatatype. Later the change in data (duplication of domain started my problem). So I changed the code using for inside for:
Map<String,Website_URL_Redirect_Exception__mdt> domainNameByMetadata = new Map<String,Website_URL_Redirect_Exception__mdt>();
List<String> domainNameList = new List<String>();
for(Website_URL_Redirect_Exception__mdt mdtRecrod:[SELECT Domain__c,Matching_Rule__c,Website_URL_Expression__c
FROM Website_URL_Redirect_Exception__mdt])
{
domainNameByMetadata.put(mdtRecrod.Matching_Rule__c,mdtRecrod);
domainNameList.add(mdtRecrod.Domain__c);
}
for( Account a : newAccounts )
{
if( oldAccountMap == NULL || ( oldAccountMap != NULL && a.Website != oldAccountMap.get( a.Id ).Website ) )
{
if( String.isNotBlank( a.Website ) )
{
//link.com/fgf
System.Url websiteURL = new System.Url( ( a.Website.startsWithIgnoreCase( 'http://' ) || a.Website.startsWithIgnoreCase( 'https://' ) ? '' : 'http://' ) + a.Website );//SFDC-2751
String hostURL = websiteURL.getHost();
//Strip out www.
String websiteDomain = hostURL.toLowerCase().remove( 'www.' );
if(domainNameList.contains(websiteDomain)){
String completeUrl = websiteURL.toExternalForm();
completeUrl = completeUrl.toLowerCase().remove( 'www.' );
Boolean isPatternMatched = false;
for(String matchingPattern:domainNameByMetadata.keySet()){
Pattern p = Pattern.compile(matchingPattern);
Matcher pm = p.matcher(completeUrl);
if (pm.matches()){
String webSiteUrlPattern = domainNameByMetadata.get(matchingPattern).Website_URL_Expression__c;
Pattern p1 = Pattern.compile(webSiteUrlPattern);
Matcher pm1 = p1.matcher(completeUrl);
if(pm1.matches()){
a.website = pm1.group();
a.Website_Domain__c = websiteDomain;
isPatternMatched=true;
break;
}else{
isPatternMatched=false;
}
} else{
isPatternMatched=false;
}
}
if(!isPatternMatched)
a.Website_Domain__c = websiteDomain;
}
else{
a.Website_Domain__c = websiteDomain;
}
}
else
a.Website_Domain__c = '';
}
It looks like it is working as expected, but I would like to know if the code looks good or needs any improvement. I tried all the way to avoid for inside for, but neither works. Please guide me.
{
and}
s).