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
trailhead solutions 10trailhead solutions 10 

Trigger on PhoneNum - Not Working

Hello

My Requirement is to, whenever there is Phone Number entered in case Object, then in that case, if User enters any Special Characters, then it should ignore the Special Characters and display only the Digits.

I wrote a Trigger on it. Seems to be Correct. But, not working while I am testing.Please someone test in your Org and let me know, where I did mistake.

trigger PhNumValidation on Case (before insert,before update) {
    list<case> clist=new list<case>();
    for(Case c:Trigger.new){
      if (c.Phone_Number__c != null ) {
           string phone =c.Phone_Number__c;
          string newph=phone.replaceAll('\\D','');
          system.debug('Ph: '+newph);
        }   
          
           
        }
        
    }


Thanks
GSN
Best Answer chosen by trailhead solutions 10
Maharajan CMaharajan C
Hi GSN,

Please try the below code:

Assign the Value to c.Phone_Number__c... It's missing in your code so it's not working...
 
trigger PhNumValidation on Case (before insert,before update) {
    for(Case c:Trigger.new){
        if(!String.isBlank(c.Phone_Number__c)) {
            string phone =c.Phone_Number__c;
            c.Phone_Number__c = phone.replaceAll('\\D','');
        }   
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi GSN,

Please try the below code:

Assign the Value to c.Phone_Number__c... It's missing in your code so it's not working...
 
trigger PhNumValidation on Case (before insert,before update) {
    for(Case c:Trigger.new){
        if(!String.isBlank(c.Phone_Number__c)) {
            string phone =c.Phone_Number__c;
            c.Phone_Number__c = phone.replaceAll('\\D','');
        }   
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
trailhead solutions 10trailhead solutions 10
It Works Perfect!!!!
Thanks a ton Maharajan..