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
ssurferssurfer 

Java or HTML code.... which way to go and why?

For about a week i've been trying to solve a problem with pre-populating a record of a custom object linked to an opportunity. Following the information i've gotten from helpful people here and what i've read, i can see that the solution could be one of two: either i build a java s-control or i build out an HTML code. My question here is what to choose when and why?
 
I have pasted in the two solutions (the java version still not functionning and in it's most basic format, that in theory should only open up the custom object)
 
My impression is that the HTML way is a "cheat" and the Java is the "real" way to go, especially having Apex in mind. Could anyone please enlighten me?
 
Best regards,
Johan
 
 
/setup/ui/recordtypeselect.jsp?retURL=
%2{!Opportunity.Id}&save_new_url=%2Fa0J%2Fe%3FretURL
%3D%25{!Opportunity.AccountId}%26CF00N2000000129Zf
%3D{!Opportunity.Account}%26CF00N2000000129Zf_lkid
%3D{!Opportunity.AccountId}%2600N2000000129Zk
%3D{!Opportunity.Survey_Address__c}%2600N200000012ANk
%3D{!Opportunity.Pack_Date_1__c}&ent=01I200000004VkE
++++++++++++++++
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script language="javascript">

<script>

function setupPage() {

createWork_Order();

}



function createWork_Order() {

var wo = new sforce.SObject("Work_Order__c");
}
try {


saveResult = sforce.connection.create([wo]);

}catch(error) {

alert("got an error: " + error);

}


if (result[0].getBoolean("success")) {
alert("new Work Order created with id " + result[0].id);
} else {
alert("failed to create Work Order " + result[0]);
}



</script>
</head>
<body onload="setupPage();"></body>
</html>
Greg HGreg H
My opinion on which way is better?  It depends on the business need.
 
Now the real reason for my post - I want to assist you with the problem you are encountering with your first example.
 
The primary reason it is breaking is because of the "/setup/ui/recordtypeselect.jsp" piece of your code.  Basically, in order to get to the edit screen for your custom object you need to replace that part of the code with the specific 'front door' url for your custom object.  You can force the recordtype selection as a parameter later in your URL string.  Since I don't know the custom objects for your org and since some other people may want to reference this to learn how they can apply this information to their org - I will use a more practical example.
 
I know you want to pre-populate some fields on your custom object but for my example I am going to pre-populate some fields on the contacts object.  Now the contacts object (like all salesforce objects) has what salesforce sometimes refers to as a front door for the URL.  Quite simply the front door for the contacts tab is "/003/o".  The front door for a new contact would be "/003/e".  If you want to see what the front door is for any object simply click the tab for the object.  The front door is the three characters directly following the ".salesforce.com/" portion of the URL while you are on the tab's "home" screen.  Further examples; Opportunities home is "/006/o", Accounts home is "/001/o".  If you want to add a new record for the object you simply change the "o" portion of the front door to an "e".  I know I wrote that the front door was three characters but I think you get my drift here.
 
So now what?  Well, if you need to pass information to a new record (regardless of custom or standard object) you simply add additional parameters to the URL following the front door portion of the string.  Each parameter must be separated by an ampersand (&) except for the first parameter which must use a question mark (?).  To find out what parameters can be sent in the URL for your object you can either use Firefox with a developer plug-in to view the form details on the edit screen of your object or you can use any browser and simply view the source code of the edit screen.  Each editable field on the screen has an identifier and a name.  For example on the contacts edit screen the salutation field part of the form has the name and id of "name_salutationcon2".
 
Using this information we can now add a new contact using nothing more than parameters passed in the URL.  To finish my example I am going to pre-populate a new contact record with a first name, last name, salutation, email address and description.  Please note that since we are passing parameters using the URL any special characters you want to use in a value field will need to be encoded so that they can be properly interpreted by the browser.  A good reference for url encoding is W3Schools (http://www.w3schools.com/tags/ref_urlencode.asp).
 
Breakdown of URL:
  • Front Door for new contact record: /003/e
  • Salutation for new contact: ?name_salutationcon2=Ms%2e (question mark is to denote that this is the first parameter in the URL; "%2e" is the encoded value for a period ".")
  • First Name for new contact: &name_firstcon2=Tess (ampersand "&" separates this parameter from the first parameter; "name_firstcon2" is the form id for the first name; equal sign "=" separates the form id from the form value; "Tess" is the value that I want the first name to be)
  • Last Name for new contact: &name_lastcon2=Dachshund
  • Email for new contact: &con15=greg%40interactiveties%2ecom (jumbled because of encoding)
  • Description for new contact: &con20=I%20like%20my%20dog (jumbled because of encoding)
 
Putting it all together:
Code:
/003/e?name_salutationcon2=Ms%2e&name_firstcon2=Tess&name_lastcon2=Dachshund&con15=greg%40interactiveties%2ecom&con20=I%20like%20my%20dog

Hopefully, most people reading this are using contacts in their orgs and, therefore, should be able to cut and paste the string from my example into a browser and see exactly what I'm trying to illustrate.
 
Now there is a lot more that can be done using this information.  You could add code similar to this to a custom button or custom link and use values from another object to pass in the URL to your new record (like apex generated values such as {!Opportunity.Id} to link your new record to a specific Opportunity).  In addition there are some specific parameters that can be added to the URL (like "retURL") to make the functionality more robust or even pass a specific parameter to save the new record without displaying the new record screen.  However, the information contained in this post should be enough to at least get you pointed in the correct direction.
 
My bad on the long post but hopefully it can be useful,
-greg
ssurferssurfer

Thank you Greg,

I've modified the code a bit and now it works and looks like this:

Code:

/a0J/e—CF00N2000000129Zf={!Opportunity.Name}
&CF00N2000000129Zf_lkid={!Opportunity.Id}
&00N200000012QPd={!Opportunity.Sales_Person_Remark__c}
&00N200000012PDu={!Opportunity.Contact_person_tel__c}
&00N200000012PDk={!Opportunity.Contact_person__c}
&00N2000000129Zk={!Opportunity.Survey_Address__c}
&00N200000012QPT={!Opportunity.Destination_Address__c}
&00N200000012ANk={!Opportunity.Pack_Date_1__c}
&retURL=%2F{!Opportunity.Id}
&cancelURL=%2F{!Opportunity.Id}
&ent=01I200000004VkE


 

I'm still trying to solve it with a java script though, but as a temporary solutions this works ok. It also seems to bypass the record type selection so i've made the record type available for change on the Work_Order object.

I found that the Mozilla Firebug was extremely useful when finding the fields in the code. Basically it lets me just mark a field on any object, right-click and choose inspect and it will give me the ID (like 00N2000000023QPd) of that specific object. It makes it very much faster to write this thing.