+ Start a Discussion

Help making S-Control more robust (code included)

Hello Folks

I am very new to this, but was able to cobble together an S-Control to acommplish the following:

Case Roles are very powerful, but often underutilized. Make a button that ...
  1. Creates a new Case
  2. Populates the accountId, ContactId, Status, and Origin fields of the new Case with information from the current Contact (or Person Account)
  3. Creates a new Case Role with the current Contact (or Person Account)
  4. Navigates to the edit Case screen
Ideally, we would use Apex Code for this, but we are a non profit with donated licenses (enterprise)

What can I do to make this more robust, catch errors, etc? Any critiques would be helpful (and may help others)

function makeCaseAndRolls(Role) {
 var c = new sforce.SObject("Case");
 var account = "{!Account.Id}";

 if ({!Account.IsPersonAccount}==true) {

   // Ids argument for retrieve only takes an array, so we create an array called accounts
   var accounts=new Array()
   accounts[0] = account

   // Use retrieve to get the Account object, so we can find its PersonContactID
   result = sforce.connection.retrieve("PersonContactId,Id", "Account", accounts)

   var contact = result[0].PersonContactId;

   } else {
     var contact = "{!Contact.Id}"

 // Set field values
 c.AccountId = account;
 c.ContactId = contact;
 c.Status = "New";
 c.Origin = "Phone";

 var caseResult = sforce.connection.create([c]);

 var cr = new sforce.SObject("CaseContactRole");

 // Set field values for Case Role
 cr.ContactId = contact
 cr.Role = Role;
 cr.CasesId = caseResult[0].id;

 // Create Role
 var roleResult = sforce.connection.create([cr]);

 // Redirect
 window.parent.location.href = "/" + caseResult[0].id + "/e—retURL=%2F" + caseResult[0].id