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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Autofilling lookup from URL using extension

Hi there,

I have an extension that someone from the forum assisted me in writing. It is used for autofilling the account field (from the URL) and saving a custom object - Destiny Survey. This is a child of account. Basically, the extension is designed so that should the url to the force.com site is appended with acct={!Account.Id} it will autofill Account__c and therefore upon completion of the survey it will place the record into the correct account.

I was hoping someone could help me do the same, except for a picklist field "Which_Product_Service__c" and more importantly Office__c which is a lookup field. Accounts also have a lookup to office if this helps?

This is what the extension looks like:

public class extDestinySurvey
{
    public Destiny_Survey__c Dess {get;set;}

    private Id AccountId
    {
        get
        {
            if ( AccountId == null )
            {
                String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
                try
                {
                    if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
                }
                catch ( Exception e ) {}
            }
            return AccountId;
        }
        private set;
    }
 

    public extDestinySurvey(ApexPages.StandardController controller)
    {
        Dess = (Destiny_Survey__c)controller.getRecord();
    }

    public PageReference saveDestinySurvey()
    {
        Dess.Account__c = AccountId;
        upsert Dess;

        // Send the user to the detail page for the new account.
        return new PageReference('/apex/Destiny_Survey_Thank_You');
    }
}


This is my ATTEMPT at a method which will do the same for the office__c field.
private Id OfficeId
    {
        get
        {
            if ( OfficeId == null )
            {
                String OffIdParam = ApexPages.currentPage().getParameters().get( 'OffId' );
                try
                {
                    if ( !String.isBlank( OffIdParam ) ) OfficeId = Id.valueOf( OffIdParam );
                }
                catch ( Exception e ) {}
            }
            return OfficeId;
        }
        private set;
    }

Please help, any advice or ehlp would be much appreciated
Best Answer chosen by Developer.mikie.Apex.Student
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Done and dusted. Was still not able to autofill recordtype....but, I didnt try with this new method. Long story short, for all future thread viewers. This is the way in which to write an extension which will autofill two lookup fields and a picklist, the special thing about this is that it wll work for force.com sites. All by simply appending the end fot he URL. E.G. Acct={!AccountID}

public class extDestinySurvey
{
    public Destiny_Survey__c Dess {get;set;}

    private Id AccountId
    {
        get
        {
            if ( AccountId == null )
            {
                String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
                try
                {
                    if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
                }
                catch ( Exception e ) {}
            }
            return AccountId;
        }
        private set;
    }
  
    private Id OfficeId
    {
        get
        {
            if ( OfficeId == null )
            {
                String OffIdParam = ApexPages.currentPage().getParameters().get( 'Off' );
                try
                {
                    if ( !String.isBlank( OffIdParam ) ) OfficeId = Id.valueOf( OffIdParam );
                }
                catch ( Exception e ) {}
            }
            return OfficeId;
        }
        private set;
    }
  



  
    public extDestinySurvey(ApexPages.StandardController controller)
    {
        Dess = (Destiny_Survey__c)controller.getRecord();
         Dess = (Destiny_Survey__c) controller.getRecord();
        Dess.Which_Product_or_Service__c = ApexPages.currentPage().getParameters().get('Ser');
    }

    public PageReference saveDestinySurvey()
    {
        Dess.Account__c = AccountId;
        Dess.Office__c = OfficeID;
      
        upsert Dess;
    

        // Send the user to the detail page for the new account.
        return new PageReference('/apex/Destiny_Survey_Thank_You');
    }
}

All Answers

Ankit GuptaAnkit Gupta
Hi mikie,

Does your extension class  just prepopulates the lookup.??

You might want to try this for prepopulating the lookup through URL hacking technique and URLFOR function in VF Page.:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000009m9EIAQ

Hope this helps.

Regards,
Ankit Gupta
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
It is for pre-populating force.com site page....I figured out my problem though. i wasnt referencing the Variable at the end of the code..I thought the end was just for applying save functionality. This is the end code:

public class extDestinySurvey
{
    public Destiny_Survey__c Dess {get;set;}

    private Id AccountId
    {
        get
        {
            if ( AccountId == null )
            {
                String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
                try
                {
                    if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
                }
                catch ( Exception e ) {}
            }
            return AccountId;
        }
        private set;
    }
   
