function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDummySFDummy 

how do I get ID from salesforce URL in my controller

I am writting a VF page to show custom objects related to account in case.

 

I need to show in case custom objects related to Case.Account. How can I can get ID from salesforce URL

for example I have the following url and I want to get if id part of the URL

 

https://na4.salesforce.com/5005000000EEh4t

 

Thanks for help

 

Peter_sfdcPeter_sfdc

You've not stated how your page is being accessed, but I'm inferring that, since you are showing a standard page layout URL that this is a VF page embedded in a page layout. 

 

In Apex, in a custom controller, it should be accessible like this: 

ApexPages.currentPage().getParameters().get('id');

But if this is embedded in a page layout then you must be using standard controller. If so, then you just go to the param in the constructor and get a handle to the passed in Standard Controller. 

 

If it is an extension for standard controller, you can just get the case record using this: 

public class MyExt{

//create a private local handle for the standard controller private ApexPages.StandardController ctrl; public MyExt(ApexPages.StandardController ctrlParam){ //assign from the passed in controller ctrl = ctrlParam; //now access the record through controller } public void someMethod(){
//access controller for information about the page record. Case theCase = (Case) ctrl.getRecord(); Id caseId = ctrl.getRecord().get('id'); } }

 You can also get it from your visualforce code by binding to the $CurrentPage global variable in your markup: 

{!$CurrentPage.parameters.Id}

 

SFDummySFDummy

Thanks for the response. 

I am getting the following error when getting caseID

 Illegal assignment from Object to Id at line 25 column 5

 

Id caseId = ctrl.getRecord().get('id');

 

Peter_sfdcPeter_sfdc

Yeah...I missed the cast. Try this:  

Id caseId = (Id) ctrl.getRecord().get('id');

 If not that, then this:

Id caseId = String.valueof(ctrl.getRecord().get('id'));

 I'm pretty sure that either of these will work and the outcome is fundamentally equivalent.

 


 

Mahesh LakkarajuMahesh Lakkaraju
Hi Peter,

I am currently trying to embed a VF Page using Page layout.

However, when i try to access the ID usiing getRecord().get('id), and System.debug the ID, i always get a 'null' value in it

I gathered you mentioned something about using a parm in a constructor. Could you kindly elaborate on that part?

Thanks,
public batchJobs(ApexPages.StandardController controller) {
    this.Systrm= (System__c) controller.getRecord();
    Id sysID = Systrm.Id; 
    System.debug(this.sysID);
    }
<apex:page standardController="System__c" extensions="batchJobs">
    <apex:form >
       <br/><br/>
        <c:BatchViewerComponent id="jobs" numberOfJobs="1"/>
    </apex:form>
</apex:page>

Thanks,

Mahesh
Reddy_tecReddy_tec
Hi Mahesh, 

In line 3, you are not casting the Systrm variable type into ID type. You will find the success I believe.

Good luck
Ulaganathan KodimariUlaganathan Kodimari
Hello Peter

Im a salesforce newbie.
Im trying to access ID from a webservice method.
Im calling this method from a custom button on custom page layout.

Im getting SOAP error when i try accessing ID using 
ApexPages.currentPage().getParameters().get('id');

Here is my code snippet. and im trying to copy an instance of custom object record into SF standard object using Custom settings.

global with sharing class copyObjCustomSettingsMapping    {
     webservice static void MapLeadfields()    {

        string MyLeadid;
        Lead LeadObj;
        string qry = '';
        Map<string, string> MapMappingTable = new map<string,string>();
        Savepoint sp = Database.setSavepoint();

        try    {
                   LeadObj = new Lead();
                                     
                   MyLeadid = ApexPages.currentPage().getParameters().get('id');
                   system.debug('Checkpint 0::::recordTypeId::::::'+ MyLeadid);

                    for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values()) {
         
                            if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null ) {
            
                                MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
                                qry += mappingTableRec.Name + ',';
                            }
                        }
                   
                   qry = 'select ' + qry + 'id FROM My_Lead__c where id ='+ MyLeadid;
                       
                       system.debug('Checkpint 1::::::::::'+ qry);
                       
                       My_Lead__c MyLead = Database.query(qry);
                    
                       for(String sMyLeadField: MapMappingTable.keySet()) {
                           String sLeadField = MapMappingTable.get(sMyLeadField);
                           LeadObj.put(sLeadField, MyLead.get(sMyLeadField));
                       }
                    
                       LeadObj.OwnerID = UserInfo.getUserId() ;
                       LeadObj.status='new';
                       insert LeadObj;
                   
        } catch(Exception ex) {
                Database.rollback(sp);
            }
        }
}

Javascript call from custom button

{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} 
   var result = sforce.apex.execute("copyObjCustomSettingsMapping", "MapLeadfields",{}); 
   alert(result);

Im always getting a null for the ID which im trying to access in the pagelayout of custom object.
If it is a visualforce page it is a matter a second i can do write code snippet exactly like yours and get the job done..

Appreciate any help.. thanks