Thursday, 18 October 2012

Log In As another User in a click - A Detailed Approach

Please refer to my previous post on Quick way of accessing Grant Login Access, before reading this post.
This post provides a more detailed approach for the System Administrator to login either as Opportunity Owner or Opportunity Sales Team User.
For this, a visual force page was developed and embedded into the Opportunity Page Layout as a section.
Also this section will be available only to System Administrator Profile users and will not be available of other users.

Here is the VF page which lists out the Opportunity Owner and Opportunity Team Member with "Login" link

 


VF Page :

<apex:page standardController="Opportunity" extensions="LoginAs" sidebar="false" rendered="{!IF($Profile.Name='System Administrator',true,false)}">
<apex:pageMessage title="Opportunity has No Sales Team members" strength="1" severity="INFO" rendered="{!IF(OpptyTeamMem.size=0,true,false)}" />
<apex:form>
    <apex:pageblock title="Opportunity Owner Information">
    <apex:pageBlockTable value="{!Oppty}" var="opp">
        <apex:column width="25px" >
            <apex:commandLink action="{!proceedLogin}" >
                <apex:param name="usersel" value="{!opp.OwnerId}" assignTo="{!selectedUser}" />Login
            </apex:commandLink>        
        </apex:column>
        <apex:column value="{!opp.Owner.Name}" headerValue="Opportunity Owner"/>
        <apex:column value="{!opp.Owner.Profile.Name}" headerValue="User Profile"/>
        <apex:column value="{!opp.Owner.UserRole.Name}" headerValue="User Name"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Opportunity Sales Team Information">
    <apex:pageBlockTable value="{!OpptyTeamMem}" var="oppteam" title="Opportunity Team Members" rendered="{!IF(OpptyTeamMem.size>0,true,false)}">
        <apex:column width="25px">          
            <apex:commandLink action="{!proceedLogin}">
                <apex:param name="userselc" value="{!oppteam.UserId}" assignTo="{!selectedUser}" />Login
            </apex:commandLink>        
        </apex:column>
        <apex:column value="{!oppteam.User.Name}" headerValue="Opportunity Team Member(s)"/>
        <apex:column value="{!oppteam.TeamMemberRole}"/>
        <apex:column value="{!oppteam.OpportunityAccessLevel}"/>
    </apex:pageBlockTable>
    </apex:pageblock>
    </apex:form>
</apex:page>

Controller :

public class LoginAs 
{
    public Opportunity oppty {get;set;}
    public String selectedUser {get;set;}
    public List<OpportunityTeamMember> OpptyTeamMem {get;set;}
    public LoginAs(ApexPages.StandardController controller)
    {
        oppty = [Select Id,Name,ownerId,Owner.Name,Owner.Profile.Name,Owner.UserRole.Name from Opportunity where Id=:ApexPages.currentPage().getParameters().get('Id')];       
        OpptyTeamMem = [Select Id,User.Name,OpportunityAccessLevel,TeamMemberRole from OpportunityTeamMember where OpportunityId=:oppty.Id];
    }
    public PageReference proceedLogin()
    {
        String str = '/servlet/servlet.su?oid='+UserInfo.getorganizationId()+'&suorgadminid='+selectedUser+'&retURL=%2F'+Oppty.Id+'&targetURL=%2F'+Oppty.Id;
        PageReference pg = new PageReference(str);
        pg.setRedirect(true);
        return pg;
    }
}


1 comment:

  1. It's works beautifully.

    Thanks for sharing.

    Regards,
    Rajesh.

    ReplyDelete