You need to sign in to do that
Don't have an account?
Wes Reed 27
Display Field Label & Create Task related to Lead
In the last for each loop, I'd like to display the field label rather than the field value (which is how it is currently) for the task subject. I'd also like the tasks to be related to the Lead.
trigger LeadKeyFieldsPopulated on Lead (before insert) { for(Lead l : Trigger.new){ List<String> fields = new List<String>(); fields.add(l.FirstName); fields.add(l.LastName); fields.add(l.Phone); fields.add(l.Email); fields.add(l.Website); fields.add(l.Title); List<String> populatedFields = new List<String>(); Integer count = 0; for(String field : fields){ if(String.isNotBlank(field)){ count = count + 1; l.Key_Fields_Populated__c = count; populatedFields.add(field); } } List<String> labels = new List<String>(); labels.add(Schema.Lead.fields.FirstName.getDescribe().getLabel()); labels.add(Schema.Lead.fields.LastName.getDescribe().getLabel()); labels.add(Schema.Lead.fields.Phone.getDescribe().getLabel()); labels.add(Schema.Lead.fields.Email.getDescribe().getLabel()); labels.add(Schema.Lead.fields.Website.getDescribe().getLabel()); labels.add(Schema.Lead.fields.Title.getDescribe().getLabel()); List<Task> taskToCreate = new List<Task>(); if(l.Key_Fields_Populated__c >= 3) { for(String field : populatedFields) { Task myTask = new Task(); myTask.WhatId = l.Id; myTask.Subject = 'Verify the ' + field + ' field'; taskToCreate.add(myTask); } insert taskToCreate; } } }
trigger LeadKeyFieldsPopulated on Lead (before insert) {
for(Lead l : Trigger.new){
Map<String,String> fieldMap = new Map<String,String>();
fieldMap.put(Schema.Lead.fields.FirstName.getDescribe().getLabel(),l.FirstName);
fieldMap.put(Schema.Lead.fields.LastName.getDescribe().getLabel(),l.LastName);
fieldMap.put(Schema.Lead.fields.Phone.getDescribe().getLabel(),l.Phone);
fieldMap.put(Schema.Lead.fields.Email.getDescribe().getLabel(),l.Email);
fieldMap.put(Schema.Lead.fields.Website.getDescribe().getLabel(),l.Website);
fieldMap.put(Schema.Lead.fields.Title.getDescribe().getLabel(),l.Title);
List<String> populatedFields = new List<String>();
Integer count = 0;
for(String field : fieldMap.keySet()){
if(String.isNotBlank(fieldMap.get(field))){
count = count + 1;
l.Key_Fields_Populated__c = count;
populatedFields.add(field);
}
}
List<Task> taskToCreate = new List<Task>();
if(l.Key_Fields_Populated__c >= 3) {
for(String field : populatedFields) {
Task myTask = new Task();
myTask.WhatId = l.Id;
myTask.Subject = 'Verify the ' + field + ' field';
taskToCreate.add(myTask);
}
insert taskToCreate;
}
}
}