Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)

I was trying to create a trigger where if there are 3 consecutive characters then it will not allow. But it is not working. Herewith I have attached my trigger:

public class TaskHandlerController {

public static void onBeforeInsert(List<Task> TaskList){

Set<String> taskID = new Set<String>();

for(Task a: TaskList){

    taskID.add(a.Id);

}

List<Task> updatePurpose = [SELECT Id, Purpose__c, Subject,Description FROM Task WHERE Id IN : taskID];

Map<String,Task> CommentMap = new Map<String,Task>();

for(Task a : updatePurpose){

    if(a.Description == 'aaa'){

        CommentMap.put(a.Description,a);

    }

}

for(Task a: TaskList){

    if(a.Description == 'aaa'){

        a.addError('Your custom error message');

    }

}

}

}

Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)
edited by

There is something wrong in the code, you can try edit your trigger with the below statement.

if(Pattern.compile("(.)\\1{2}").matcher(a.Description).find()) { a.addError('Your custom error message'); }

I hope this will help.

Want to become a Salesforce expert? join Salesforce Integration Training now!!

Want to know more about Salesforce Integration? Watch this video on Salesforce Integration Tutorial | Salesforce Online Training:

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...