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
MKPartners.comMKPartners.com 

S-Control: Create Contact from Case submitted via Web

Has anyone created an S-Control that will take the information submitted via Web-to-Case and create a Contact and then relate the Case to that Contact?
 
This need must be common, so I'm hoping someone already has some code to leverage.

Thanks in advance.
 
-Matt
MKPartners.comMKPartners.com
Nevermind, I created it myself.
michaelforcemichaelforce

Matt,

I'm curious how you handled this.  It is easy enough to use the AJAX toolkit to add code which will do this to the end of your webform, but in that case you have to have login details hard coded into the form... which isn't very secure unless you set up a special profile / user account just for this purpose.

Is that how you handled it?

-Michael

MKPartners.comMKPartners.com
Actually, I created two s-controls that can be launched from the case via formula buttons.
 
The first s-control creates a contact from web-to-case fields and then populates the Case's ContactId with the new contact's id
The formula button is only displayed when there is no ContactId in the case
 
The second s-control creates an asset from web-to-case fields and populates the Case's AssetId with the new asset's id
The formula button is onlhy displayed when there is no AssetId on the case and when custom model/serial# fields are populated
 
This is not an automated solution, but rather, requires the end user to take action.  Ideally, all of this would be done automatically behind the scenes, but that was out of scope for this project.
Again AgainAgain Again
I did this something like this by not using the web-to-case html provided by sf but by creating my own web-to-case using asp.net. This allowed me to do some additional validation against values in other databases - and I could do a search for the account/contact, and create them in SF if they didn't exist, or just hook them to the case if they did already exist. Total dev time wasn't much longer than it would have taken to just add javascript validations for the different fields on the web-to-case form.
--brooke----brooke--
<update> fixed the code to have better debug messaging and functionality -- working well for me, best of luck to everyone else! I Still can't believe salesforce missed the mark so badly with Web to Case (Web2Case). It's clear that if a contact doesn't exist, there should be an option to auto create one to make it easy to field customer support. That, or the Send an Email link should allow sending messages to the web contact info. The self service customer support flow is extremely poor.</update>
 
<update2>added matching for existing contacts since sales force only tries to match on email when the case is created (with sql injection protection)</update2>
 
I like the original idea you had for the post when you wanted something that was free... so here's something for everyone who comes a look'n can use.
 
(no guarantees on it working, use at your own risk, etc, etc...)
 
just add this as a custom s-control, then add it to the case view as a field. I put it in as 100% x 25px in the second column of the case detail so it's right nexto where the contact is filled in, the UI is pretty clean, the page just flashes after a second with IE, and doesn't even flash with FireFox.
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Update Case</title>
<script src="/soap/ajax/13.0/connection.js" type="text/javascript"></script>
<script language="javascript">
function initPage()
{
    window.setTimeout(createContactForCase, 200);
}
function createContactForCase()
{
    try
    {
        if (parent.location.href.indexOf("_auto_create_contact_=1") > 0)
        {
            document.getElementById('_auto_create_contact_').innerHTML = "<center>contact populated</center>";
        }
        else if ('{!Case.ContactId}' != '')
        {
            document.getElementById('_auto_create_contact_').innerHTML = "<center>contact exists</center>";
        }
        else
        {
            var contactId = 0;
            //find existing contact
            var emailString = "{!Case.SuppliedEmail}";
            var result = sforce.connection.query("SELECT Id FROM Contact WHERE Email='" + emailString.replace(/'/g,"%27") + "'");
            if (result && result.records && result.records.length > 0)
            {
                contactId = result.records[0].Id;
            }
            //create new contact
            if (contactId == 0)
            {
                var newContact = new sforce.SObject("Contact");
                newContact.set("Email", "{!Case.SuppliedEmail}");
                newContact.set("FirstName", " ");
                newContact.set("LastName", "{!Case.SuppliedName} ");
                newContact.set("Phone", "{!Case.SuppliedPhone}");
                var saveResult = sforce.connection.create([newContact])[0];
                if (!saveResult || saveResult.success == 'false')
                {
                    document.getElementById('_auto_create_contact_').innerHTML = "create contact error: " + saveResult;
                    return;
                }
                contactId = saveResult.id;
            }
            var thisCase = new sforce.SObject("Case");
            thisCase.set("Id","{!Case.Id}");
            thisCase.set("ContactId", contactId);
            saveResult = sforce.connection.update([thisCase])[0];
            if (!saveResult || saveResult.success == 'false')
            {
                document.getElementById('_auto_create_contact_').innerHTML = "update case error: " + saveResult;
                return;
            }
  
            //update ui and reload
            document.getElementById('_auto_create_contact_').innerHTML = "<center>contact created, reloading...</center>";
            window.setTimeout(reloadPage, 200);
        }
    }
    catch (error)
    {
        document.getElementById('_auto_create_contact_').innerHTML = "unable to add contact - error: "+error; //.faultstring;
    }
}
function reloadPage()
{
    if (parent.location.href.indexOf("?") < 0)
        parent.location.replace(parent.location.href + "?_auto_create_contact_=1");
    else
        parent.location.replace(parent.location.href + "&_auto_create_contact_=1");
}
</script>
</head>
</script>
</head>
<body onload="initPage();">
<div id='_auto_create_contact_'>
<center>
Updating contact ... &nbsp; &nbsp;<img src="/img/waiting_dots.gif" border="0" width=100 height=15>
</center>
</div>
</body>
</html>


Message Edited by --brooke-- on 08-12-2008 01:34 PM

Message Edited by --brooke-- on 08-20-2008 05:56 PM
UmapenUmapen

Does any know whether this S-control will have governor Limit related problem?

 

I wrote a trigger to do the same as this S-control update case with contactId when

case.supplied email == Contact.email

 

Update case.contactid = contact.Id

 

But I am getting the following error

System.DmlException: Insert failed. First exception on row 0;
caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
 

I want to use this S-Control, 

Is there any limitation on how many cases it can process at a time?

Did any one see errors related to Governor limts using this S-control

 

I appreciate your comments/suggestions.

Thanks