0

I am a relatively new starter to Salesforce and not quite comfortable coding yet. I just need some advice to point me in the right direction. Specifically my requirements are:

  • Form made available over the WWW
  • Form ultimately populates a custom objects within Salesforce
  • Form includes a Contact Master-Detail relationship field
  • Form will need to validate fields and decide if a new contact is required

I've been reading a lot online but I don't know what the easiest solution, or the best practice is for such forms.

  • Externally written webpage with API calls?
  • Visualforce, apex and custom controller?
  • Flows?
  • Triggers?

My organisation can't make use of the web-to-case functionality as ultimately we use custom objects for almost everything (not a private company so "selling" isn't as important to us).

2
  • Just out of curiousity in case I'm misunderstanding something. Are these forms hosted on an external server? So, as an example, is it a web site built with HTML, CSS, JS etc... If so, what back-end language are you using? PHP for example or .NET maybe?
    – Dan Jones
    Sep 30, 2015 at 11:29
  • Oh, no forms exist as yet - but ultimately I have a few options - I could use one of a few technologies to create the external forms (our main website is .Net driven and we could publish them on there or I could use a few others options) I was hoping to do it all in Salesforce though...I just don't know if a) it can be done and b) how to start.
    – n34_panda
    Sep 30, 2015 at 11:32

1 Answer 1

2

Sticking to the main question:

What is the best way to allow web forms to populate a custom object?

There are a number of ways, but I'll tell you ours. Because of SEO reasons and driving traffic to our site, we use a lot of web forms built within on our site.

Assuming you take this route, you'll want to look at whether you use REST or SOAP. For a form, both can do the same thing. You'll need to decide which is best for you for your requirement, the long story short is:

SOAP is definitely the heavyweight choice for Web service access. It provides the following advantages when compared to REST:

  • Language, platform, and transport independent (REST requires use of HTTP)
  • Works well in distributed enterprise environments (REST assumes direct point-to-point communication)
  • Standardized
  • Provides significant pre-build extensibility in the form of the WS* standards
  • Built-in error handling
  • Automation when used with certain language products

REST is easier to use for the most part and is more flexible. It has the following advantages when compared to SOAP:

  • No expensive tools require to interact with the Web service
  • Smaller learning curve
  • Efficient (SOAP uses XML for all messages, REST can use smaller message formats)
  • Fast (no extensive processing required)
  • Closer to other Web technologies in design philosophy

You can read a more detailed explanation of SOAP vs. REST here.

Salesforce supports both these methods. Now, again purely because my bread and butter is PHP, you can find these REST and SOAP resources. Both are pretty simple with a low learning curve, it's definitely worth playing with them. But there's similar things for JSP, .NET or whatever. This is just what I know but hopefully will push you in the right direction.

Once you've done the DML on the Form itself and populated the Object with a record, whether you use things like Triggers is entirely up to you depending on whether you need something to Trigger when a record is created, or if a value matches a certain criterea. That could be handled using Apex Classes, Triggers, Workflows etc...

Here's an example DML call on SOAP:

$records = array();

$records[0] = new stdclass();
$records[0]->FirstName = 'John';
$records[0]->LastName = 'Smith';
$records[0]->Phone = '(510) 555-5555';
$records[0]->BirthDate = '1957-01-25';

$records[1] = new stdclass();
$records[1]->FirstName = 'Mary';
$records[1]->LastName = 'Jones';
$records[1]->Phone = '(510) 486-9969';
$records[1]->BirthDate = '1977-01-25';

$response = $mySforceConnection->create($records, 'Contact');

$ids = array();
foreach ($response as $i => $result) {
  echo $records[$i]->FirstName . " " . $records[$i]->LastName . " "
  . $records[$i]->Phone . " created with id " . $result->id
  . "<br/>\n";
  array_push($ids, $result->id);
}

And REST:

    function create_account($name, $instance_url, $access_token) {
        $url = "$instance_url/services/data/v20.0/sobjects/Account/";
        $content = json_encode(array("Name" => $name));

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token", "Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
        $json_response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ( $status != 201 ) {
            die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }

        curl_close($curl);

        $response = json_decode($json_response, true);

        $id = $response["id"];

        echo "New record id $id<br/><br/>";

        return $id;
    }

Both are available in the resources I've linked. The point here is that it's actually really simple to do something like this, through your site.

As for mentioning:

Form includes a Contact Master-Detail relationship field

That's doable through this method. You'd probably just want to call a query to get the ID of the person you'd want to assign and marry it up to the field.

Form will need to validate fields and decide if a new contact is required

Again, you could do this on your own web server using JS for client-side validation and a back-end language for server-side validation before it's even sent.

It's also possible to create your own REST class within Salesforce and do the whole thing through Salesforce, sending responses back if something doesn't match, say, a REGEX or a required field is missed although this isn't something I'm too familiar with to give you a good explanation on.

2
  • Thanks for that - I've made use of one of our RAD environments today and it handles SOAP really well so I've progressed quite far. Unfortunately I am using Javascript to handle everything and have got stuck on one part...the "create" method from the SOAP request, I am struggling with how to format/send the request using javascript. I'll keep fiddling until something sticks - thanks for your help.
    – n34_panda
    Oct 1, 2015 at 14:52
  • @n34_panda My pleasure. Glad it helped. One thing with SOAP in Salesforce is that you'll need to download the WSDL files and replace what's already there. The default download only comes with standard SF stuff and no custom stuff. If you go to "Setup > Develop > API" to find these files.
    – Dan Jones
    Oct 1, 2015 at 15:08

You must log in to answer this question.

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