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
RKRK 

Querying data from User Table From Within S-Control

Hey all,
A new process is being developed within my sales organization where a group of people, on a daily basis, will manually create "Thank you" calls for another group of users.

Who the calls should be assigned to is based off of a custom field on the User Record of the Contact Owner (a relationship field).

So, since the calls are all essentially the same, I'm trying to allow the task-enteres to click a single link to create the task.

I can't seem to anything to query from the user table.  Here are my variable declaratoins.  As you will see near the bottom (for testing purposes)


var contactOwnerId = '{!Contact.OwnerId}';  //Here I'm grabbing the OwnerID from the Contact Record
alert (contactOwnerId );
alert("owenrID");
var firstTicketContactId = '{!Contact.Id}';
var firstTicketTaskSubject = 'First Ticket Follow-up Call';
var firstTicketResults = 'Follow-Up';
var firstTicketDate = {!Today};
var firstTicketTaskNotes = prompt('What are the comments for the task?',' ');
var firstTicketTaskStatus = 'Not Started';
//below is the query that where I'm trying to get the first name of the Contact Owner from the user table
var internalWholesalerId = sforceClient.Query("Select FirstName from User where Id = "'+contactOwnerId+"'");'
alert (internalWholesalerId);

So, even a simple query of pulling the first name from the user table returns a value of Undefined.

Thoughts?  As always, thanks for educating the ignorant (i.e. Me).

cheenathcheenath
Please try to use the production toolkit.
It is much faster than beta toolkit and it is supported.
To use production toolkit, you have to just include the script:

<script src="/soap/ajax/9.0/connection.js"> </script>

Then your query will look like:

var internalWholesalerId = sforce.connection.query("Select FirstName from User where Id = "'+contactOwnerId+"'");'
alert (internalWholesalerId);

HTHs,



RKRK
Thanks, Cheentah...you got me in the right direction, and it does appear to be quicker.

So, my final code is shown below.  The only problem that I am encountering is if I try to assign the task to the user that is related to the contact owner user.

Check out the code in red...If I leave it in, the task does not get created.  However, if I just set the owner of the task to be teh contact owner, it works just fine (It is the code in the blue at the bottom).  Maybe I can't assign the task via the relationship field on the user record?


<html>

<head>

<script src="/soap/ajax/9.0/connection.js"></script>

<script>

var contactOwnerId = '{!Contact.OwnerId}';
var firstTicketTaskType = 'Outbound Call';
var firstTicketContactId = '{!Contact.Id}';
var firstTicketTaskSubject = 'First Ticket Follow-up Call';
var firstTicketResults = 'Follow-Up';
var firstTicketDate = {!Today};
var firstTicketTaskNotes = prompt('What are the comments for the task?',' ');
var firstTicketTaskStatus = 'Not Started';
var internalWholesalerId = sforce.connection.query("Select Internal_Wholesaler__c from User where Id = '" +contactOwnerId+ "'");
alert ("about to give Internal Wholesaler ID");
alert (internalWholesalerId);

function initPage() {
alert("Infun");
var firstTicketTask = new sforce.SObject("Task");
alert("DynaBean");
firstTicketTask.set ("Results__c",firstTicketResults);
alert("ResultsSet");
firstTicketTask.set("Type",firstTicketTaskType);
alert ("Task Type Set");
firstTicketTask.set("Subject",firstTicketTaskSubject);
alert("SubSet");
firstTicketTask.set("Status",firstTicketTaskStatus);
alert("StatSet");
firstTicketTask.set("Description",firstTicketTaskNotes);
alert("CommSet");
firstTicketTask.set("ActivityDate",new Date('{!Today}') );
alert("dateset");
firstTicketTask.set("OwnerId",internalWholesalerId);
alert("OwnerSet");
/*
if I substitute the red code above with the blue code below, the task works fine and it creates properly
firstTicketTask.set("OwnerId",contactOwnerId );
alert("OwnerSet");
*/

firstTicketTask.set("WhoId",firstTicketContactId);
alert("ConIDSet");
var result = sforce.connection.create([firstTicketTask]);
alert(result);


}

</script>

</head>

<body onload="initPage();">

</body>

</html>
cheenathcheenath
var internalWholesalerId = sforce.connection.query("Select Internal_Wholesaler__c from User where Id = '" +contactOwnerId+ "'");

query() returns a QueryResult object not an id. So you have to get the id from the query result.

var queryResult = sforce.connection.query("Select Internal_Wholesaler__c from User where Id = '" +contactOwnerId+ "'");

var records = queryResult.getArray("records");

if (records.length == 1) {
  internalWholesalerId = records[0].Internal_Wholesaler__c;
} else {
  thorw "error query didnt return any result";
}

 

RKRK
Thank you so much for the help, Cheenath.  I suspected that it had something to do with that (based upon what the alert for the internalWholesalerId was providing), but didn't know how to address it.  Thanks for helping out a hack.

For those of you who are n00b's like me, here is the final code (and feel free to vote on my Idea repository, found at the red link below!)
http://ideas.salesforce.com/article/show/57416/A_UserSubmitted_CodeSControlEnhancement_Sample_Library

<html>

<head>

<script src="/soap/ajax/9.0/connection.js"></script>

<script>

var contactOwnerId = '{!Contact.OwnerId}';
//alert(contactOwnerId);
var firstTicketTaskType = 'Outbound Call';
var firstTicketContactId = '{!Contact.Id}';
var firstTicketTaskSubject = 'First Ticket Follow-up Call';
var firstTicketResults = 'Follow-Up';
var firstTicketDate = {!Today};
var firstTicketTaskNotes = prompt('What are the comments for the First Ticket Task?',' ');
var firstTicketTaskStatus = 'Not Started';

var queryResult = sforce.connection.query("Select Internal_Wholesaler__c from User where Id = '" +contactOwnerId+ "'");

var records = queryResult.getArray("records");

if (records.length == 1) {
internalWholesalerId = records[0].Internal_Wholesaler__c;
} else {
throw "error query didnt return any result";
}

function initPage() {
//alert("Infun");
var firstTicketTask = new sforce.SObject("Task");
//alert("DynaBean");
firstTicketTask.set ("Results__c",firstTicketResults);
//alert("ResultsSet");
firstTicketTask.set("Type",firstTicketTaskType);
//alert ("Task Type Set");
firstTicketTask.set("Subject",firstTicketTaskSubject);
//alert("SubSet");
firstTicketTask.set("Status",firstTicketTaskStatus);
//alert("StatSet");
firstTicketTask.set("Description",firstTicketTaskNotes);
//alert("CommSet");
firstTicketTask.set("ActivityDate",new Date('{!Today}') );
//alert("dateset");
firstTicketTask.set("OwnerId",internalWholesalerId);
//alert("OwnerSet");
firstTicketTask.set("WhoId",firstTicketContactId);
//alert("ConIDSet");
var result = sforce.connection.create([firstTicketTask]);
//alert(result);

top.window.close();
parent.parent.frames.location.replace("/"+firstTicketContactId+"");
opener.location.reload();

}

</script>

</head>

<body onload="initPage();">

</body>

</html>