+ Start a Discussion
kevinedelmannkevinedelmann 

Help Upgrading an S-Control

I don't have much Java/programing experience and I need some help to upgrade two s-controls that were created last year.

What would be the best way to go about getting help for this?  I can post the s-control if need be, but didn't want to clog things up at first.

Thanks,

Kevin
KaushikKaushik

 Hi,
Please post it.This would give us a better idea.

Kaushik
kevinedelmannkevinedelmann
The s-control is below.  The s-control takes information from the Opportunity page and a custom child object and creates a case.  It populates the case requestor from the Primary Contact Role related list. 
 
What I want to do is populate the Account name of the case with the opportunity account name.  Currently it populates it with the account name of the primary contact in the contact related list which does not always equal the opportunity account.
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Create RFR Case</title>
<script language="javascript" src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js?browser=true" type="text/javascript"></script>
<script id="clientEventHandlersJS" language="javascript">
<!--

function initPage() {
sforceClient.registerInitCallback(setup);
sforceClient.setLoginUrl("https://www.salesforce.com/services/Soap/u/7.0");
sforceClient.init("{!API.Session_ID}", "{!API.Partner_Server_URL_70}", true);
}

//begin processing
function setup() {
//for debugging
//displayInfo();

//get relevant info from opportunity record
displayWorkingImg();
getAccount();
//displayInfo();
}

//display 'working' image while s-control is processing data
function displayWorkingImg() {
var html = "<img src='" + workingUrl +"'>";
document.getElementById("divInfo").innerHTML = html;
}

//display 'Successful' image if submission was successful
function displayCompleteImg() {
var html = "<img src='" + completeUrl +"'>";
document.getElementById("divInfo").innerHTML = html;
}

//display 'UNsuccessful' image if submission did not go through
function displayINcompleteImg() {
var html = "<img src='" + incompleteUrl +"'>";
document.getElementById("divInfo").innerHTML = html;
}

//displays asset, account, contact, and product type info
function displayInfo() {
var html = "<table width=50%>";
html += "<tr><td width=25% nowrap valign=top class='dataLabel'>Account ID:</td>";
html += "<td class='dataField'>" + accId + "</td></tr>";
html += "<tr><td width=25% nowrap valign=top class='dataLabel'>Opportunity ID:</td>";
html += "<td class='dataField'>" + oppId + "</td></tr>";
html += "<tr><td width=25% nowrap valign=top class='dataLabel'>Contact ID:</td>";
html += "<td class='dataField'>" + conId + "</td></tr>";
for (rfrPos in rfrIds)
{
html += "<tr><td width=25% nowrap valign=top class='dataLabel'>RFR ID[" + rfrPos + "]:</td>";
html += "<td class='dataField'>" + rfrIds[rfrPos] + "</td></tr>";
}
html += "</table>";
document.getElementById("divInfo").innerHTML = html;
}

//get account info for case description
function getAccount() {
//query sforce for account for this Opp
var queryResult = sforceClient.Query("Select BillingState, Client_Number__c, County__c, Name, OwnerId, Sub_Client__c from Account where Id ='" + accId + "'");
if (queryResult.size > 0) {
groupName = queryResult.records[0].get("Name");
clientNum = queryResult.records[0].get("Client_Number__c")+"-"+queryResult.records[0].get("Sub_Client__c");
county = queryResult.records[0].get("County__c");
//get account owner (salesrep) name
getAccountOwner(queryResult.records[0].get("OwnerId"));
} else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: No parent account exists for this opportunity!");
//return to opp page
frameNav(oppUrl);
}
}

//get account owner info for case description
function getAccountOwner(Id) {
//query sforce for account owner name
var queryResult = sforceClient.Query("Select FirstName, LastName from User where Id ='" + Id + "'");
if (queryResult.size > 0) {
salesRep = queryResult.records[0].get("FirstName")+" "+queryResult.records[0].get("LastName");
}
getContact();
}

//get primary contact on opp - use later when creating case
function getContact() {
//query sforce for primary Contact Id for this Opp
var queryResult = sforceClient.Query("Select ContactId from OpportunityContactRole where IsPrimary = True AND OpportunityId ='" + oppId + "'");
if (queryResult.size > 0) {
//1 primary contact returned - good to go
if (queryResult.records.length==1) {
conId = queryResult.records[0].get("contactid");
getRFRs();
}
//more than one primary contact - should never happen
else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: More than one primary contact exists for this opportunity!");
//return to opp page
frameNav(oppUrl);
}
}
//no primary contact exists
else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: No primary contact exists for this opportunity. Please specify a primary contact using the Contact Roles related list before resubmitting the RFRs.");
//return to opp page
frameNav(oppUrl);
}
}

//get rfrs from opp where stage = Working On
function getRFRs() {
//query sforce for rfr ids
var queryResult = sforceClient.Query("Select Id, Name, RFR_Name__c, Type__c, Format__c, Proposal_Due_Date__c, Rates_needed_by__c, Part_of_Existing_Group__c, State__c from Quotes_RFR__c where Stage__c = 'Working On' AND Opportunity__c ='" + oppId + "'");

//loop through results
if (queryResult.size > 0) {
//rfrs returned - good to go
requiredBy=queryResult.records[0].get("Proposal_Due_Date__c");
partofExisting= queryResult.records[0].get("Part_of_Existing_Group__c")
workType = queryResult.records[0].get("Type__c")+" - "+queryResult.records[0].get("Format__c");
proposalDueDate = queryResult.records[0].get("Proposal_Due_Date__c");
ratesNeededBy = queryResult.records[0].get("Rates_needed_by__c");
state = queryResult.records[0].get("State__c");

for (i=0;i<queryResult.size;i++)
{
rfrIds[i]=queryResult.records[i].get("Id");
//alert("rfrIds["+i+"]"+rfrIds[i]);
correspondingRFRs+=queryResult.records[i].get("Name")+"; ";
}
createCase();
}
//no rfr exists
else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: No unsubmitted RFRs exist for this opportunity. RFR stage must be Working On to be eligible for submission. Please create at least one RFR using the Quotes/RFRs related list or update the RFR stage(s) before resubmitting.");
//return to opp page
frameNav(oppUrl);
}
}

//create case
function createCase() {
//check that all required variables are set
if (conId != "" && !isNaN(parseFloat(expectedEnrollment.replace(",","")))) {
//update case description
caseDescription = "Group Name: "+groupName;
caseDescription += "\nClient Number: "+clientNum;
caseDescription += "\nState: "+state;
caseDescription += "\nCounty: "+county;
caseDescription += "\nSize: "+size;
caseDescription += "\nExisting Group: "+partofExisting;
caseDescription += "\nWork Type: "+workType;
caseDescription += "\nSales Rep: "+salesRep;
caseDescription += "\nEffective Date: "+effectiveDate;
caseDescription += "\n\nCorresponding RFRs: "+correspondingRFRs;

//create case dynabean
var _case = new Sforce.Dynabean("case");
_case.set("ContactId", conId);
_case.set("OwnerId", gaQueueId);
_case.set("RecordTypeId", rfrCaseRecordTypeId);
_case.set("Rates_Needed_By__c", ratesNeededBy);
_case.set("Required_By__c", requiredBy);
_case.set("Origin", origin);
_case.set("Expected_Enrollment__c", parseFloat(expectedEnrollment.replace(",","")));
_case.set("Subject", caseSubject);
_case.set("Description", caseDescription);

//create new case
var saveResult = sforceClient.Create([_case]);

//for debugging
//alert("Here is the case save result:"+saveResult.toString());

for (var i=0;i<saveResult.length;i++) {
var oneSaveResult = saveResult[i];
if (oneSaveResult.success == true) {
//for debugging
//alert("The new id is: " + oneSaveResult.id);

//navigate to new Case Record
//var caseUrl = "/" + oneSaveResult.id + "/e?retURL=%2F" + oneSaveResult.id;
caseId = oneSaveResult.id;
caseUrl = "/" + caseId;
//frameNav(caseUrl);
} else {
alert("Case creation did not succeed: " + oneSaveResult.errors[0].message);
//return to opp page
frameNav(oppUrl);
}
}
createRfrCase();
}
else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: Information required to create the Case is missing. Please ensure the primary contact and total expected enrollment have been specified for this opportunity.");
//return to opp page
frameNav(oppUrl);
}
}

//create rfr case relationship
function createRfrCase() {
//check that all required variables are set
if (caseId != "") {
for (curPos in rfrIds)
{
//create rfr case dynabean
var rfr_case__c = new Sforce.Dynabean("rfr_case__c");
rfr_case__c.set("Case__c", caseId);
rfr_case__c.set("RFR__c", rfrIds[curPos]);

//create new rfr case
var saveResult = sforceClient.Create([rfr_case__c]);

//for debugging
//alert("Here is the rfr case save result:"+saveResult.toString());

for (var i=0;i<saveResult.length;i++) {
var oneSaveResult = saveResult[i];
if (oneSaveResult.success == true) {
//for debugging
//alert("The new id is: " + oneSaveResult.id);
} else {
alert("RFR Case creation did not succeed: " + oneSaveResult.errors[0].message);
//return to opp page
frameNav(oppUrl);
}
}
}
updateRfrs();
}
else {

//display ERROR image
displayINcompleteImg() ;

alert("Error: Information required to create the RFR Case relationship is missing.");
//return to opp page
frameNav(oppUrl);
}
}

