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
SId.New devSId.New dev 

ContactShare using apex

Hi all.Please let me know if any one worked on this.

 

OWD is private for account and contact. so no user connot see records now, now i want to show contact records to users when they entered existing email. i prevoiusly worked on lead which is woking. but help me now to do the same on contacts.

 

public class contacterror {

    public PageReference AssignPermissionforcontact1() {
        return null;
    }

public list<user> u{get;set;}
id contactid;
public contacterror ()
{
 contactid=ApexPages.CurrentPage().getParameters().get('id');
}

Public pagereference AssignPermissionforcontact(){

system.debug('GGGG'+UserInfo.getUserId());

ContactShare cs = new ContactShare();
     //   cs.ContactAccessLevel = 'Edit';     ( here getting errror as "=")
     //   cs.ContactId = contactId;
      //  cs.UserOrGroupId =  UserInfo.getUserId();

//Database.SaveResult contactInsertResults = Database.insert(cs, false);


pagereference p=new pagereference('/'+contactid);

return p;

}
}


VF page:------------------


vf page:------

<apex:page controller="Contacterrorclass" action="{!AssignPermissionforcontact}">
<apex:form >

</apex:form>
</apex:page>
-----------------------------------------------------------
Trigger:-(checks the dups, and gives the link to open the existing record)


trigger TriggerOncontact on contact (before insert) {
 
    Map<String, contact> mapcontact1 = new Map<String, contact>();
    Map<String, contact> mapcontact2 = new Map<String, contact>();

    for (contact contact : System.Trigger.new) {
          mapcontact1.put(contact.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), contact);
          mapcontact2.put(contact.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), contact);
    }
     list<contact>contact1=[SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM contact WHERE concatinate_name_and_email__c  IN :mapcontact1.KeySet()];
     list<contact> contact2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM contact WHERE concatinate_name_and_phone__c IN :mapcontact2.KeySet()];
    for (contact con4 : System.Trigger.new) {
      if(con4.Any_way_save_record__c!=true)
      {
  
    if(contact1.size()>0)
    {
     for (contact contact :contact1){
     contact newcontact = mapcontact1.get(contact.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
      
         
     newcontact.addError('Les contacts de cette personne existent dans Salesforce. Voir le contact :<a style=color:GREEN href=/apex/leaderror?id='+contact.ID+'>'+  contact.name + '</a>');
    }}else if(contact2.size()>0){
    for (contact Contact :contact2 ) {
      contact newcontact1 = mapcontact2.get(contact.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      newcontact1.addError('contact already exists see this:<a style=color:GREEN href=/apex/leaderror?id='+contact.ID+'>'+  contact.name + '</a>');

  
    }
    
   }
}
}
}

 I want to show the contact when he clicks on link , (if it is not possible i need to show his related account).

KUDOS in Advance

 

Thanks

Sid.

sfdcfoxsfdcfox

It's one of those fields you can't assign directly. Instead, use the SObject constructor parameters:

 

Contactshare cs = new ContactShare( ContactAccessLevel='Edit', ContactId=contactId, userOrGroupId=UserInfo.getUserId()

 

 

S!d.DevS!d.Dev

Hi sfdc fox.Thanks a lot for yoy reply but i am unable to mark it as solution because I'm not yet got it.

 

 it is showing as in suffiecient previlages(login with standard profile user). even i have given this vf page and class acess to all users ,but i think the problem is my contactshare class vf page is not calling.

 account owd is private and contact owd is also private in my org.

Please see the same . let me know the same to complete.

 

<apex:page controller="Contacterrorclass"  action="{!AssignPermissionforcontacts}">
<apex:form >

</apex:form>
</apex:page>
-------------------------------------------------------------->
public class Contacterrorclass {
public PageReference AssignPermissionforcontact() {
        return null;
}
public list<user> u{get;set;}
id contactid;
public Contacterrorclass()
{
 contactid=ApexPages.CurrentPage().getParameters().get('id');
 }
   public PageReference AssignPermissionforcontacts() {

Contactshare cs = new ContactShare( ContactAccessLevel='Edit', ContactId=contactId, userOrGroupId=UserInfo.getUserId());
 
pagereference p1=new pagereference('/'+contactid);
return p1;
}
}

 In trigger also i have given this page acess,let me know if any one got the solution.

 

Thanks in advance

S!ddu.