Create Chatter Post Using Apex in Salesforce

Sample Code:

//Create a text post
FeedItem fi = new FeedItem();
fi.ParentId = '0010I00002AbcMm'; //Record Id
fi.Body = 'Chatter post from apex';
insert fi;

//Create a link text post
FeedItem fi = new FeedItem();
fi.ParentId = '0010I00002AbcMm'; //Record Id
fi.Body = 'Chatter post from apex';
fi.LinkUrl = 'http://www.salesforce.com';
insert fi;