How to use Connect API in Apex

Using Connect Api , we can access the chatter post and delete the posts using apex.

Scenario: How to post an announcement to a community

Using “ConnectApi.Announcements” class , we can create/update/read/delete the announcements.

208

Post an Announcement:

Syntax:

public static ConnectApi.Announcement postAnnouncement(String communityId, ConnectApi.AnnouncementInput announcement)

Parameters:
CommunityID — String
announcement — ConnectApi.AnnouncementInput object
Return type:
ConnectApi.AnnouncementInput

 

/**
*
* Posting an announcement to community using ConnectApi namespace.
*
* Includes convenience methods to:
*
* – Post Chatter @-mentions, rich text, and inline images with Apex code.
* – Take a feed item or comment body and return an input body that matches it.
* This is useful for when you want post an announcement and send emails to the users.
*
* This class works with API version 32.0 and later.
*
*
*/

 

public class postingAnnoucement{

/**
* Creating an Announcement using ConnectApi.Announcement
*
* @param – None
* @return – Announcement
*/

public ConnectApi.AnnouncementInput myAnnouncement(){
//Creating an announcement
ConnectApi.AnnouncementInput newAnnouncement = new       ConnectApi.AnnouncementInput();
newAnnouncement.expirationDate= datetime.now(); // Announcement Expiration Time, usually it will be expire on end of the day.
newAnnouncement.sendEmails=true;
newAnnouncement.ParentID=’232323sdsd’;
return newAnnouncement;

}

/**
* Posting an Announcement on community using ConnectApi.Announcement
*
* @param – None
* @return – Void
*/

public void postAnnouncement(){

ConnectApi.AnnouncementInput input=myAnnouncement();
ConnectApi.Announcement myfinalannouncemnt=                ConnectApi.Announcements.postAnnouncement(‘communityid’, input);

}

}

 

Next time will see you with some other interesting topic, Thank you.

Custom Permission

Hey Guys, Once again brothers are back .

Have you ever build any vf page with Buttons Edit / Clone/ Save /Cancel ??

If yes,  Have you ever tried to assign this page to all users ?

If yes, Have you ever worried about permissions they have on the respective object to do clone/ Edit / Save / Cancel ??

Ok. let me explain it clear.

Any user in salesforce can able to access the whole page or not able to access the page.

Have you ever tried to give the access to the part of the page , based on User permissions??

Yes!!! If you want to give access to pare of the page based on login user access , you are at right place.

Lets get into the functionality. How it works both theoretical and followed by practical way.

Step 1: Create a Permission Set with Some Label “MyCustomPermission

Step 2: Go to Permission Set (SetUp > Permission Set(Type Permission Set in Quick Find))

Step 3: Open any Existing Permission Set (For Ex: MyPermissionSet)and Click on Custom Permission

Step 4: There you can able to  add the Custom Permission that you have created at Step 1(In Our ex: MyCustomPermission)

Step 5: Add Users to the Permission Set by clicking on Manage User.

Ex : Lets add X, Y and Z to our Permission Set (in our Case its MyPermissionSet)

Step 5:  Select the Custom Permission  and Click on Save. (We almost Done( 🙂 )

Step 6: Now create on VF Page and show the data to the User.

Step 7: Before dive into the topic, Let you explain something related to Render

Step 7.1 : Rendered = “True” Shows the Piece of the page  to the Users

                    Rendered = “False” Hides the Piece of the Page to the Users

Step 8: Give Rendered = “{!(IF($Permission.MyCustomPermission,TRUE,TRUE))}”

(Ex  :  <apex:CommandButton value=”Save” action”{!save}” Rendered = “{!(IF($Permission.MyCustomPermission,TRUE,TRUE))}” / >)

Let me Explain Exactly what Step 8 does:

1 ) It will check the Login User Permission Set first

2 ) It will check each & every  Permission Set t o which that User belongs

3 ) Check  whether the Permission Set has the “MyCustomPermission” as the Custom Permission

4) If the any of the Permission Set of that User have Custom Permission “MyCustomPermission” then the block of code will shown to the user.

Other wise, it  doesn’t.

In our Cases lets take i am  logging as a  User “X”

That  User have  Permission Set  called, “MyPermissionSet

MyPermissionSet” have  Custom Permission Called “MyCustomPermission

So as per our example :

<apex:CommandButton value=”Save” action”{!save}” Rendered = “{!(IF($Permission.MyCustomPermission,TRUE,TRUE))}” / >

This button will visible the User X.

Conclusion: The above ComomandButton says , “Other than X,  Y and Z , no other user is allowed to see Save the button”

That’s all Folks. I hope this article will help you guys a lot.

Lets discuss some other new topic in the Next meeting.

Comments are always welcome 🙂

Profiles Clone

In my organization , there are some profiles with salesforce license . Now,  the same profiles need to assign it to community users. Salesforce doesn’t support changing the license type in profile. Once the profile is created with salesforce license , we can’t change the license type in profile.
Now, we can’t configure manually each and every permission in new profile for community users. It will take days to configure manually. Is there any automation to do ???

Yes. We can achieve this with ANT Tool(Meta data API)


Steps to Create new profiles with different license

    1) Go to “Sfdctoolkit.com” and generate package.xml with all components in the organization.
    2) Extract all the organization meta data.
    3) Go to extracted Meta data folders and open “Profiles” Folder.
    4) open the particular profile and change the user license to “Community”

From:
<userLicense> Salesforce</userLicense>

To:
 <userLicense>Customer Community</userLicense>
    5) Rename the file and deploy back in to the organization.

That’s it . You will get all the permissions like as other profile and license will be Community.

Apex Sharing

Hi Guys, Today we came up with very exciting topic . Do you know what ?? Lets guess . Its none other than Apex Sharing.

Have you ever shared your records through Apex Sharing. ??

If not you are at right place to learn.

Lets get into the content.

Lets take the scenario like , Your OWD settings are Private on Custom Object (Job__c). If we like to share this record with Community User we have to do it at Profile / Permission Set Level.

Lets try to make it and come back to the next point

Have you find is there any way to share this with View All / Modify All ??

Ultimately not.

This is where you need this blog post.

In order to share the record with any Community / Community Plus User We have to share that record with Apex. 🙂

Have to ever heard about ” __Share ” in Salesforce. If not we have to learn it first in order to go further.

For every Custom Object we are creating , Salesforce will create a share object along with your Custom object.

I can understand the sentence it bit tricky . Lets form it with Example.

Lets take you are trying to create New object named as “Expense__c”.

While you creating this object, salesforce will give you other object also called as “Expense__Share”.

We have to make use of this “Expense_Share” object in our Apex Sharing.

Lets get into the Coding part.

Lets take We have to share one of the Record from “Expense__c” with Community User .

Lets take the User Id is “005XXXXXXXXXXXX”

In order to share this record with the above user , make use of this code.

List<Expense_Share> shareList = new List<Expense_Share>();

Expense__c getAllExpense= [select id from Expense__c limit 100];

for(Expense__c eachExpense : getAllExpense){

Expense_Share sharingInst = new Expense_Share();

sharingInst.ParentId = eachExpense.id;

sharingInst.UserOrGroup = ‘005XXXXXXXXXXXX’;

shareList.add(sharingInst);

}

try{

insert shareList;

}catch(Exception e){}