+ Start a Discussion
sparkysparky 

s-control to trigger record creation

Hi, wondering if it's possible to create an s-control that:

* initiates creation of a record (let's say an Opportunity)
* sets the needed foreign keys
* sets the Record Type
* fills in certain fields in the form (amount and date, for example)
* leaves the user in Edit mode with option to complete/commit or cancel

If so, does anyone have an example they could point me to?

I've seen the technique where the record is created & committed via code, then pulled up in edit mode.  But I'd prefer to give control back to the user before the rec is committed.

Sorry if this is old hat - a quick search of the forum didn't turn up the answer.

Thanks!
Matthew
darozdaroz
You may be able to do this by passing a link to the opportunity edit page with the right parameters to hold your data... I don't remember, to be honest, how the record type was set, it might have been a POST variable...

I've probibly got it buried in my notes around here somewhere.
sparkysparky
Well, I'd sure love to see whatever examples you might be able to send my way.
Thanks!
M.


daroz wrote:
You may be able to do this by passing a link to the opportunity edit page with the right parameters to hold your data... I don't remember, to be honest, how the record type was set, it might have been a POST variable...

I've probibly got it buried in my notes around here somewhere.



gokubigokubi
The way I do it is to create the Opp with limited data and then give it to the user in edit mode. I'd be interested in anyone's info on how to pass a lot of data to the create new opp screen without actually creating the opp first.

Here's the code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
Membership Opportunity Creator for Salesforce.com
Copyright (C) 2005 Steve Andersen, steve@onenw.org, ONE/Northwest, 1402 Third Ave, Suite 1000, Seattle, WA, 98101

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
---------------------------------------------------------------------
Description: This S-Control is built to be used as a custom link off of a Contact. It creates a new gift Membership, enforcing a naming convention on the Opportunity, and correctly setting the primary Opportunity Contact Role.
Call the S-Control from a custom link that looks like this:
HYPERLINK("/servlet/servlet.Integration—lid=[S-Control Id]&eid=" & [[!Id]] & "&type=Individual&opptype=Gift", "Create a New Individual Donor Gift") Replace the double brackets with curly braces
Change the type and opptype variables to create different kinds of Opportunities from a Contact.
Last Modified: 4/06/2006-->
<html>
 <head>
  <title>Opportunity creator - Powered by the Sforce AJAX Toolkit (Beta 3.3)</title>
  
  <script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>
  <link href="/css/ie_global.css" rel="stylesheet" type="text/css">
  <link href="/css/ie_navigation.css" rel="stylesheet" type="text/css">
  <link href="/css/opportunities_styles.css" rel="stylesheet" type="text/css">
  
  <script type="text/javascript">
   /****************************************************************
   * Main function called on page load
   *****************************************************************/
   function initPage() { 
    
    //info("Got inside init()");
    //Proof of session when used in salesforce.com interface
     //Initialize the connection to salesforce.com by setting the sessionid and the
                //soap endpoint in the init call
                sforceClient.setLoginUrl("https://www.salesforce.com/services/Soap/u/7.0");
                sforceClient.registerInitCallback(startUp);
                sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}", false);
    //Get the current contact id from the session
            }

   function startUp() {
 
                // Parse the quesry string for the type variable
                var ThisQuerystring = new Querystring(parent.document.URL.substring(parent.document.URL.indexOf('–'), parent.document.URL.length))
                var Type = ThisQuerystring.get("type", "Individual")
                var OppType = ThisQuerystring.get("opptype", "Gift")
                //if type was passed as individual
                if (Type=="Individual") {
                    //OpportunityContactRole role name
        var OpportunityContactRoleName = 'Individual Donor';

                } else {
                    //OpportunityContactRole role name
        var OpportunityContactRoleName = 'Organizational Donor';

                }                
                //Default Opportunity Stage
    var OpportunityStage = "Prospecting";
    //set MonthlyGivingType to noting
    //var MonthlyGivingType = "";
    //CloseDate is today
    var Today = new Date();
    var CloseDate = Today;
                
                if (OppType=="Gift"){
                    //Record type id for the opportunity to be created
        var OpportunityRecordType = '012300000000LvCAAU';
        //Suffix to tack on the name of a created Opportunity
        var OpportunityNameSuffix = 'Contribution';
                } else if (OppType=="Grant") {
                 var OpportunityRecordType = '012300000000UxqAAE';
                 var OpportunityNameSuffix = 'Grant';
                }
   
    
    /****************************************************************
    * Main function called on page load
    *****************************************************************/
    
    var ContactID = "{!Contact_ID}";
    var ContactFirstName = "{!Contact_FirstName}";
    var ContactLastName = "{!Contact_LastName}";
    var AccountID = "{!Account_ID}";
    var AccountName = "{!Account_Name}";
    
    var oppty = new Sforce.Dynabean("Opportunity");
    var opptyArray = new Array(1);
    
    //Set the Recordtype
    oppty.set("RecordTypeId",OpportunityRecordType); 
    //Set the opportunity name based on your naming convention
    if (Type=="Individual") {
        var OpportunityNamePrefix = ContactFirstName + " " + ContactLastName;
    } else {
        var OpportunityNamePrefix = AccountName;
    }
    var OpportunityName = OpportunityNamePrefix + " " + Sforce.Util.FormatDate(Today,"yyyy") + " " + OpportunityNameSuffix;
    oppty.set("Name", OpportunityName);
    //Set the close date to today    
    oppty.set("CloseDate", CloseDate);
    //Set the Opportunity stage to default
    oppty.set("StageName",OpportunityStage);
    //Set the account id as that of the Contact  
    oppty.set("AccountId",AccountID);
    //drop the object into an array
    opptyArray[0] = oppty;   
    //Call Update on the Opportunity.
    var OpportunitySaveResult = sforceClient.Create(opptyArray);
         
    if (OpportunitySaveResult[0].success == true) {
     
     //get the object that was returned
     CreatedOpportunity = OpportunitySaveResult[0];
     //create the object for opportunity contact role
     var OpportunityContactRoleSaveResult = SetContactRole(CreatedOpportunity.id,ContactID,OpportunityContactRoleName);
     
     if (OpportunityContactRoleSaveResult[0].success == true) {
      top.location.replace("/" + CreatedOpportunity.id + "/e˜retURL=/" + CreatedOpportunity.id); 
     } else {
      alert("Error! Did you change the OpportunityRecordType variable to match your salesforce.com instance™");
     }     
    } else {
     alert("Error!" + OpportunitySaveResult[0].message);
    }  
   }
  //This is a function to create Opportunity Contact Roles for a known Opp, Contact, and Role Name
  function SetContactRole(CreatedOpportunityId,ContactID,OpportunityContactRoleName){
   //create the object for opportunity contact role
   var ContactRole = new Sforce.Dynabean('OpportunityContactRole');
   //set the id to match the new opportunity
   ContactRole.set("OpportunityId",CreatedOpportunity.id);
   //set the contact
   ContactRole.set("ContactId",ContactID);
   //set the contact role to the default
   ContactRole.set("Role",OpportunityContactRoleName);
   //Make it primary
   ContactRole.set("IsPrimary",1);
   //create an array to pass it to Update
   var contactRoleArray = new Array(1);
   contactRoleArray[0] = ContactRole;
   //Update it
   return sforceClient.Create(contactRoleArray);
  }
  