//update rfr stage to Submitted to UW
function updateRfrs() {
for (cPos in rfrIds)
{
//create rfr case dynabean
var rfr_case__c = new Sforce.Dynabean("quotes_rfr__c");
rfr_case__c.set("Id", rfrIds[cPos]);
rfr_case__c.set("Stage__c", "Submitted to UW");
//rfr_case__c.set("RecordTypeId", rfrLockedId);

//update rfr
var saveResult = sforceClient.Update([rfr_case__c]);

//for debugging
//alert("Here is the rfr save result:"+saveResult.toString());

for (var i=0;i<saveResult.length;i++) {
var oneSaveResult = saveResult[i];
if (oneSaveResult.success == true) {

} else {

//call to error image here

//display ERROR image
displayINcompleteImg() ;

alert("RFR update did not succeed: " + oneSaveResult.errors[0].message);

}
}
}

//display image
displayCompleteImg() ;

//for debugging
//alert("The new id is: " + oneSaveResult.id);

//inform user of success
alert("Click OK to go back to the opportunity!");


frameNav(oppUrl);
}

//trim function
function trim(string) {
return string.replace(/(^\xA0*)|(^\s*)|(\s*$)/g,'');
}

//handle navigation from inside frame
function frameNav(url)
{
window.parent.parent.location.href = url;
}

var accId = "{!Opportunity.AccountId}";
var oppId = "{!Opportunity.Id}";
var oppUrl = "/" + oppId;
var rfrIds = new Array();
var rfrCaseRecordTypeId = "012300000004wxmAAA";
var gaQueueId = "00G300000015gZ0EAI";
var rfrLockedId = "012300000004xCwAAI"; //Dental Locked RT ID
var conId = "";
var caseId = "";
var caseUrl = "/" + caseId;
var requiredBy = "";
var origin = "Internal";
var expectedEnrollment = "{!Opportunity.Total_Expected_Enroll__c}";
var caseSubject = "RFP/RFR Info - {!Opportunity.Name}";
var caseDescription = "";
var workingUrl = "/servlet/servlet.FileDownload?file=01530000000BDeT";
var completeUrl = "/servlet/servlet.FileDownload?file=01530000000BW3H";
var incompleteUrl = "servlet/servlet.FileDownload?file=01530000000BXK4";


//variables needed to update case description
var groupName = "";
var clientNum = "";
var state = "";
var county = "";
var size = "{!Opportunity.Total_Expected_Enroll__c}";
var workType = "";
var partofExisting = "";
var proposalDueDate = "";
var ratesNeededBy = "";
var salesRep = "";
var effectiveDate = "{!Opportunity.Effective_Date__c}";
var correspondingRFRs = "";

//-->
</script>
</head>
<body onLoad="initPage()">
<div id="divInfo" style="WIDTH: 600px; POSITION: relative; HEIGHT: 96px" ms_positioning="GridLayout"></div>
</body>
</html>
KaushikKaushik
I assume that this Scontrol exists on the Opportunity page.
Here in your code,the account Id in the case has not been populated/assigned. So according to the Salesforce API rules it  takes the Account    ID of the contact by default.The additional line of code required here is to assign the account Id.
so change the code of //create case dynabean section to

//create case dynabean
var _case = new Sforce.Dynabean("case");
_case.set("AccountId", accId);
_case.set("ContactId", conId);
_case.set("OwnerId", gaQueueId);
_case.set("RecordTypeId", rfrCaseRecordTypeId);
_case.set("Rates_Needed_By__c", ratesNeededBy);
_case.set("Required_By__c", requiredBy);
_case.set("Origin", origin);
_case.set("Expected_Enrollment__c", parseFloat(expectedEnrollment.replace(",","")));
_case.set("Subject", caseSubject);
_case.set("Description", caseDescription);


This  should most probably work.The other way might be to query the Account Id.Lets see and post it back if you have any problem

Kaushik
kevinedelmannkevinedelmann
Unfortunately that does not work, I have tried it.  I think it has to do with the ajax version beta3.3 and think it does not allow accountID to be used as it was a valid call in June of 06.
kevinedelmannkevinedelmann
Forgot to say this in the post.  Thanks for your help I do appreciate it.  :)
KaushikKaushik
    You are welcome.
Also try Account.Id instead of AccountId
I just went through the Apex API manuals..might be the rules are different for the Ajax toolkit.I quote the following text from it.
It is on page 254, apex api 9.0 document

Separating Accounts from Contacts in Cases
In releases before 8.0, the AccountId could not be specified, it was derived from the contact’s account. This behavior will continue to be supported in future releases, but you can also now specify an AccountId. If you do not specify the AccountId during the creation of a case, the value will default to the contact’s AccountId. Note: When a record is updated, if the ContactId has not changed, then the AccountId is not regenerated. This prevents the API from overwriting a value previously changed in the Salesforce user interface. However, if an API call changes the ContactId and the AccountId field is empty, then the AccountId is generated using the contact’s account

I am not sure whether this applies to the AJAX toolkit.Even in the setup->case->fields->account Name it has been mentioned that it can be editable

Hope you are able to solve this

Kaushik

Message Edited by Kaushik on 06-21-2007 02:31 PM

Message Edited by Kaushik on 06-21-2007 02:40 PM