+ Start a Discussion
Roshan TamrakarRoshan Tamrakar 

Open 'Add New' window and populate Custom and Standard Fields via Apex Class

Hello experts,


I am creating a VF Page in Contact detail page. The page lists some values from an object which is child of the Contact object. This is not important though. One of the columns in the list has a commandLink.

What I want to achieve is that when the link is clicked, I want open Case detail page in ‘add mode’ (ready to save, as if user has clicked on ‘New’ button in ‘Cases’ Home page). I also want to populate some of the fields with default values when the Case detail page is opened.

I succeeded on most parts, like I could open Case detail page in ‘add-mode’ and populated some of the fields.


Part of my VF Page code is:


 <apex:commandLink value="{!ent.ButtonCaption}" action="{!CreateNewCase}">
  <apex:param name="prmFunc" value="{!ent.NewCaseFuncName}"/>
  <apex:param name="prmEntId" value="{!ent.ent.Id}"/>
  <apex:param name="prmSPId" value="{!ent.ent.Support_Program__c}"/>
  <apex:param name="prmEntName" value="{!ent.ent.Name}"/>


And the function in  my Apex Class is:


public PageReference CreateNewCase(){
 String prmFunc = System.currentPageReference().getParameters().get('prmFunc');
 ID prmEntId = System.currentPageReference().getParameters().get('prmEntId');
 ID prmSpId = System.currentPageReference().getParameters().get('prmSPId');
 String prmEntName = System.currentPageReference().getParameters().get('prmEntName');

 String ContactId, AccountName, CaseOrigin, Status, Priority, EntId;
 ContactId = (String)contId;
 ContactId = '&cas3_lkid='+ContactId.substring(0,ContactId.length()-3);

 AccountName = [Select Name from Account where Id=:acId].Name;
 AccountName = '&cas4='+AccountName;

 EntId = (String)prmEntId;
 EntId = '&00N60000001NTnz='+EntId.substring(0,EntId.length()-3);
 //EntId = '&00N60000001NTnz=a0760000001kq3lAAA';
 //EntId = '&00N60000001NTnz=a0760000001MYD9';

 CaseOrigin = '&cas11=Phone';
 Status = '&cas7=New';
 Priority = '&cas8=High';
 return new PageReference('/500/e—retURL=/'+acId+ContactId+AccountName+CaseOrigin+Status+Priority+EntId);



The parameter is being sent correctly and there is no problem in it.


According to the suggestion given in the links below, I was able to populate some of the fields by assigning values in their ‘Field ID’ in the link.





So, to open a New Case window with data populated for Contact, Account, Case Origin, Status, Priority and Ent (one of the custom fields in Case object) I build up new URL in the following way:


  • ‘/500/e’ - to open Case in add mode
  • ‘&cas4 =’ +AccountName – to populate Account Name field
  • ‘&cas3_lkid=’+ContactId – to populate Contact Name field
  • '&cas11=Phone' – to populate Case Origin field
  • '&cas7=New' – to populate Status field
  • '&cas8=High' – to populate Priority field
  • '&00N60000001NTnz=a0760000001MYD9' – to populate Ent field


Ent is the lookup field in Case object that references to a custom object ‘Ent’


The value ‘00N60000001NTnz’ is the ‘Field ID’ of Ent field in Case object, which I got in the following way:

  • Setup --> App Setup --> Customize --> Cases --> Fields
  • Click on Custom field ‘Ent’ under ‘Case Custom Fields & Relationships’ section
  • From the url ‘https://na4.salesforce.com/00N60000001NTnz’, get the last value


All other other fields are populated except the ‘Ent’ field. I even tried hard-coding the ‘Ent’ id.


My question is:

  • Am I specifying the Ent ‘Field ID’ incorrectly?
  • Is it an appropriate way of opening ‘New Record’ window?
  • Getting ‘Field ID’ this way is correct?
  • The Ent field is custom field in Case. If I create the same field (Ent) in the same object (Case) in another org, will the ‘Field ID’ (00N60000001NTnz) remain the same?


Please suggest me if there is another reliable way of opening ‘New Record’ window and populating default values in Custom and Standard fields.





I'm not entirely sure of the answer to all your questions, but to the last one:

The Ent field is custom field in Case. If I create the same field (Ent) in the same object (Case) in another org, will the ‘Field ID’ (00N60000001NTnz) remain the same?

The answer here is no, if you create this field in another org the field ID will not be the same, even if you packaged that field yourself.
I agree with Werewolf, but the packaging system will try to compensate for this by rewriting every occurence of a unique ID string inside of packaged S-controls and other components.  So, in Roshan's scenario, if you create some custom field on Case and Firebug tells you that the ID of the field is 10000000000abcdef, then if you create an S-control in that org that includes something like:

     <a href="/500/e?10000000000abcdef=my_prepop_text">Click here for a pre-populated editor you won't forget</a>

and package up the whole mess, installing into other orgs will preserve the correspondence between the field's unique ID and the text within the S-control.  I think it will also update the contents of Documents (if plain text) and other kinds of packageable components.

Roshan, you should do some experimentation to verify the functionality you need and/or look at the current docs on the packaging system, because I'm going from memory here, and this whole field prepopulation trick may not be the recommended way of doing this.