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
Amar cAmar c 

Need object record id with salesforce Urgent

HI I am stuck in a problem.I need code for get object record id with salesforce  by this my problem will soved to redirect on record type... for example URL is 

 

https://cs11.salesforce.com/abc?setupid=CustomObjects

 

I need abc by apex code not by visual force page.Is there any way to fetch this. Please some one rely on this.

Noam.dganiNoam.dgani

what do you mean in apex code not in visualforce page?

 

in what context your apex code will run? (custom button/trigger/...) and how is that context related to this url?

Amar cAmar c

code is something like this...

i have to open some record type page directly, so make Url we need custom object's ID.

I will override new button of custom object and by this visual force page's controller i will redirect on record type accoding to profile.

so to make recordtype URL i need this.because i have to skip on some caes recordtype selection page as well.

???

need any clerification?

Noam.dganiNoam.dgani

Hi Amardeep

 

I'm sorry if i dont understand you correctly, but what i did understand is that you want to override the "new" button of an object and skip the record type selection, to automatically select it according to the user profile. hope i'm correct.

 

please see the solution below. its quite robust and you can always use a more hardcoded solution, but i dont advise that.

 

basically the solution is overriding the new button to a visualforce page (provided below with extension)  and using a custom setting list table to hold a mapping of profile name to desired record type name (this way you can change mapping without deployment).

 

you can change the maaping to id for id and save some queries, but that will cause maintenance issues when transferring between environments.

 

public with sharing class CTRL_RT_Guiding{

    private Id user_profile_id,record_type_id;
    private string profile_name,record_type_name,obj_id_prefix;
    private string obj_name = 'RT_Example_Obj__c';
    ApexPages.StandardController controller;
    
    public CTRL_RT_Guiding(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }
    
    public pageReference guide()
    {
        //get user profile id
        user_profile_id = UserInfo.getProfileId();
        try{
            //get profile name
            profile_name = [select name from Profile where Id =: user_Profile_id limit 1].Name;
            //get relevant record type for profile using the custom settings table
            record_type_name = Profie_RT_Mapping__c.getValues(profile_name).Record_Type_Name__c;
            //get record type id according to name
            record_type_id = [select id from RecordType where Name =: record_type_name limit 1].Id;
            //get the object prefix
            Map<String,Schema.SobjectType> globalDescribe = Schema.getGlobalDescribe();
            obj_id_prefix = globalDescribe.get(obj_name).getDescribe().getkeyprefix();
            
            //return a pageReference to the edit page of the new record already populated with the relevant record type
            pageReference pRef = construct_page_reference();
            return pRef;
        }
        catch(Exception queryException)
        {
            error_handler(null);
            return null;
        }
        return null;
    }
    
    
    private void error_handler(string msg)
    {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,msg == null ? 'Error Occured' : msg));
    }
    
    private pageReference construct_page_reference()
    {
        return new pageReference('/'+ obj_id_prefix + '/e?RecordType='+record_type_id+'&nooverride=1');   
    }
}

 

 

and the page itself (please remember to change the standardController to your object).

 

<apex:page standardController="RT_Example_Obj__c" extensions="CTRL_RT_Guiding" action="{!guide}">
    <apex:pageMessages />
</apex:page>

 

The custom setting used is called - Profie RT Mapping (i dropped the "L" and didnt bother fixing, you can go ahead and get it right in table and code) - and it consists of a "Name" field (default for all custom settings) which holds the PROFILE name. and a "Record_Type_Name__c" name field (Text) which holds the relevant record type name.

 

i didnt bother with "return URL" for the page, or default maaping in case the profile isnt mapped - i think you can handle that the way you like.

 

 

Hope this helps.

dont hesitate the ask any questions.