I have to write a class that accepts input from a trigger. This class checks "DBD_ID" when users are trying to create a new lead. If the condition is not correct, then throw an error message. If the condition is correct, they can create and save a new lead, and the "business type" field of the lead will be automatically updated according to the master object. I don't know how to update the "business type" field, and the error messages are shown as "We hit a snag." instead. Please help me!
1.LeadHandler.apxc
public class LeadHandler {
public static void CheckDBD(List<Lead> newLeads) {
//LeadHandler.CheckDBD([select Name, DBD_ID__c from Lead]); **execution code
//List<String> dbdid = new List<String>();
List<List<String>> dbdid = new List<List<String>>();
//List<String> pass = new List<String>();
List<List<String>> pass = new List<List<String>>();
List<List<String>> dbdprivate = new List<List<String>>();
List<List<String>> dbdpublic = new List<List<String>>();
List<Lead> testLead = new List<Lead>();
Integer count_private = 0;
Integer count_public = 0;
DBD_Info__c[] dbdinfo_private = [select Type__c, starting_number__c, End_number__c
from DBD_Info__c order by Type__c];
DBD_Info__c[] dbdinfo_public = [select Type__c, public_co_number__c
from DBD_Info__c order by Type__c];
//keep dbd_id for each lead
for(Lead lead : newLeads) {
//dbdid.add(lead.DBD_ID__c);
dbdid.add(new List<String>{String.valueOf(lead.Name),
String.valueOf(lead.DBD_ID__c), String.valueOf(lead.Business_Type__c)});
}
//keep public_co_number__c
for(DBD_Info__c dbdpub : dbdinfo_public){
dbdpublic.add(new List<String>{String.valueOf(dbdpub.Type__c),
String.valueOf(dbdpub.public_co_number__c)});
}
system.debug(dbdpublic);
//keep private dbdid
for(DBD_Info__c dbdpri : dbdinfo_private){
dbdprivate.add(new List<String>{String.valueOf(dbdpri.Type__c),
String.valueOf(dbdpri.starting_number__c),
String.valueOf(dbdpri.End_number__c)});
}
system.debug(dbdprivate);
//check condition for input dbdid
for(List<String> x : dbdid){
//reject if empty (for debugging, we continue)
if(String.isEmpty(x[1])){
//continue;
//string is empty, throw an error here
newLeads[0].DBD_ID__c.addError('Please input your DBD ID');
}
//check length of input dbdid = 5
if(x[1].length()==5){
system.debug('pass = '+x);
pass.add(new List<String>{String.valueOf(x[0]),
String.valueOf(x[1]),String.valueOf(x[2])});
}
else{
//not pass the requirement, throw an error here
newLeads[1].addError('Invalid DBD ID');
}
}
//check public or private
for(List<String> y : pass){
//check private, if first letter is number
if(y[1].substring(0,1).isNumeric()){
system.debug(y+' = private');
for(List<String> m : dbdprivate){
//compare input dbd in the dbd information
if(y[1]>=m[1] && y[1]<=m[2]){
system.debug('in the range'+m[1]+' '+m[2]);
//then specify type to m[0]
system.debug('type = '+m[0]);
count_private = 0; //reset to zero
//update business type to m[0]
break;
}
else{
system.debug('not in the range ');
system.debug(count_private);
count_private += 1;
}
if(count_private == 21){
//system.debug('your dbd id is not in any range');
//error not match in the master, throw an error here
newLeads[1].addError('your dbd id is not in any range');
}
}
}
//check public
else{
system.debug(y+' = public');
system.debug(dbdpublic);
for(List<String> n : dbdpublic){
//compare input dbd in the dbd information
if(y[1].Contains(n[1])){
system.debug('yes '+y+'='+n[1]);
system.debug('type = '+n[0]);
count_public = 0; //reset to zero
//update business type to n[0]
break;
}
else{
system.debug('no');
system.debug(count_public);
count_public += 1;
}
if(count_public == 21){
system.debug('not match');
//error not match in the master, throw an error here
newLeads[1].addError('your dbd id is not in the master');
}
}
}
}
} }
2.LeadTriggerHandler.apxc
/**
* @description Lead trigger implementation class
*/
public with sharing class LeadTriggerHandler implements ITriggerHandler {
public static Boolean TriggerDisabled = false;
public Boolean isDisabled() {
return TriggerDisabled;
}
public void beforeInsert(List<SObject> newItems) {
LeadHandler.CheckDBD(newItems);
}
public void beforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {
}
public void beforeDelete(Map<Id, SObject> oldItems) {
}
public void afterInsert(Map<Id, SObject> newItems) {
}
public void afterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {
//LeadHandler.CheckDBD(newItems.values());
}
public void afterDelete(Map<Id, SObject> oldItems) {
}
public void afterUndelete(Map<Id, SObject> oldItems) {
}
}
3.LeadTrigger.apxt
/**
* @description trigger for Lead
*/
trigger LeadTrigger on Lead(
before insert,
before update,
before delete,
after insert,
after update,
after delete,
after undelete
) {
TriggerDispatcher.run(new LeadTriggerHandler());
}