Wednesday 10 August 2016

current user

Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
system.debug('ProfileName'+profileName);
public googlePageController(){         PageReference pg = null;                      String usr = AF_DealerCRM_Utility.getUserInfo();         System.debug (' ProfileName: ' + usr); } public string getUserInfo(){                   String usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()].Profile.Name;       return usrProfileName ;    } (OR) public googlePageController(){         PageReference pg = null;                     User usr = AF_DealerCRM_Utility.getUserInfo();         System.debug (' ProfileName: ' + usr.Profile.Name); } public User getUserInfo(){                User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];       return usrProfileName ;    } please select this as solution if you got your answer.

Sunday 7 August 2016


udayar_jayam

custom lead convert button created on custom object

Hi All,
      we created a custom convert button on custom object,which is to convert  
Vinit_KumarVinit_Kumar
So whats the issue in that.

udayar_jayamudayar_jayam
We created a custom convert button on custom object, which is to convert a lead into account, opportunity and contact. Can anyone tell how to do this convertion either by code or anyother way

Vinit_KumarVinit_Kumar
Udyarar,

Try below :-

01public class LeadConversion {
02
03    private final Lead Lead;
04     
05    // The extension constructor initializes the private member
06    // variable acct by using the getRecord method from the standard
07    // controller.
08    public LeadConversion(ApexPages.StandardController stdController) {
09        this.Lead = (Lead)stdController.getRecord();
10    }
11     
12    public void convertLead(){
13     
14    if (lead.isConverted =false//to prevent recursion
15      {
16       
17        Database.LeadConvert lc = new Database.LeadConvert();
18        lc.setLeadId(lead.Id);
19             
20        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=trueLIMIT 1];
21        lc.setConvertedStatus(convertStatus.MasterLabel);
22         
23          
24        Database.LeadConvertResult lcr = Database.convertLead(lc);
25        System.assert(lcr.isSuccess());
26     
27        }
28    }
29     
30    }


udayar_jayamudayar_jayam
Hi vinit kumar,
         Thanks for the code,I have saved this code in apex class then what i have to do am new to apex coding plz tell me immediately its very urgent. waiting for ur reply.Reply me as soon as possible.

Regards
udaya

Vinit_KumarVinit_Kumar
You need to create a VF page and use this class as an extension to it.Then,you can create a Custom button and attach the VF page to it.

udayar_jayamudayar_jayam
Here this VF page code is correct or not,if wrong means tell me the correct one
<apex:page Controller="LeadConversion" cache="true" action="{!autorun}" extensions="leadController" >
<br/><br/>
<apex:messages style="color:red; font-weight:bold; text-align:center;"/>
<apex:form >
<center>
<apex:commandLink value="Redirect to Lead" style="color:blue; font-weight:bold;" action="{!RedirecttoLead}"/>
</center>
</apex:form>
</apex:page>

thanks,
regards,
Udaya

Vinit_KumarVinit_Kumar
Change this line from

<apex:page Controller="LeadConversion" cache="true" action="{!autorun}" extensions="leadController" >

to

<apex:page StandardController="Lead" cache="true" action="{!convertLead}" extensions="LeadConversion" >



udayar_jayamudayar_jayam
geting this error

unknown Constructor'Lead/Conversion.LeadConversion() Create a apex method 'LeadConversion.LeadConversion()'

Please check this once.
thanks,
regards.
udaya.

SF DEVSF DEV
Hi Vinit,

Can we control the custom lead conversion class(LeadConversion), not to create account. I just wanted to create only contact when converting the lead. Is it controlling in the code anyway.

Thanks.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I will need to create my own "convert lead" button because I need to ensure that certain fields are updated when the button is clicked.
However, I was wondering if there is a quick way of doing this, because usually when converting a lead, we have the option of creating an opportunity, contact and account. Can I maybe create a button that, when clicked, populates the fields that I need to populate and then reroutes to the standard lead conversion process?
Tia.
shareimprove this question

2 Answers


There is a Database.leadConvert method available documented in LeadConvert Class.
So yes, you could create e.g. a JavaScript button that calls your own WebService method that does whatever population you need and then calls Database.leadConvert.
shareimprove this answer

Of course you can. But I doubt if this is a quick way as you are looking for. This is what I have.
Create a custom button of Content source type URL under Lead object.
Set the URL as /apex/LeadConvertView?id={!Lead.Id} (LeadConvertView is just a blank page with Lead StandardController and controller extension to redirect user into real conversion page).
Have below code in LeadConvertView .
<apex:page standardController="Lead" action="{!convertLead}" extensions="ControllerLeadConvertView">
</apex:page>
And have the below code in the ControllerLeadConvirtView
public class ControllerLeadConvertView {
    public Id leadId;

    public ControllerLeadConvertView(ApexPages.StandardController stdController){
        leadId = ApexPages.CurrentPage().getParameters().get('id');
        //You can retrieve the Lead object and do whatever the populating here
       //populateFields(); if you need 
    }

    public void populateFields(){
        //You can do your stuffs here may be by taking as an action of UI events
    }

