0

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.

1 Answer 1

0

You are using the SOQL inside the for loop. Please move outside of it.

First get all the email in a set

Set<String> emailSet = new Set<String>; 
for(Contact c: Trigger.New)
{
    emailSet.put(c.Email);
}

Then query the for existing record and store in a set

Set<String> extEmailSet = new Set<String>;
for(contact con : [SELECT Id FROM contact WHERE Email IN :emailSet])
{
    extEmailSet.put(con.Email)
}

Now check the user type and ensure the set contains the existing email set then add error else create a share record.

if(us == 'PowerPartner' || us == 'CSPLitePortal' || us == 'CustomerSuccess' || us == 'PowerCustomerSuccess') 
    {
        for(Contact c: Trigger.New)
        {
            //Check Duplicate email 
            if(!extEmailSet.contains(c.Email)) 
            {                
                Contactshare cs = new ContactShare(ContactAccessLevel='Read', ContactId = c.Id, userOrGroupId = UserInfo.getUserId());
                sharesToCreate.add(cs);   
            }
            else
            {
                c.addError('Duplicate contact found with the same email. The existing contact has been shared with you.');
            }
        }

    }

then insert the share record.

if (!sharesToCreate.isEmpty()) {
        //Database.SaveResult[] insertResults = Database.insert(sharesToCreate,true);
        insert sharesToCreate;
    }
5
  • I tried using that code making minor changes, but doens't seems to be working. The logs says "Op:Insert|Type:ContactShare|Rows:1", but don't see any new rows being added.
    – Gops
    Commented Jun 15, 2017 at 22:58
  • I did some debug and see that after the code "insert cs; system.debug('Last Inserted Id: ' + cs.id); ". I see the debug log with Last inserted Id having a value, but when I use that that Id to see the record(appending on URL), it says insufficuent previlage even though I am the admin.
    – Gops
    Commented Jun 15, 2017 at 23:40
  • U cant see the share record in vf page. Use developer console and query there you can see the Id for the ContactShare
    – bharath
    Commented Jun 16, 2017 at 5:44
  • Hi Bharath, I did the query and still it shows 0 rows. Any idea?
    – Gops
    Commented Jun 19, 2017 at 18:24
  • select id,ContactId,ContactAccessLevel,RowCause,UserOrGroupId from Contactshare WHERE ID = 'YOUR_ID' -> Try this in developer console query with your ID
    – bharath
    Commented Jun 20, 2017 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .