function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana 

Salesforce: Apex Contact sharing not working

I do have an issue with sharing a contact with partner users. The objective of the trigger is that, if a partner tries to create a contact in our system and if the email he uses to create the new contact is already in a contact, then share that existing contact with the partner rather than creating new contact.
Below is trigger that I used.
 
trigger checkContactExists on Contact(before insert) {
    String us = userinfo.getUserType();
    String emailValue = '';
    List<ContactShare> sharesToCreate = new List<ContactShare>();

    if(us == 'PowerPartner' || us == 'CSPLitePortal' || us == 'CustomerSuccess' || us == 'PowerCustomerSuccess') {
        for(Contact c: Trigger.New){
            emailValue = c.Email;           
            list<contact> contact1 = [SELECT Id FROM contact WHERE Email =:emailValue];
            if(contact1.size()>0) {
                for (Contact cont :contact1){
                    Contactshare cs = new ContactShare(ContactAccessLevel='Read', ContactId = cont.Id, userOrGroupId = UserInfo.getUserId());
                    sharesToCreate.add(cs);   
                }
            }
            c.addError('Duplicate contact found with the same email. The existing contact has been shared with you.');
        }

    }
     if (!sharesToCreate.isEmpty()) {
        //Database.SaveResult[] insertResults = Database.insert(sharesToCreate,true);
        insert sharesToCreate;
    }
}

The code doesn't shows any error and debug log says it created a row on contactsshare.
 
:57:25.0 (49336464)|STATEMENT_EXECUTE|[20]
16:57:25.0 (49337911)|STATEMENT_EXECUTE|[22]
16:57:25.0 (49400693)|DML_BEGIN|[22]|Op:Insert|Type:ContactShare|Rows:1
16:57:25.0 (49430731)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:57:25.0 (94617974)|DML_END|[22]

But when looking into the SOQL, I don't see any new rows of data being added. Any help appreciated.