function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darrell GallegosDarrell Gallegos 

Remove www. http:// or https:// in Trigger

Greetings. I was hoping to gain some assistance with my Trigger. In the current stated the Trigger works; however, I want to account for the various formats of a website, i.e. www. or http:// or https://.  

Right now I am able to strip off the www. but I cannot figure out how to incorporate code to handle the other options. Can anybody provide assistance?
trigger FindDomain on Lead (before insert, before update) {

    // Populate list with of substring of company removing http(s):// or www.
    List<String> leadStr = new List<String>();
    
    for (Lead newLead : Trigger.new) {
        if(newLead.Website != NULL) {
            String s = newLead.Website.removeStartIgnoreCase('www.');
            leadStr.add(s);
            System.debug(leadStr);
        }
  	}
    
    List<Domain__c> domains = [SELECT Id,Name FROM Domain__c WHERE Name IN :leadStr];
    System.debug(domains);
	
    Map<String, Id> domainToLead = new Map<String, Id>();
    
    for (Domain__c dom : domains) {
		domainToLead.put(dom.Name, dom.Id);
        System.debug(domainToLead);
    }
    
    for (Lead l : Trigger.new) {
        
        if (l.Website != NULL) {
            
            l.Domain__c = domainToLead.get(l.Website.removeStartIgnoreCase('www.'));
            
        } else {
            
            l.Domain__c = NULL;
        }     
    }
}





 
Best Answer chosen by Darrell Gallegos
Prakash NawalePrakash Nawale
Hi Darrell Gallegos,

Please use below code in your trigger.
trigger FindDomain on Lead (before insert, before update) {

    // Populate list with of substring of company removing http(s):// or www.
    List<String> leadStr = new List<String>();
    
    for (Lead newLead : Trigger.new) {
        if(newLead.Website != NULL) {
            String s = newLead.Website.removeStartIgnoreCase('www.');
			 s= s.removeStartIgnoreCase('http://');
			 s= s.removeStartIgnoreCase('https://');
            leadStr.add(s);
            System.debug(leadStr);
        }
  	}
    
    List<Domain__c> domains = [SELECT Id,Name FROM Domain__c WHERE Name IN :leadStr];
    System.debug(domains);
	
    Map<String, Id> domainToLead = new Map<String, Id>();
    
    for (Domain__c dom : domains) {
		domainToLead.put(dom.Name, dom.Id);
        System.debug(domainToLead);
    }
    
    for (Lead l : Trigger.new) {
        
        if (l.Website != NULL) {
            string s = l.Website.removeStartIgnoreCase('www.');
			 s= s.removeStartIgnoreCase('http://');
			 s= s.removeStartIgnoreCase('https://');
            l.Domain__c = domainToLead.get(s);
            
        } else {
            
            l.Domain__c = NULL;
        }     
    }
}

Please like and mark best answer if this helps to you.
 

All Answers

Prakash NawalePrakash Nawale
Hi Darrell Gallegos,

Please use below code in your trigger.
trigger FindDomain on Lead (before insert, before update) {

    // Populate list with of substring of company removing http(s):// or www.
    List<String> leadStr = new List<String>();
    
    for (Lead newLead : Trigger.new) {
        if(newLead.Website != NULL) {
            String s = newLead.Website.removeStartIgnoreCase('www.');
			 s= s.removeStartIgnoreCase('http://');
			 s= s.removeStartIgnoreCase('https://');
            leadStr.add(s);
            System.debug(leadStr);
        }
  	}
    
    List<Domain__c> domains = [SELECT Id,Name FROM Domain__c WHERE Name IN :leadStr];
    System.debug(domains);
	
    Map<String, Id> domainToLead = new Map<String, Id>();
    
    for (Domain__c dom : domains) {
		domainToLead.put(dom.Name, dom.Id);
        System.debug(domainToLead);
    }
    
    for (Lead l : Trigger.new) {
        
        if (l.Website != NULL) {
            string s = l.Website.removeStartIgnoreCase('www.');
			 s= s.removeStartIgnoreCase('http://');
			 s= s.removeStartIgnoreCase('https://');
            l.Domain__c = domainToLead.get(s);
            
        } else {
            
            l.Domain__c = NULL;
        }     
    }
}

Please like and mark best answer if this helps to you.
 
This was selected as the best answer
Darrell GallegosDarrell Gallegos
@Prakash Nawale
Thank you that worked. I appreciate your help. 
Prakash NawalePrakash Nawale
Hi Darrell Gallegos,

Please like and mark best answer.

Thanks