    //This will convert the Lead and redirect the user into newly created Account's detail page
    public PageReference convertLead(){
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];            
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(leadId);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setDoNotCreateOpportunity(true);
        try{
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            convertedAccountId = lcr.getAccountId();    
        }
        catch(Exception e){
            System.Debug('Error - ControllerLeadConvertView.convertLead - Exception [' + e.getMessage() + ']');
            return null;
        }
        String sServerName = ApexPages.currentPage().getHeaders().get('Host');
        sServerName = 'https://'+sServerName+'/';
        PageReference retPage = new PageReference(sServerName+convertedAccountId); 
        retPage.setRedirect(true);

        return retPage;
    }       
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


CUSTOM REDIRECTION ON LEAD CONVERT OPERATION IN SALESFORCE.COM

Standard

When you convert a Lead, you will be taken to newly created Account’s detail screen. Thats the standard salesforce.com behaviour.
But what if, you want to show newly created Contact’s detail screen (instead of Account screen), upon Lead conversion? Unfortunately, there is no such setting available in salesforce (yet).
Many people might have achieved it, by some or the other workaround. And here is my trick.
  • Create following Apex Controller class, to use it along with the VF page.
    public class LeadConvertLandingController{
    
        Id newId = null;
        public LeadConvertLandingController(){
            newId = ApexPages.CurrentPage().getParameters().get('newid');
        }
    
        public PageReference redirect(){
            Lead convertedLead = [select ConvertedContactId from Lead where id=:newId];
            PageReference pRef = new PageReference('/' + convertedLead.ConvertedContactId);
            pRef.setRedirect(true);
            return pRef;
        }
    }
    
  • Create a VF page with following markup.
    <apex:page controller="LeadConvertLandingController" action="{!redirect}">
        please wait....
    </apex:page>
    
  • Create a Custom Button for Lead object, as follows:
    • Label = “Convert”.
    • Display type = “Detail Page Button”.
    • Behavior = “Display in existing window without sidebar or header”.
    • Content source = “URL”.
    • And copy below URL in the big text area.
      /lead/leadconvert.jsp?retURL=/{!Lead.Id}&id={!Lead.Id}&saveURL=/apex/LeadConvertLanding
      
  • Now edit the Lead layout and remove the standard Convert button and add the custom Convert button.
  • Thats it!
Hope thats helpful!
Note: This solution works only when, lead conversion is done with new Account/Contact during lead conversion process. It may not work, if existing Account/Contact is chosen, during lead conversion process/screens.
=====================================================
========================================================

Override Lead Conversion


One of the most annoying things to me about Salesforce is that, unless you have a private sharing model for lead processes, anyone can convert anyone else's leads.  It causes great distress at many organizations, but mostly people tend to live with it.

Here's a simple little solution - Make a custom button that overrides the Salesforce "Convert" button.
First, start off with a page controller that calls Database.ConvertLead():
01public class leadConvertController {
02        Public lead lObj;
03        Public Id leadId;
04         
05        public leadConvertController (ApexPages.StandardController stdController)  {
06            System.debug('Called Constructor.');   
07            leadId = ApexPages.currentPage().getParameters().get('id');                   
08            }
09           
10      public PageReference DoConvert()  {
11            System.debug('In DoConvert Method');                       
12            LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];           
13             
14            lObj = [SELECT ID, OwnerId from lead where id =: leadid];
15            System.debug('Found Lead - leadID = '+leadId+', UserID = '+UserInfo.getUserId());                                           
16             
17            if(lObj.OwnerId == UserInfo.getUserId() ){  // perform the lead convert
18                System.debug('VALID USER; LEAD ID:'+lObj.Id);
19                Database.LeadConvert lc = new database.LeadConvert();
20                lc.setLeadId(lObj.Id);        
21                lc.setConvertedStatus(convertStatus.MasterLabel);               
22                Database.LeadConvertResult lcr = Database.convertLead(lc);
23                System.assert(lcr.isSuccess());                                
24                                 
25                Id oppId = lcr.getOpportunityId();
26                System.debug('OPPID: '+oppId);
27                String sServerName = ApexPages.currentPage().getHeaders().get('Host');
28                sServerName = 'https://' + sServerName + '/';
29 
30                System.debug('REDIRECTING TO:'+sServerName+oppId);
31                PageReference newPage =  new pagereference(sServerName+oppId);
32                
33                newPage.setRedirect(true);
34                return newPage ;
35                }
36            else {  // can't do the convert               
37                System.debug('INVALID USER; DO NOT CONVERT');
38                String sServerName = ApexPages.currentPage().getHeaders().get('Host');
39                PageReference newPage =  new pagereference('https://'+sServerName+'/apex/NotAccountOwner');               
40                System.debug('REDIRECTING TO: '+'https://'+sServerName+'/apex/NotAccountOwner');
41                newPage.setRedirect(true);
42                return newPage ;
43                }
44            return null;
45           }          
46    }
Next, create an APEX page that calls the controller's DoConvert() method:
1<apex:page standardController="lead"
2   action="{!DoConvert}"
3   extensions="leadConvertController">
4</apex:page>
Next, define a button on your Lead page-layout that clls this APEX page, with the Lead ID as a parameter;
1../apex/ConvertThisLead?Id={!Lead.Id}
... And of course, you'll have to make sure that the Salesforce standard "Convert" button is removed from your page layout.
This code doesn't create an open task, like the Salesforce standard convert button, but that's a pretty easy thing to add.
 ==========================================================================