Friday, September 11, 2015

@chatter Mention user

Hello everyone after the version 31 below methods are no longer available 

ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.News, userId);


So how we can do @mention now . see sample code below how to do that .


// Method that takes the list sobject as  parameter and post 


    public static void postOnChatter(List<Sobject> Items){

        List<ConnectApi.BatchInput> batchInputs = new                      List<ConnectApi.BatchInput>();
Integer MAXSIZE = 10 ;// Select Your size
        for (Sobject item : Items) {
            //  initialize the FeedItemInput
            ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
            input.subjectId = item.Id;
            
            // initialize the body of the chatter post
            ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
            body.messageSegments = new List<ConnectApi.MessageSegmentInput>();
            
            // add the mention to the post
            ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
            mentionSegment.id = item.OwnerId;
            body.messageSegments.add(mentionSegment);
            
            // add the text containing the Product Name and the tracking information
            ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
            textSegment.text = 'Sample Text';
            body.messageSegments.add(textSegment);
            
            input.body = body;
            ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input);
            
            // add the FeedItemInput instances to a list
            batchInputs.add(batchInput);
            
            // postFeedElementBatch() method can process maximum of 500 feeds at a time
            // if the feeds are 500, post them and re-instantiate the list
            if(batchInputs.size() == MAXSIZE){
                ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
                batchInputs = new List<ConnectApi.BatchInput>();
            }
        }
        // post the feeds  in the list
        if(batchInputs.size() > 0)
            ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
    }

Wednesday, March 4, 2015

Keep alive your debug logs without manual refresh in Salesforce

Hello Everyone,

I felt this urge to write such thing because I have to monitor log in salesforce for performance testing being a lazy person J I don’t want to do it by keeping my eye on developer console. So I found this trick it might be helpful to you as well

Here goes the code !!!

Apex Class

/**
* This class will create debug log instances for users 
**/ 
public class LogResetController {
    public List<String> userIds {get;set;} //will hold the user ids 
    public List<String> userNames {get;set;} // will hold the user names used in soql
    Public Integer counter {get;set;} {counter = 0;}
    public Boolean isFstLoad {get;set;} { isFstLoad = true;}
    /**
* controller of the class initialize your user name list here  
**/
    public LogResetController(){    
        //initialise list of users here and call initpage();
        userNames = new List<String>();        
       //addition of users for those you want log to be enabled 
       userNames.add('test@test.com'); 
        initPage();
    }
    public void initPage(){
        // getting the users 
        List<User> users = [SELECT ID FROM USER WHERE USERNAME IN:userNames AND isActive = true];
        // creating list of ids this can be omitted i did it for ease of use on the page 
        userIds = new List<String>();
        for(User u : users){
            userIds.add(u.Id) ;
        }
    }
    public void dummyAction(){
        // this will be called from vf page action poller to re-initiate the debug log 
        counter++;
        isFstLoad = false;
    }
    public void ResetAction(){
        isFstLoad = true;
    }
}

Visualforce Page

<apex:page sidebar="false" showHeader="false" controller="LogResetController">
    <style>
    #loadingMessage {
   width: 100%;
   height: 100%;
   z-index: 1000;
   top: 50%;
   left:50%;
   position: absolute;
   background-repeat: no-repeat !important;

    </style>
    <script>
    var openWindows= new Array();
    </script>
    <apex:form id="mainform">
        <apex:actionFunction name="resetPage" action="{!ResetAction}" reRender="mainform" />
        <apex:repeat value="{!userIds}" var="u" rendered="{!isFstLoad}">
            <script>
                var user = '{!u}' ;
                var url = '/setup/ui/listApexTraces.apexp?user_id='+ '{!u}'+ '&user_logging=true';               
                var win = window.open(url);
                openWindows.push(win);
                console.log("************* Pushing ***************");
            </script>
        </apex:repeat>
        <apex:actionPoller action="{!dummyAction}" interval="20" reRender="mainform"> 
            <apex:outputPanel id="counterPanel">
            <div id="loadingMessage">
                    <img src="/img/loading32.gif" title="/img/loading32.gif"/>
                    <br/>
                    <div style="font-size:20px;margin-left:-60px;color:red">
                        Counter Value is : {!counter}
                    </div>
                </div>
            </apex:outputPanel>
        </apex:actionPoller>
        <apex:outputPanel id="refreshPanel">
            <apex:outputPanel rendered="{!NOT(isFstLoad)}">
                <apex:outputLabel > Refreshing .....</apex:outputLabel>
                <script>
                try{
                    for(var index = 0 ; index < openWindows.length ; index++){
                        console.log(" **************** CLOSING *********************** " + index );
                        openWindows[index].close();                           
                    }
                    openWindows= new Array();
                    resetPage();
                }
                catch(err){
                    console.log(err);    
                }
                </script>
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>


Test Class




@istest
public class LogResetControllerTest {
    private static testMethod void unitTest(){
        User U = [Select Id,UserName From User WHERE Id =: userInfo.getUserId()];
        Test.startTest();
        LogResetController con = new LogResetController();
        con.userNames = new List<String>();
        con.userNames.add(U.UserName);
        con.initPage();
        con.dummyAction();
        Test.stopTest();
        System.assertEquals(con.userIds.size(), 1);
    }

}

