anusha(salesforce developer)

Tuesday 14 June 2016

Execute SOQL On Chatter in Salesforce

Today I am providing a piece of code which will introduce a new way to run your simple SOQL on Chatter it self without opening developer console(that might take some time while loading).

To use this tool you just need to keep this trigger code in your org and then you can execute simple SOQL queries as follows.

Step1 : Post a SOQL in chatter on any object in the org.
Step2 : Refresh the screen you will get your result as below. 
 
trigger ChatterAsConsole on FeedItem (after insert)
{  
    if(UtilClass.noOfExecution == 0)
    {
        String queryString = ''; 
        FeedItem post ;
        for (FeedItem f: trigger.new)
        { 
            Try
            {                
                //Adding a Text post
                post = new FeedItem();
                post.ParentId = f.ParentId; //eg. Opportunity id, custom objectid
                queryString = f.Body ;
                String idString = '';
               
                for(Sobject s : Database.query(queryString))
                {
                        idString += s.id+', ';
                }       
                if(idString.endsWith(', '))
                    idString = idString.substring(0,idString.length()-2);
                post.Body = idString ;
                UtilClass.noOfExecution ++ ;
                insert post;
            }catch(Exception e)
            {
                post = new FeedItem(ParentId = f.ParentId , Body  = e.getMessage() );
                insert post;
            }
        }
    }
}
Helper Class : 

public Class UtilClass
{
 public static Integer noOfExecution = 0;
}

This code has its own limitation but you can customize it as per your need

No comments:

Post a Comment