You need to sign in to do that
Don't have an account?
Mark Thomas 49
Remove Lead Trigger error information from new validation rule error message.
I needed to add a validation rule to opportunities to prevent a specific type of contact from being added to the opportunity upon lead conversion.
The validation rule works as expected, but I need help cleaing up the error message. Currently it looks like this.
Validation error on Lead: LeadTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0060d00001y6mn8AAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ***SORRY. THE CONTACT YOU HAVE SELECTED IS INACTIVE IN JIVA. PLEASE SELECT A DIFFERENT CONTACT OR CREATE A NEW CONTACT***: [] Trigger.LeadTrigger: line 44, column 1
I really only need the text between the asterisks.
This is the lead conversion trigger:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
trigger LeadTrigger on Lead (after update, after insert)
{
//JWAHLERS/FF 12/22/2016
//Added if after update to existing logic:
if(trigger.isUpdate)
{
//List<Lead> convertedLeadsToProcess = new List<Lead>();
Map<Id, Id> oppIdToConIdMap = new Map<Id, Id>();
for (Lead l : (List<Lead>)Trigger.new){
if (((Lead)trigger.oldMap.get(l.Id)).isConverted == false && l.isConverted == true
&& l.ConvertedOpportunityId != null && l.ConvertedContactId != null)
{
//convertedLeadsToProcess.add(l);
oppIdToConIdMap.put(l.ConvertedOpportunityId, l.ConvertedContactId);
}
}
//JW@ff: query for opportunities in opptIdSet
if(!oppIdToConIdMap.isEmpty())
{
List<Opportunity> oppList = [Select Id From Opportunity Where Id IN: oppIdToConIdMap.keySet()];
List<Opportunity> updatedOppList = new List<Opportunity>();
for(Opportunity opp: oppList)
{
if(oppIdToConIdMap.containsKey(opp.Id))
{
opp.Contact__c = oppIdToConIdMap.get(opp.Id);
updatedOppList.add(opp);
}
}
if(!updatedOppList.isEmpty())
{
update updatedOppList;
}
}
}
//JWAHLERS/FF 12/22/2016
//Call handler for inserts EAGSC2-51
//DISABLED UNTIL DEVELOPMENT IS COMPLETE
if(trigger.isInsert)
{
LeadTriggerHandler.handleAfterInsert(trigger.newMap);
}
/*if (convertedLeadsToProcess.size() > 0){
Set<Id> OpptyIdSet = new Set<Id>();
Set<Id> ContactIdSet = new Set<Id>();
for(Lead l: convertedLeadsToProcess){
OpptyIdSet.add(l.ConvertedOpportunityId);
ContactIdSet.add(l.ConvertedContactId);
}
// Map<OpportunityId, Map<ContactId, OpportunityContactRole>>
Map<Id, Map<Id, OpportunityContactRole>> oCRMap = new Map<Id, Map<Id, OpportunityContactRole>>();
for(OpportunityContactRole oCR :[SELECT Id,IsPrimary,OpportunityId,ContactId FROM OpportunityContactRole WHERE OpportunityId IN :OpptyIdSet AND ContactId IN :ContactIdSet]){
if (oCR.IsPrimary == false){
if(!oCRMap.containsKey(oCR.OpportunityId))
{
oCRMap.put(oCR.OpportunityId, new Map<Id, OpportunityContactRole>());
}
oCRMap.get(oCR.OpportunityId).put(oCR.ContactId,oCR);
}
}
Map<Id, Id> oppIdToConIdMap = new Map<Id, Id>();
List<OpportunityContactRole> oCRToUpdate = new List<OpportunityContactRole>();
for(Lead l: convertedLeadsToProcess){
if (oCRMap.ContainsKey(l.ConvertedOpportunityId) && oCRMap.get(l.ConvertedOpportunityId).containsKey(l.ConvertedContactId)){
OpportunityContactRole oCR = oCRMap.get(l.ConvertedOpportunityId).get(l.ConvertedContactId);
oCR.IsPrimary = true;
oCRToUpdate.add(oCR);
//JW@ff generate map of opportunityId => contactId
oppIdToConIdMap.put(oCR.OpportunityId, oCR.ContactId);
}
}
if (oCRToUpdate.size() > 0){
update oCRToUpdate;
}
}*/
}
Or do I need to update the Lead Trigger Class?
The validation rule works as expected, but I need help cleaing up the error message. Currently it looks like this.
Validation error on Lead: LeadTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0060d00001y6mn8AAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ***SORRY. THE CONTACT YOU HAVE SELECTED IS INACTIVE IN JIVA. PLEASE SELECT A DIFFERENT CONTACT OR CREATE A NEW CONTACT***: [] Trigger.LeadTrigger: line 44, column 1
I really only need the text between the asterisks.
This is the lead conversion trigger:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
trigger LeadTrigger on Lead (after update, after insert)
{
//JWAHLERS/FF 12/22/2016
//Added if after update to existing logic:
if(trigger.isUpdate)
{
//List<Lead> convertedLeadsToProcess = new List<Lead>();
Map<Id, Id> oppIdToConIdMap = new Map<Id, Id>();
for (Lead l : (List<Lead>)Trigger.new){
if (((Lead)trigger.oldMap.get(l.Id)).isConverted == false && l.isConverted == true
&& l.ConvertedOpportunityId != null && l.ConvertedContactId != null)
{
//convertedLeadsToProcess.add(l);
oppIdToConIdMap.put(l.ConvertedOpportunityId, l.ConvertedContactId);
}
}
//JW@ff: query for opportunities in opptIdSet
if(!oppIdToConIdMap.isEmpty())
{
List<Opportunity> oppList = [Select Id From Opportunity Where Id IN: oppIdToConIdMap.keySet()];
List<Opportunity> updatedOppList = new List<Opportunity>();
for(Opportunity opp: oppList)
{
if(oppIdToConIdMap.containsKey(opp.Id))
{
opp.Contact__c = oppIdToConIdMap.get(opp.Id);
updatedOppList.add(opp);
}
}
if(!updatedOppList.isEmpty())
{
update updatedOppList;
}
}
}
//JWAHLERS/FF 12/22/2016
//Call handler for inserts EAGSC2-51
//DISABLED UNTIL DEVELOPMENT IS COMPLETE
if(trigger.isInsert)
{
LeadTriggerHandler.handleAfterInsert(trigger.newMap);
}
/*if (convertedLeadsToProcess.size() > 0){
Set<Id> OpptyIdSet = new Set<Id>();
Set<Id> ContactIdSet = new Set<Id>();
for(Lead l: convertedLeadsToProcess){
OpptyIdSet.add(l.ConvertedOpportunityId);
ContactIdSet.add(l.ConvertedContactId);
}
// Map<OpportunityId, Map<ContactId, OpportunityContactRole>>
Map<Id, Map<Id, OpportunityContactRole>> oCRMap = new Map<Id, Map<Id, OpportunityContactRole>>();
for(OpportunityContactRole oCR :[SELECT Id,IsPrimary,OpportunityId,ContactId FROM OpportunityContactRole WHERE OpportunityId IN :OpptyIdSet AND ContactId IN :ContactIdSet]){
if (oCR.IsPrimary == false){
if(!oCRMap.containsKey(oCR.OpportunityId))
{
oCRMap.put(oCR.OpportunityId, new Map<Id, OpportunityContactRole>());
}
oCRMap.get(oCR.OpportunityId).put(oCR.ContactId,oCR);
}
}
Map<Id, Id> oppIdToConIdMap = new Map<Id, Id>();
List<OpportunityContactRole> oCRToUpdate = new List<OpportunityContactRole>();
for(Lead l: convertedLeadsToProcess){
if (oCRMap.ContainsKey(l.ConvertedOpportunityId) && oCRMap.get(l.ConvertedOpportunityId).containsKey(l.ConvertedContactId)){
OpportunityContactRole oCR = oCRMap.get(l.ConvertedOpportunityId).get(l.ConvertedContactId);
oCR.IsPrimary = true;
oCRToUpdate.add(oCR);
//JW@ff generate map of opportunityId => contactId
oppIdToConIdMap.put(oCR.OpportunityId, oCR.ContactId);
}
}
if (oCRToUpdate.size() > 0){
update oCRToUpdate;
}
}*/
}
Or do I need to update the Lead Trigger Class?
You can not invoke validation rule from Apex. However, triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged.
You need to update your Lead Trigger and use the addError Method. Please see the documentation to learn more about using addError() in triggers
Here is a sample trigger that will show the error message in proper format
Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you