Tuesday, January 6, 2015

Intercommunication between parent and child window ( IFRAME and window.open)

I will show you how you can communicate between browser windows. We already know we can access it via parent. method but there is another way of doing it . 
parent.html
<html>
This is parent page 
<iframe src="childIframe.html" id="frame1" />
<script>
window.open("child.html");
function callParent(){
alert(" I am at parent");
// accessing child element of iframe make sure iframe loaded completely before you call this                          // will hide the div form the lunched window 
$($("#frame1").contents().find("#sampleDiv")).hide();  
}
</script>

</html>

child.html
<html>
This is child 
<script>
window.opener.callParent(); // accessing parent 
</script>
</html>

childIframe.html

<html>
This is a Iframe
<div id = "sampleDiv"> Hello </div>
</html>

Monday, November 10, 2014

bootstrap carousel control

Recently I was working on bootstrap carousel . I was looking for something like 

  • I don't want cycle on the images as well as no back action on first page and no forward button on the last page
  • And don't want  to navigate to the next until i clicked the icon to navigate, As you know bootstrap create a  vertical section for scrolling. 

I did the following to achieve my requirement  


<div id="carousel-example-generic"  class="carousel slide" data-ride="carousel" data-interval="false">  <!-- data-interval="false"-->                     
<!-- Wrapper for slides -->
<div class="carousel-inner" >
<div class="item active">
</div>
<div class="item">
</div>
    </div>
<!-- Controls -->
<!-- style="width:1%" did the trick and enforce user to click on the icon to navigate -->
<a style="width:1%;"  class="left carousel-control" href="#carousel-example-generic1" role="button" data-slide="prev">
<!-- put any custom image you want -->
<img  height="47" width="47" />
</a>   
<a style="width:1%;" class="right carousel-control" href="#carousel-example-generic1" role="button" data-slide="next">
   <!-- put any custom image you want -->
<img   height="47" width="47" />
</a>
</div>
<!-- run the below script on load of your page and it will do the hiding and showing the navigation icon on the first and last page --> 
<script>
var checkitem = function() {
        var $this;
        $this = $("#carousel-example-generic");
        if ($("#carousel-example-generic .carousel-inner .item:first").hasClass("active")) {
            $this.children(".left").hide();
            $this.children(".right").show();
        } else if ($("#carousel-example-generic .carousel-inner .item:last").hasClass("active")) {
            $this.children(".right").hide();
            $this.children(".left").show();
        } else {
            $this.children(".carousel-control").show();
        }
    };
</script>

Happy coding :)

Friday, October 31, 2014

IE issue with window.open/ hyperlink - not opening a new tab

In IE window.open open a new pop up window but in other browser it open a new tab especially in chrome, mozilla and safari.

There is a way to get a similar behaviour in all Browser by CSS3. 

a{
target-name:new;
target-new:tab;
}

or 

<a target ="_blank" style="target-new: tab;" onclick="window.open(url);" > 
   link 
</a>

The above solution will work every where _blank will for other and target for IE .


Click here to see details    

Tuesday, July 29, 2014

Rendering problem in visualforce pages

Some time we face problem that some section is not getting re-rendered even if we are re-rendering the section from some action from my experience I have learned most of the time it stop working on the below reasons

1) We are trying to render a pageBlock or pageBlockSection 
2) We have some rendered condition on the section

Solution for point 1 is put an outputPanel and re-render it

<apex:outputPanel id="panel1">
 <apex:pageBlock> ....
</apexoutputPanel>
<apex:actionFunction reRender="panel1">

Solution for point 2 is put an outputPanel and re-render it

<apex:outputPanel id="panel1">
<apex:outputPanel rendered ={condition}>
 <apex:pageBlock> ....
</apexoutputPanel>
</apexoutputPanel>
<apex:actionFunction reRender="panel1">

The reason behind it if we have a rendered condition and the condition became false there is no html outcome hence VF page fails to re-render on second attempt as there is no DOM element for that 

Wednesday, July 16, 2014

Auto Complete in Salesforce

Hi,

In past I have worked on many auto complete for picklist, mainly for look up attributes there are many JavaScript based solution which will give you the solution but there are problems with like if you want to merge two fields like Account Number and Account Name as an option it will be a hard job.

So I have modified a solution I have found on git hub this VF component is very cool and easy to use. Though  you can attach auto complete for the object from out of the box. Please find the attached URL for this 

https://help.salesforce.com/HTViewHelpDoc?id=search_enhanced_lookup_enable_autocomplete.htm&language=en_US

Form my knowledge it is applicable for custom objects as well but the link says it is limited to certain object .


But My problem was some what different I have to show other unique attribute along with the Name or any other configurable field  





It will look like above image as soon you select it will fill the target destination field which need to be hidden I have clubbed BillingCity and Name for example 

Total code can be found at 

https://drive.google.com/file/d/0B5TneolnEkocalh6ZHJrdkg2RXc/edit?usp=sharing

I hope this will help many developers like me who are struggling with type ahead :)  

Thanks,
Avijit 

Tips on passing Salesforce AI Associate Certification

  🌟 Motivation to Pursue the Salesforce AI Associate Certification 🌟 The world of technology is in a state of perpetual evolution, and on...