I have a trigger written for portal users (3 user type as described on trigger below )to check if the contact they are trying to create is s duplicate one based on email entered. If it's a duplicate one, don't allow to create the contact, but instead SHARE THE EXISTING CONTACT WITH THAT EMAIL WITH THIS USER. Below is the trigger I wrote.
trigger DuplicateContact on Contact (before insert) {
system.debug('Inside the contactshare trigger');
if(QueryHelper.LeadConvert == true) {
Set<String> emailSet = new Set<String>();
String us = userinfo.getUserType();
List<ContactShare> sharesToCreate = new List<ContactShare>();
Map<String, Id> mapEmalSet = new Map<String, Id>();
system.debug('Customer type:' + us);
if(us == 'PowerPartner' || us == 'CSPLitePortal' || us == 'CustomerSuccess' || us == 'PowerCustomerSuccess')
{
List<ContactShare> ContactShareLs = new List<ContactShare>();
for(Contact c: Trigger.New)
{
system.debug('Contact trigger : ' + c.Email);
system.debug('Contact trigger Query: SELECT Id, Email FROM contact WHERE Email =' + c.Email);
if(c.Email != null) {
for(contact con : [SELECT Id, Email FROM contact WHERE Email =: c.Email]) {
if(con.Id != null)
{
ContactShare cs = new ContactShare();
cs.ContactAccessLevel = 'Edit';
cs.ContactId = con.Id;
cs.RowCause = 'Manual';
cs.UserOrGroupId = UserInfo.getUserId();
ContactShareLs.add(cs);
}
}
insert ContactShareLs;
for(ContactShare csid: ContactShareLs) {
System.debug('ContactShareId: ' + csid.Id);
}
c.addError('Duplicate contact found with the same email. The existing contact has been shared with you.');
}
}
}
QueryHelper.LeadConvert = false;
}
}
But when I try to create the contact with an existing email address, on the debug log it shows as contact created, but don't see that new contact on the portal users contact list or as any entry on ContactShare object. I checked the ContactShare object and don't see any additional rows being added over there. Below is the log message.
15:37:12.0 (15589957)|DML_BEGIN|[35]|Op:Insert|Type:ContactShare|Rows:1
15:37:12.0 (15619989)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:37:12.0 (52163289)|DML_END|[35]
15:37:12.0 (52250811)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:37:12.0 (52458636)|HEAP_ALLOCATE|[36]|Bytes:5
15:37:12.0 (52491994)|HEAP_ALLOCATE|[36]|Bytes:24
15:37:12.0 (52506717)|VARIABLE_SCOPE_BEGIN|[36]|csid|ContactShare|true|false
15:37:12.0 (52550992)|VARIABLE_ASSIGNMENT|[36]|csid|{"ContactAccessLevel":"Edit","ContactId":"003V000000TC9scIAD","RowCause":"Manual","UserOrGroupId":"005V0000003yep2IAA","Id":"03sV000001kUf9mIAC"}|0x7695976a
15:37:12.0 (52560933)|STATEMENT_EXECUTE|[36]
15:37:12.0 (52563133)|STATEMENT_EXECUTE|[37]
15:37:12.0 (52567611)|HEAP_ALLOCATE|[37]|Bytes:16
15:37:12.0 (52651880)|HEAP_ALLOCATE|[37]|Bytes:18
15:37:12.0 (52675111)|HEAP_ALLOCATE|[37]|Bytes:34
15:37:12.0 (52695498)|USER_DEBUG|[37]|DEBUG|ContactShareId: 03sV000001kUf9mIAC
15:37:12.0 (52714086)|HEAP_ALLOCATE|[36]|Bytes:5
15:37:12.0 (52730229)|VARIABLE_ASSIGNMENT|[36]|csid|null|
15:37:12.0 (52736308)|STATEMENT_EXECUTE|[39]
I searched the contactshare object with the new ID from log and don't see it. any help much appreciated.