/* Client-side access to querystring name=value pairs
 Version 1.2.3
 22 Jun 2005
 Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
 this.params = new Object()
 this.get=Querystring_get
 
 if (qs == null)
  qs=location.search.substring(1,location.search.length)

 if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
 qs = qs.replace(/\+/g, ' ')
 var args = qs.split('&') // parse out name/value pairs separated via &
 
// split out each name=value pair
 for (var i=0;i<args.length;i++) {
  var value;
  var pair = args[i].split('=')
  var name = unescape(pair[0])

  if (pair.length == 2)
   value = unescape(pair[1])
  else
   value = name
  
  this.params[name] = value
 }
}

function Querystring_get(key, default_) {
 // This silly looking line changes UNDEFINED to NULL
 if (default_ == null) default_ = null;
 
 var value=this.params[key]
 if (value==null) value=default_;
 
 return value
}

</script>

 </head>
 <body onLoad="initPage();">
 
 </body>

</html>

 

Message Edited by gokubi on 04-13-2006 02:53 PM

Ron HessRon Hess
You can see how this is done by going to the object you want to build, click Edit, then ( in firefox) right click on the page, use "View Page Info" ,
then go to the Forms tab and you will see all the name and value pairs that the form would like to have to work properly.

you then construct a URL with these ( all the ones you want) lumped together with "&"

so you end up with /003/e?name=value&name2=value&...

the recordtype id is just another name=value pair.

to get it to "save" , i think you just add "save=1" to the end.

hope this helps get you started.  
sparkysparky
Thanks, Ron!  I'll give it a whirl.

M.
bcallumbcallum

Ron - do you know if the save=1 attribute still works?

 

Thank you,

Brendan