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
Again AgainAgain Again 

To choose APEX or SControl?

Hi,
 
I'm looking to implement a new page/view/integrated report and I'm not sure whether I should be going with an S-Control or Apex Code.  I have limited experience with S-Controls as they were before the winter release (have written some to display data from different recordsets and update this).
 
In theory, I could create this page as a back-end report (have some replication in place and user SQL reporting services), but I don't want to burden my server with replicating the different objects at a very high frequency. This is why I'd like to create this solution inside Salesforce.
 
The scenario: need to display selected data for a custom object related to a case. Basically, I have case, account, check requests (related to case) and complaints (related to case). For a given check request, I will want to show some case info, some account info, the complaints for the parent case, and a list of the attachments for the parent case.
 
I think I can do this with an s-control - a bunch of queries and displays. However, I think the s-control will be a little slow. My other s-controls got slow if I was pulling 3 or 4 different queries. Also not sure if I'll run into any problems with joins.
 
Not having done any APEX coding, I'm not sure if it's intended for a problem like this. Can it do the same thing as the s-control would, but faster?
 
I'll appreciate any guidance you can give.
 
Thanks
Rozanne
Doug ChasmanDoug Chasman
Rozanne,
 
One of the exciting new features of the salesforce.com 8.0 api (exposed via the ajax toolkit for scontrols) is support for relationship queries. Basically you can retrieve a set of objects and their related objects in the same soql query. This may be all you need to solve your problem or you might want to think about the "power of AND" - scontrols and apex code work naturally together to complement each other. You can invoke an apex code method from a package and that method will run server side (apex code is a server technology only at this time) returning the results to your browser/javascript contained in your scontrol where you can handle handle presentation/rendering...
 
Doug
Again AgainAgain Again
Thanks so much for the quick reply! So it looks like you're saying, start out with an s-control, and if you get brave, you can add some extra functionality with APEX.
 
I could find an Apex Code Reference - is there a code reference for S-Controls with Ajax 8.0 that you could perhaps point me to?  I'm not just lazy, just really lost, trying to navigate through the available resources on the site. There's an AJAX Toolkit and S-controls discussion section. From here I try the "Getting Started" link - but these are all for Apex.  I downloaded the Apex Code Lang. Reference thinking they maybe put everything together ... but they didn't. I'm sure it's around here somewhere (before, there was a section where one downloaded Eclipse, and then there were 2 famous blog entries that had s-control samples, which is what I used before the winter release. I looks like some syntax etc. has changed so I don't just want to rely on those.
 
Thanks!
Rozanne
Again AgainAgain Again
Maybe this https://wiki.apexdevnet.com/index.php/API  the AJAX section is it.
Again AgainAgain Again

https://na4.salesforce.com/servlet/servlet.Integration?lid=01r500000009Upd&ic=1 

Was linked to from AJAX Tools 0.8.4 - not sure where it is in site navigation setup, but I'll take what I can get :-)

Again AgainAgain Again

