RecordshareToUser using Apex and vfpage in salesforce.com
For This Need to create one Job Object
vfpage
----------------------------------
<apex:page standardController="Job__c" extensions="RecordshareToUserApexCon">
<apex:form >
<apex:pageBlock >
<apex:selectList size="1" value="{!jobst }" multiselect="false">
<apex:selectOptions value="{!Job}"> </apex:selectOptions>
<apex:actionSupport event="onchange" reRender="table,opt1" />
</apex:selectList>
<apex:selectList size="1" value="{!selectedVal}" multiselect="false">
<apex:actionSupport event="onchange" reRender="table,opt1" />
<apex:selectOptions value="{!Value}">
</apex:selectOptions>
</apex:selectList>
<apex:outputText id="opt12" value="{!selectedVal}"></apex:outputText>
<apex:commandLink action="{!manualShareRead}" value="Share" id="opt1">
</apex:commandLink>
</apex:pageBlock>
</apex:form>
</apex:page>
--------------------------------------------------------------------------
apex page
-----------------------------------------------------------------------------
public class RecordshareToUserApexCon
{
List<user> con1= new List<user>();
public job__c j {get;set;}
Public List<string> usertselect{get;set;}
public string jobst { get;set;}
public string val{ get;set;}
public user gp {get;set;}
public boolean nojb{get;set;}
public String selectedVal{get;set;}
public List<job__C> joblist = new List<job__C>();
public RecordshareToUserApexCon () {
}
public RecordshareToUserApexCon (ApexPages.StandardController controller) {
}
public List<SelectOption> getJob(){
List<SelectOption> option = new List<SelectOption>();
for(job__c j: [Select id,name From job__c]){
option.add(new SelectOption(j.name,j.name));
system.debug('option value is '+option);
}
return option;
}
public List<SelectOption> getValue(){
List<SelectOption> option = new List<SelectOption>();
for(user gp: [Select id,name From user ])
{
option.add(new SelectOption(gp.name,gp.name));
system.debug('option value is '+option);
}
return option;
}
public PageReference manualShareRead() {
joblist = [select id from job__C where name=:jobst];
con1 = [select id from user where name=:selectedVal];
system.debug('PGList value is'+con1);
system.debug('PGList value is'+joblist);
Job__Share jobShr = new Job__Share();
jobShr.ParentId = joblist[0].id;
jobShr.UserOrGroupId = con1[0].id;
jobShr.AccessLevel = 'Read';
jobShr.RowCause = Schema.Job__Share.RowCause.Manual;
Database.SaveResult sr = Database.insert(jobShr,false);
system.debug('sr value is'+sr);
if(sr.isSuccess()){
system.debug('sr.isSuccess() value:'+sr.isSuccess());
system.debug('if sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/CustomTeam?id='+joblist[0].id);
return nextpage;
}
else {
system.debug('inner else sr.isSuccess() block');
Database.Error err = sr.getErrors()[0];
if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION &&
err.getMessage().contains('AccessLevel')){
system.debug(' iner if sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/CustomTeam?id='+joblist[0].id);
return nextpage;
}
else{
system.debug('outer else sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/RecordShareFailure?id='+joblist[0].id);
return nextpage;
}
}
}
}
For This Need to create one Job Object
vfpage
----------------------------------
<apex:page standardController="Job__c" extensions="RecordshareToUserApexCon">
<apex:form >
<apex:pageBlock >
<apex:selectList size="1" value="{!jobst }" multiselect="false">
<apex:selectOptions value="{!Job}"> </apex:selectOptions>
<apex:actionSupport event="onchange" reRender="table,opt1" />
</apex:selectList>
<apex:selectList size="1" value="{!selectedVal}" multiselect="false">
<apex:actionSupport event="onchange" reRender="table,opt1" />
<apex:selectOptions value="{!Value}">
</apex:selectOptions>
</apex:selectList>
<apex:outputText id="opt12" value="{!selectedVal}"></apex:outputText>
<apex:commandLink action="{!manualShareRead}" value="Share" id="opt1">
</apex:commandLink>
</apex:pageBlock>
</apex:form>
</apex:page>
--------------------------------------------------------------------------
apex page
-----------------------------------------------------------------------------
public class RecordshareToUserApexCon
{
List<user> con1= new List<user>();
public job__c j {get;set;}
Public List<string> usertselect{get;set;}
public string jobst { get;set;}
public string val{ get;set;}
public user gp {get;set;}
public boolean nojb{get;set;}
public String selectedVal{get;set;}
public List<job__C> joblist = new List<job__C>();
public RecordshareToUserApexCon () {
}
public RecordshareToUserApexCon (ApexPages.StandardController controller) {
}
public List<SelectOption> getJob(){
List<SelectOption> option = new List<SelectOption>();
for(job__c j: [Select id,name From job__c]){
option.add(new SelectOption(j.name,j.name));
system.debug('option value is '+option);
}
return option;
}
public List<SelectOption> getValue(){
List<SelectOption> option = new List<SelectOption>();
for(user gp: [Select id,name From user ])
{
option.add(new SelectOption(gp.name,gp.name));
system.debug('option value is '+option);
}
return option;
}
public PageReference manualShareRead() {
joblist = [select id from job__C where name=:jobst];
con1 = [select id from user where name=:selectedVal];
system.debug('PGList value is'+con1);
system.debug('PGList value is'+joblist);
Job__Share jobShr = new Job__Share();
jobShr.ParentId = joblist[0].id;
jobShr.UserOrGroupId = con1[0].id;
jobShr.AccessLevel = 'Read';
jobShr.RowCause = Schema.Job__Share.RowCause.Manual;
Database.SaveResult sr = Database.insert(jobShr,false);
system.debug('sr value is'+sr);
if(sr.isSuccess()){
system.debug('sr.isSuccess() value:'+sr.isSuccess());
system.debug('if sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/CustomTeam?id='+joblist[0].id);
return nextpage;
}
else {
system.debug('inner else sr.isSuccess() block');
Database.Error err = sr.getErrors()[0];
if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION &&
err.getMessage().contains('AccessLevel')){
system.debug(' iner if sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/CustomTeam?id='+joblist[0].id);
return nextpage;
}
else{
system.debug('outer else sr.isSuccess() block');
PageReference nextpage = new PageReference('/apex/RecordShareFailure?id='+joblist[0].id);
return nextpage;
}
}
}
}