    private Id OfficeId
    {
        get
        {
            if ( OfficeId == null )
            {
                String OffIdParam = ApexPages.currentPage().getParameters().get( 'Off' );
                try
                {
                    if ( !String.isBlank( OffIdParam ) ) OfficeId = Id.valueOf( OffIdParam );
                }
                catch ( Exception e ) {}
            }
            return OfficeId;
        }
        private set;
    }
   
       private Id RecordTypeId
    {
        get
        {
            if ( RecordTypeId== null )
            {
                String RecordIdParam = ApexPages.currentPage().getParameters().get( 'Record' );
                try
                {
                    if ( !String.isBlank( RecordIdParam ) ) RecordTypeId= Id.valueOf( RecordIdParam );
                }
                catch ( Exception e ) {}
            }
            return RecordTypeId;
        }
        private set;
    }
   


   
    public extDestinySurvey(ApexPages.StandardController controller)
    {
        Dess = (Destiny_Survey__c)controller.getRecord();
    }

    public PageReference saveDestinySurvey()
    {
        Dess.Account__c = AccountId;
        Dess.Office__c = OfficeID;
        Dess.RecordTypeId = RecordTypeId;
        upsert Dess;

        // Send the user to the detail page for the new account.
        return new PageReference('/apex/Destiny_Survey_Thank_You');
    }
}

However, thank you so much for taking the time to reply.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Woops, i talked too soon. I managed to get the office__c field going, except when deploying the recordtypeID part of the code, the changeset gave me this error and failed:


TestExtDestinySurvey.TestExtDestinySurvey() Class 72  Failure Message: "System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: : [RecordTypeId]", Failure Stack Trace: "Class.extDestinySurvey.saveDestinySurvey: line 72, column 1 Class.TestExtDestinySurvey.TestExtDestinySurvey: li...
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
The extension class is designed to pre-populate the VF page hosted on force.com sites, it is meant to pre-populate the fields office__c, account__c and set the record type. THh first two work, however the record type will not post to change-set as a result of the above error. Maybe I am querying it wrong? Instead of the record type, it would even work better to pre-populate the which_product_or_service__c (picklist) field. Is that possible? E.g adding &ser=Advantage Program ...to the url? What are your thoughts? Thank you for your help/
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Done and dusted. Was still not able to autofill recordtype....but, I didnt try with this new method. Long story short, for all future thread viewers. This is the way in which to write an extension which will autofill two lookup fields and a picklist, the special thing about this is that it wll work for force.com sites. All by simply appending the end fot he URL. E.G. Acct={!AccountID}

public class extDestinySurvey
{
    public Destiny_Survey__c Dess {get;set;}

    private Id AccountId
    {
        get
        {
            if ( AccountId == null )
            {
                String acctParam = ApexPages.currentPage().getParameters().get( 'acct' );
                try
                {
                    if ( !String.isBlank( acctParam ) ) AccountId = Id.valueOf( acctParam );
                }
                catch ( Exception e ) {}
            }
            return AccountId;
        }
        private set;
    }
  
    private Id OfficeId
    {
        get
        {
            if ( OfficeId == null )
            {
                String OffIdParam = ApexPages.currentPage().getParameters().get( 'Off' );
                try
                {
                    if ( !String.isBlank( OffIdParam ) ) OfficeId = Id.valueOf( OffIdParam );
                }
                catch ( Exception e ) {}
            }
            return OfficeId;
        }
        private set;
    }
  



  
    public extDestinySurvey(ApexPages.StandardController controller)
    {
        Dess = (Destiny_Survey__c)controller.getRecord();
         Dess = (Destiny_Survey__c) controller.getRecord();
        Dess.Which_Product_or_Service__c = ApexPages.currentPage().getParameters().get('Ser');
    }

    public PageReference saveDestinySurvey()
    {
        Dess.Account__c = AccountId;
        Dess.Office__c = OfficeID;
      
        upsert Dess;
    

        // Send the user to the detail page for the new account.
        return new PageReference('/apex/Destiny_Survey_Thank_You');
    }
}
This was selected as the best answer