I've made a little progress (very little!) based on the docs I've been working with (mostly  the AJAX toolkit dev guide and the "Relationship Queries" chapter of the API doc.

So, I have the following snippet:

select cr.Id, cr.Taxes__c, Case__r.Subject, Case__r.Status FROM Check_Request__c cr Limit 20

and this returns a checkrequest record, and what seems like a related list for the case object. The above query runs correctly.

I now output the values, but can't figure out the syntax to show the (parent Case)'s subject or status.

 for (var i = 0; i < records.length; i++) {
        var checkrequest  = records[i];   // no error
        var parentcase = checkrequests__r;  // no error thrown, but I'm not actually sure
        var subject = parentcase(0).Subject;  //  a guess - assuming the case__r relationship is being returned as another array, maybe i need to loop through these also??

   //somehow need to get the nested case values.
   //String fname = bbb.getFirstname();  - ?? doesn't work
   
   output += checkrequest.Id + "<br> " + subject; // id displays, but subject says [object error] on the display (at least no javascript error)
      

      }

There is another example, where they'll use syntax like this:

Contact contact = (Contact)qr.getRecords(i) but this is used with a binding and whathaveyou, which the other example set I'm following doesn't have.

Could someone please help me out of my murky misery??

Again AgainAgain Again

Hurray ... a little more

var it = new sforce.QueryResultIterator(queryResult);
while(it.hasNext())
{
var record = it.next();
var CaseSubject = record.Case ? record.Case.Status : null;
output += "<LI>" + record.Taxes__c + " " + CaseSubject;
}

Only ... both display null instead of the value.

cheenathcheenath
For this query :

select cr.Id, cr.Taxes__c, Case__r.Subject, Case__r.Status FROM Check_Request__c cr Limit 20

You can access the fields like this:

var it = new sforce.QueryResultIterator(queryResult);
while(it.hasNext())
{
var record = it.next();
  alert("Result " + record.Id + record.Taxes__c + record.Case__r.Subject +
      record.Case__r.Status);
}


You can just alert(record) and see what all fields/child objects it got.




Again AgainAgain Again

That's very useful, thank you! I can see that the recordset does return the value I expect, so I think it's maybe a syntax issue? 

a: var ComplaintName = record.Complaint__c ? record.Complaint__c.Id : null;
b: var ComplaintName = record.Complaint__c ? Id : null;

what is the : null for?

The guide uses line (a), but in my scenario this writes out the word "null".

Extract of the recordset returned below:

{type:'Complaint__c',
Id:'a0760000000t3ukAAA',
Component_Group__c:'Transmission / Transaxle',

Complaint__r:{type:'Complaint_Causes__c',
Id:'a0F300000008Zo6EAE',
Name:'Transmission',
},
Cause__r:null,
Cause_Description__c:null,
Labor_Code__r:null,
Correction_Description__c:null,
Case__r:{type:'Case', Id:'50060000002hcs1AAA',
 },
 }

Component Group Transmission / Transaxle
Subcomponent Automatic Transmission

{type:'Complaint__c',
Id:'a0760000000tFIZAA2',
Component_Group__c:'Transmission / Transaxle',
Sub_Component__c:'Automatic Transmission',
Complaint__r:{type:'Complaint_Causes__c',
Id:'a0F300000008Zo6EAE', Name:'Transmission',
 },

Cause__r:null,
Cause_Description__c:'Vehicle moved erratically',
Labor_Code__r:{type:'Labor_Code__c',
Id:'a0630000000KaoyAAC',
Name:'X1300',
Description__c:'Description of Labor Code',
 },
Correction_Description__c:'Problem.',
Case__r:{type:'Case', Id:'50060000002hcs1AAA', }, }

 

 

cheenathcheenath

{type:'Complaint__c',
Id:'a0760000000t3ukAAA',
Component_Group__c:'Transmission / Transaxle',

Complaint__r:{type:'Complaint_Causes__c',
Id:'a0F300000008Zo6EAE',
Name:'Transmission',
}

This tells me that the record is of type Complaint__c.

So try this:

var ComplaintName = record.Complaint__r ? record.Complaint__r.Name : null;





Again AgainAgain Again

Wonderful, thank you! You've made my day! 

When a field is legitimately empty and the display comes through as null ... is there a standard  way to convert it to a blank string or does one run it through one's own stringreplace function?

Again AgainAgain Again

That works. You've made my day!

When legit "null" values are returned - should one create a new string object for each variable to use the s.replace("xx","yy"); function, or is there a way to replace the null strings more "automatically"?

Thanks so much!

cheenathcheenath
There is no automatic way of doing this. You could write a function to
always return non-null string, something like:

function toNonNullString(str) {
  return str ? str : "";
}

can be used like:

var name  = toNonNullString(record.Name);

 


Again AgainAgain Again
Thanks, cheenath!