There is object called 'Event__c' and in that, a record is created let's say 'parent' and on a button click child records can be created inside the 'parent'

var masterQry = "SELECT Board_Opinion__c,Master_Event__c,No_of_child__c,Request_End_Date__c,Request_Start_Date__c,Event_Start_Date__c,Event_End_Date__c  FROM Event__c WHERE Id = '{!Event__c.Id}' AND RecordType.Name = 'Parent Event'";
masQry = sforce.connection.query(masterQry); 
masQryRec = masQry.getArray("records"); 

var childsQry = "SELECT Board_Opinion__c,Master_Event__c,No_of_child__c,Request_End_Date__c,Request_Start_Date__c,Event_Start_Date__c,Event_End_Date__c  FROM Event__c WHERE Master_Event__c = '{!Event__c.Id}' AND RecordType.Name = 'Child Event'";
chldsQry = sforce.connection.query(childsQry); 
chldsQryRecs = chldsQry.getArray("records"); 

    var masterEvent = masQryRec[0]; 
    var childEvents = chldsQryRecs.length;

    var boardOpinion = masterEvent.Board_Opinion__c;
    var noOfChildRecs = masterEvent.No_of_child__c;

    if(boardOpinion == "Positive Opinion"){
        if(noOfChildRecs > childEvents){

        var url ="/a00/e?CF00N7F000008JtcF_lkid={!Event__c.Id}"+ 

            alert("Your child record limit excide.");
    } else{
        alert("You are not having access to create record.");
    alert("An Error has Occurred. Error:" + e);

In URL on a javascript button, the Id's are to be assigned dynamically so that no error occurs when the code is moved to production org. Lookup field is to be auto-populated with parent record name ('parent').

1 Answer 1


call the apex class from the javascript button click

var fldId = sforce.apex.execute("dynamicId","dynamicId", {field_label:"Master Event"}); 
var ObjPrefix = sforce.apex.execute("dynamicId","getObjectKeyPrefix", {object_name:"Event__c"});

And update the URL

if(noOfChildRecs > childEvents){
            var url ="/"+ObjPrefix+"/e?"+fldId+"_lkid={!Event__c.Id}"+ 

apex class to find field ID's and object prefix. Make sure the fields are added in the detail page (i.e)page layout

global class dynamicId{

webservice static String getObjectKeyPrefix(String object_name){
        return (Event__c.SObjectType.getDescribe().getKeyPrefix());

    webservice static string dynamicId(String field_label){
        // find prefix of object name :'Event__c'
        String prefix = Schema.getGlobalDescribe().get('Event__c').getDescribe().getKeyPrefix();
        PageReference pr = new PageReference('/' + prefix + '/e?nooverride=1');

        String html;
        Blob pageContent = pr.getContent();
        if( pageContent != null ) {
            html = pageContent.toString();

        Matcher macher = Pattern.compile('<label for="(.*?)">(<span class="requiredMark">\\*</span>)?(.*?)</label>').matcher(html);
        while ( macher.find() ) {
            String label = macher.group(3);
            String fldId = macher.group(1);
            system.debug(label+ '****************' +fldId);
            // return id if the fieldLabel matches
                return fldId;

        return '';

with reference to How to get the entity ID for a custom field in Apex?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .