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
dmchengdmcheng 

URLFOR with custom button to create new record?

Hello.  I want to create a simple custom list button that creates a new record of a specific record type and with some fields already populated with data.  However, I keep getting the record type screen when I click the button.

 

Here's the entire URL code for the button. I'm not even trying to populate the fields with data right now, just trying to get past the Record Type prompt screen.

 

 

{!URLFOR($Action.Investment_Task__c.New, null, [RecordType='012400000009bAQAAY'])}

 

 

 

Thanks

David

Best Answer chosen by Admin (Salesforce Developers) 
RCE_JereriahRCE_Jereriah

FYI,

 

"p3" is the field for Record type on Custom Objects. I'm not sure if this is also true on Standard Objects. Here's some working code that incorporate quite a few different field types, including the Record type field. It uses the appropriate ID as the value.

 

 

{!URLFOR($Action.Scope_3_Emmissions__c.New, null, [ "p3" = "012500000009Wjw", "Name" = ( "Business Travel - Flight " & "(" & (TEXT(TODAY())) & ")" ), "00N50000001ydlr" = "3", "00N50000001ydmL" = "Business Travel - Flight", "00N50000001ygWz" = IF (Event.Event_Roundtrip_Flight__c = TRUE, 1, 0), "00N50000001ydr6" = Event.Event_One_Way_Flight_Miles__c, "00N50000001ygXE" = Event.Event_Arrival_Airport_Text__c, "00N50000001ygXJ" = Event.Event_Departure_Airport_Text__c, "save"=1 ], true)}

 ~jm

 

 

 

All Answers

aalbertaalbert

I ran into this scenario in the past too. Not sure I have a good solution or workaround.

 

Here is the Idea to vote on: http://ideas.salesforce.com/article/show/66276

dmchengdmcheng
Rats.  I hate having to create S-Controls with their limited lifespan now.
James (CloudAnswers)James (CloudAnswers)

I believe you can do this, just add "save=1" at the end of your parameter list:

 

{!URLFOR($Action.Investment_Task__c.New, null, [RecordType='012400000009bAQAAY', save=1])}

 

 

dmchengdmcheng
That was it, James, thanks!  Do you know where save=1 is documented?
James (CloudAnswers)James (CloudAnswers)
I am not sure where it's documented (if it's documented).  I am a freelance consultant and had a small client on group edition looking for that functionality and I tried a couple things until I found one that worked.
dmchengdmcheng

I spoke too soon - the URLFOR is using the default record type, even if I change the record type ID.  I noticed that the final URL of the page has two occurrences of "&RecordType" : the first one is 15 characters and is the default record type; the second occurrence is 18 characters and is the record type ID that I want.

 

I've tried changing the 'no override' setting but it doesn't make a difference.

James (CloudAnswers)James (CloudAnswers)

Are you putting the "&" character in front of the RecordType field name?  Try without it.  If you paste the entire string here you are using behind the button it may help.  The Salesforce.com IDs are 18 characters internally (and by API) and 15 characters to the web interface.  If removing the "&" character doesn't help, I would say to try adjusting the length of your ID that you are passing to be the other length to see if it gets imprinted into the page correctly.

dmchengdmcheng

Here's the code:

 

{!URLFOR($Action.Investment_Task__c.New, null, [RecordType='012400000009bAVAAY', save=1], true)}

 

 The default record type is 012400000009bAQAAY.  Here's what the portion of the final URL:  &RecordType=012400000009bAQ&RecordType=012400000009bAVAAY.  I've tried

RecordType='012400000009bAV'
but that didn't make a difference.

 

 

TigerPowerTigerPower

I'm trying to solve the record type issue too. I'm copying Opportunity details to an offer, and I would like to choose the right record type in URLFOR code.

 

This is the code without the record type:

 {!URLFOR( $Action.Opportunity.Clone , Opportunity.Id,
[
cloneli=1,
opp11="Offer Draft",
opp5="",
opp14="",
opp10="",
opp3= Opportunity.Name + " - Offer",
CF00N20000001dhJ2=0,
CF00N20000001dhJ2_lkid=0,
CF00N20000001dhJ2_lkold=0,
CF00N20000001dhJ2_lktp=0,
CF00N20000001dhJ2_mod=""
],
true)}
CF00N20000001cfve = {!Opportunity.Assigned_Partner__c},
00N20000001dBDz ={!Opportunity.Estimated_Building_Time__c}

 

I have tried to add the record type to code but I have not succeeded to change the record type with the code. Source Opportunity is 'Opportunity' record type, and target Opportunity is 'Offer' record type (01220000000DX9R).

 

I have been reading discussion board URLFOR samples and advice, but not been able to solve this myself. Can you give me some advice with this one?

 

BR,

TigerPower

RCE_JereriahRCE_Jereriah

How about forcing data into a lookup field? So far I'ce tried to for the Opportunity name and Opportunity ID into a lookup field on another Object and I'm getting a blank field.

 

Does anyone know how to do this? I'd love for this to work automatically to put in the related list entried upon creation. I'm dealing with Opportunity Products, so everything has been a treat so far. It's working--just not forcing data into the Lookup(Opportunity) field on ym Custom Object.

 

The information in this thread prooved invaluable to me. Thanks for the great starting point. Also, I can't get the Saved=1 part to work in here. I get some Error 20 every time. Here's my working code (where's the saved syntan go?):

 

{!URLFOR($Action.Commission_Tracking__c.New, null,
[
"00N50000001y1mA" = (100*OpportunityLineItem.OP_Commission_Rate__c),
"00N50000001y20f" = OpportunityLineItem.OP_Opportunity_Name__c,
"00N50000001y20p" = OpportunityLineItem.OP_Account_Name__c,
"00N50000001y214" =  OpportunityLineItem.TotalPrice,
"00N50000001y21s" =  OpportunityLineItem.Link,
"00N50000001y227" = OpportunityLineItem.OP_Product_Name__c,
"00N50000001y22q" = OpportunityLineItem.Quantity,
"00N50000001y22v" = OpportunityLineItem.UnitPrice,
"00N50000001y23Q" = OpportunityLineItem.Cost_Floor__c,
"0N50000001y23Z" = OpportunityLineItem.Price_Floor__c,
"CT_Opportunity_Lookup__c" =  "&OpportunityLineItem.OP_Opportunity_ID__c"
],
true)}

 

~jm

 

RCE_JereriahRCE_Jereriah
Ugh, no Edit option. The final line in my aboce code is my attempt to get the lookup field to populat upon creation. I've tried every context that I know how (field IDs, quotes, &, etc...).
RCE_JereriahRCE_Jereriah

QUOTE: "Also, I can't get the Saved=1 part to work in here."

 

Proper Syntax, when following other parameters, is "save"=1

rcravenrcraven

Did you ever get this to work?  Can you post a full example of the URLFOR code.

RCE_JereriahRCE_Jereriah

{!URLFOR($Action.Commission_Tracking__c.New, null, [ "00N50000001y1mA" = (100*OpportunityLineItem.OP_Commission_Rate__c), "00N50000001yCjB" = OpportunityLineItem.OP_Commission_Amount__c, "00N50000001y214" = OpportunityLineItem.TotalPrice, "00N50000001y21s" = OpportunityLineItem.Link, "00N50000001y227" = OpportunityLineItem.OP_Product_Name__c, "00N50000001y22q" = TEXT(OpportunityLineItem.Quantity), "00N50000001y22v" = OpportunityLineItem.UnitPrice, "00N50000001y23Z" = OpportunityLineItem.OP_Price_Floor__c, "00N50000001y23Q" = OpportunityLineItem.OP_Cost_Floor__c, "00N50000001y4HO" = OpportunityLineItem.OP_Opportunity_Record_Type__c, "CF00N50000001y1zc" = Opportunity.Name, "CF00N50000001y2RH" = Account.Name, "CF00N50000001y2Uu" = CASE( Opportunity.OwnerId, "[OwnerId], "John Smith:, ""), "save"=1 ], true)}

This created a button on the Opportunity Line Item that creates a new Commision entry and fills in certain fileds on the commission entry based upn fields on the Opportunity Line Item and in the Opportunity. Notice the CF in front of the fields... those are lookup fields and the CF is required. I have a big long CASE statement set up to put the appopriate commission payee in a lookup field to that employee's HR record. I truncated that down and took out the values--no reason to have those here. 

 

This does everything that I want it to do other than set the Owner of the new Commission Entry to the Opportunity Owner. I was informed that you can't set Ownership with a function like this. I have to click Change and set the owner after the records saves, which is does automatically.

 

One last thing, the first data set in the "" is the SF field ID for the field that is going to be populated. The second data set in the "" is the data to populate those fields with.

 

Hope this helps!

~jm

 

RCE_JereriahRCE_Jereriah

Does anyone know the field name to use for the Name field on a custom object. I'm trying to use a button to create a new customer object entry and force the name in. Here's what I have so far. As "Name" doesn't have a SF ID (the number), I don't know what to call that field in my code:


{!URLFOR($Action.Customer_Snapshot__c.New, null,
[
"CF00N50000001yRfr" = Account.Name,
"{!Customer_Snapshot__c.Name}" = Account.Name,

"save"=1
],
true)}

 

 Customer Snapshot is my custom object name, I just can't figure out the correct syntax for the Name field on that object. Every time I try something, it either fails to compile or sets the Name to the ID of the new data. Any help would be great to have, even a hacked work around would be great.

 

I've tried the following to date:

"Customer_Snapshot__c.Name"

"{!Customer_Snapshot__c.Name}"

"Customer_Snapshot__c.Name__c"

"Name"

 

Nevermind, it is simply "Name". It looks like that is the code for the Name field of any customer Object.

~jm

 

 

IntuitivIntuitiv

Hi, 

 

I tried to fallow this discussion the best I could but still have problems... Here is my code :

 

{!URLFOR( $Action.Facture__c.New , null, ["00N20000001qd9M"= Fiche_Projet__c.Numero_de_commande__c , // number"00N20000001qd9N" = Fiche_Projet__c.Date_de_commande__c , // date"00NR0000000Xa1H"= Opportunity.Adresse_de_Facturation__c , // Text"CF00N20000001qerM"= Account.Name , // lookup field"save"=1 

], True)} 

 

Unfortunatly, only the 'number' type field works perfectly. The 'date' type field gives me something but in the wrong type (-> Fri Jul 17 00:00:00 GMT 2009 (NOT DATE TYPE!)) and finally the 'Text' and 'Lookup' fields give me a blank space.

 

 Anyone has an idea? 

 

Thanks,

 

Intuitiv 

IntuitivIntuitiv

Sorry for the syntax! Here is my code again :

 

{!URLFOR( $Action.Facture__c.New , null, ["00N20000001qd9M"= Fiche_Projet__c.Numero_de_commande__c , // number

"00N20000001qd9N" = Fiche_Projet__c.Date_de_commande__c , // date

"00NR0000000Xa1H"= Opportunity.Adresse_de_Facturation__c , // Text

"CF00N20000001qerM"= Account.Name , // lookup field

"save"=1 

], True)} 

 

 

ThomasTTThomasTT

It seems that this post didn't actually find the solution for RecordType.

 

I found a solution for it. Rather than using URLFOR, use

 

'/(keyprefix)/e?nooverride=1&RecordType={!$Request.RecordType}'

 

This URL is called after Record Type selection, and selected RecordType is put in RecordType parameter (because you don't use URLFOR anymore, you have to put all parameters by yourself). The parameter nooverride=1 does the same thing as URLFOR(Action..., null, null, true)
so that you can call original New page. You just add your field id & value pairs and other parameters (retURL, cancelURL, etc.) in the URL.

 

In my case, I put the routing logic in an action method (redirect()) and let it return PageReference for '/(keyprefix)/e?nooverride=1&RecordType={!$Request.RecordType}' based on given parameters.

 

When I inherited "save_new" parameter, it still show RecordType Selection page twice.

 

I hope this can help you (it worked perfect in my case).

 

ThomasTT

 

<apex:page standardController="Opportunity" extensions="OpportunityRedirectExtension" action="{!redirect}">
</apex:page>

 

public with sharing class OpportunityRedirectExtension {

private ApexPages.StandardController controller = null;

public OpportunityRedirectExtension (ApexPages.StandardController controller){
this.controller = controller;
}


public PageReference redirect(){

String prefix = Opportunity.SObjectType.getDescribe().getKeyPrefix();
String param = getParameters() + '&(your field id)=(your field value)';
return new PageReference('/'+prefix+'/e?nooverride=1&'+param);
}

// Inherit previous parameters, more imporatntly, RecordType parameter!
private String getParameters(){
string param = '';
Map<String, String> strMap = ApexPages.currentPage().getParameters();
String[] keys = new String[]{'RecordType', 'retURL', 'cancelURL'};
for(String s : keys){
if(strMap.containsKey(S)) param += s + '=' + strMap.get(s) + '&';
}
if(param.length() > 0) param = param.substring(0, param.length()-1);
return param;
}

}

 

 

Message Edited by ThomasTT on 09-09-2009 06:06 AM
Message Edited by ThomasTT on 09-09-2009 06:08 AM
Message Edited by ThomasTT on 09-09-2009 06:08 AM
Message Edited by ThomasTT on 09-09-2009 04:06 PM
RCE_JereriahRCE_Jereriah

FYI,

 

"p3" is the field for Record type on Custom Objects. I'm not sure if this is also true on Standard Objects. Here's some working code that incorporate quite a few different field types, including the Record type field. It uses the appropriate ID as the value.

 

 

{!URLFOR($Action.Scope_3_Emmissions__c.New, null, [ "p3" = "012500000009Wjw", "Name" = ( "Business Travel - Flight " & "(" & (TEXT(TODAY())) & ")" ), "00N50000001ydlr" = "3", "00N50000001ydmL" = "Business Travel - Flight", "00N50000001ygWz" = IF (Event.Event_Roundtrip_Flight__c = TRUE, 1, 0), "00N50000001ydr6" = Event.Event_One_Way_Flight_Miles__c, "00N50000001ygXE" = Event.Event_Arrival_Airport_Text__c, "00N50000001ygXJ" = Event.Event_Departure_Airport_Text__c, "save"=1 ], true)}

 ~jm

 

 

 

This was selected as the best answer
RCE_JereriahRCE_Jereriah

The only way I've found to move a date field effectively is ugly, but it works. On the original record, I created two additional fields one is a Forumla field of type Text that is simply the the date field you need to move to the new record. I used the LEFT and RIGHT functions to put the date in the correct order. Here's that code:

 

Right( Text(EC_Week_Start__c),5) & "-" &
Left( Text(EC_Week_Start__c),4)

 

 I then created a second field to replace come characters in the above field. You need / not - in a date field. Here's that code:

 

SUBSTITUTE( EC_Week_Start_Text__c, "-","/")

 I could probably do both of the above in one field, I just didn't want to deal with the syntax. I then use the result of thesecond formula to push it into a new record. Here's that final code:

 

{!URLFOR($Action.Scope_3_Emmissions__c.New, null,
[
"p3" = "012500000009WnZ",
"Name" = ( $User.FirstName & " " & $User.LastName & " Employee Commute (" & TEXT(Employee_Commute__c.EC_Week_Start__c) & ")" ),
"00N50000001yiKq" = Employee_Commute__c.EC_Week_Start_Formatted__c,
"00N50000001yi9o" = Employee_Commute__c.EC_Total_Miles_Traveled__c,
"00N50000001yiXQ" = Employee_Commute__c.EC_Total_Emissions__c,
"CF00N50000001yivw" = Employee_Commute__c.Name,
"save"="1"
],
true)}

 

 EDIT: Turns out you can do this in one field. Here's the appropraite code:

 

SUBSTITUTE( Right( Text(EC_Week_Start__c),5) & "-" &
Left( Text(EC_Week_Start__c),4), "-","/")

 

EDIT2: Hell, I just did the date format directly in my URLFOR code. Fewer fields is always better. Here's the final code for the URLFOR function:

 

{!URLFOR($Action.Scope_3_Emmissions__c.New, null, [ "p3" = "012500000009WnZ", "Name" = ( $User.FirstName & " " & $User.LastName & " Employee Commute (" & TEXT(Employee_Commute__c.EC_Week_Start__c) & ")" ), "00N50000001yiKq" = (SUBSTITUTE( Right( Text( Employee_Commute__c.EC_Week_Start__c ),5) & "-" & Left( Text( Employee_Commute__c.EC_Week_Start__c ),4), "-","/")) , "00N50000001yi9o" = Employee_Commute__c.EC_Total_Miles_Traveled__c, "00N50000001yiXQ" = Employee_Commute__c.EC_Total_Emissions__c, "CF00N50000001yivw" = Employee_Commute__c.Name, "save"="1" ], true)}

 

 ~jm

 

 

Message Edited by RCE_Jereriah on 09-09-2009 09:24 AM
Message Edited by RCE_Jereriah on 09-09-2009 09:26 AM
psa_phooeypsa_phooey

Hi,

Can someone tell me how to get the parameters to pass into each $Action, for example $Action.Object.New you seem to be passing in null as the second parameter. But what does that mean?

Thanks

dmchengdmcheng

Hi RCE.  Out of curiosity, how did you discover that p3 is the RecordType field for custom objects?

 

Thanks

David

RCE_JereriahRCE_Jereriah

Only by creating a record and then using the [Change] link to change the record type. On the change record type page, I did a View Source and did a search for Record Type. You'll see the "label" for the field New Record Type is p3. I've had to use this same procedure to find other fields names as well. Especially for Case fields that are standard to SFDC.

 

The exact line on the Page Source is here:

<tr><td class="labelCol requiredInput"><label for="p3"><span class="requiredMark">*</span>New Record Type</label></td><td

 

Hope this helps!

~jm

 

Jereriah Manning

Director Of Business Operations

Renewable Choice Energy

Message Edited by RCE_Jereriah on 11-06-2009 02:10 PM
dmchengdmcheng

ThomasTT - I'm finally returning to this thread.  I have a couple questions.

 

1.  In your code, where do I specify the record type ID field and value so I can bypass the type selection screen?  Do I put it in this line?

 

String param = getParameters() + '&(your field id)=(your field value)';

 

2.  When I go to create the list button, no VF pages appear in the picklist.  I see that you have another forum thread where the same thing happens, but it seems like a different situation.

 

Thanks

David

ThomasTTThomasTT

1. Look at getParameters() method carefully. It copies RecordType parameter value from the original URL parameter. I thought that's your point.

 

# Actually, rather than putting URL parameters in new PageReference(),

# PageReference page = new PageReference('just URL');

# Map<String, String> param = page.getParameters();

# param.put('key', 'value');

# ....

# is much better.

 

2. List Button

If you meant a List Button which will be displayed on the related list rather than the top of detail page, it makes sense. You need to use StandardSetController of the SObject for the VF page to use it as a List Button. And some SObject doesn't support StandardSetController (e.g. Task).

So, rather than using it as VFPage, use javascript Onclick, and in it, call VF Page like document.location.replace("/apex/yourpage?id=xxxxx&...");

In that case, it will be very difficult to get values (e.g. record id) related to the detail page (rather than the related list), because the LIst button can be on any detail page. If it is Task List button, it can be in any SObject detail page. You may have to get the record id (of the detail page) from URL itself by using document.location.pathname.

 

Good luck 

dmchengdmcheng

Hmm, I'm confused.  When I started this thread, I wanted to use javascript/URLFOR because I didn't know how to use VF.  When I saw your code, I thought that was all I needed and I didn't have to mess with js or urlfor.  Are you saying that I need to pass a URL to your apex code?  I just want to make a custom button with VF/Apex that creates a new record of a specific type (with the type being hardcoded) but use the standard salesforce pages instead of having to create my own VF pages for data input.

 

Thanks

David

RCE_JereriahRCE_Jereriah

That's what my code (top of thise page) does. It is for a custom object, but the p3 field might be the record type feild on standard objects as well.

~jm

 

 

ThomasTTThomasTT

Oh, then I missed your point. I didn't try it from javascript, but you can do the same thing with creating the URL and URL parameter by yourself like document.location.href = "/(prefix)/e?nooverride=1&RecordType=xxxxxxxxxxxxxxx&....".

 

ThomasTT

SimplySfdcSimplySfdc

I test p3 is work, but Cancel button not return to initial page, even added retURL.

Marc C.Marc C.
This looks like the syntax for a VF page - it doesn't seem to work in Custom Buttons where you don't have the {!...}. Something like URLFOR($Action.Account.new, null, null) fails.
Nate V.Nate V.

I'm fairly certain that if you are using the hard coded Saleforce ID for your button, then you have to use RecordTypeId.