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
embemb 

Mixing apex and ajax. How to store data in the page loaded?

Hello,

 

I would like to know how to store data, after a database query, for using this data in a Ajax script?

 

I have a custom object. Called "Calendar". I need to store, to collect data from this custom object "Calendar", but i don't know how?

 

I need to have my data in this 'format' :

 

{ResourceId: 'LastName FirstName', Type : 'Type_1', Title : 'Type_title', Ip : 'LastName FirstName',  NBapprenants : '10', StartDate : '2011-08-15 09:00:00', EndDate : '2011-08-15 18:00:00'},

 

 

I can display this data with a Controller and a Visualforce using apex tags, but i need to store this data for an ajax application.

 

 

If somebody can help me?

 

regards

 

Emmanuel

 



Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

you have not included the script required for performing ajax call.

try this:

<apex:page >
<apex:includeScript value="/soap/ajax/20.0/connection.js"/> <apex:includeScript value="/soap/ajax/20.0/apex.js"/> 
<apex:pageBlock >
<script>
sforce.connection.sessionId = '{!$Api.Session_ID}';
var theQuery = "SELECT LastName from Contact limit 10";
var result = sforce.connection.query(theQuery)
var records = result.getArray("records"); alert(records);
</script>
</apex:pageBlock>

</apex:page>

 

All Answers

Rahul SharmaRahul Sharma

Hi Emmanuel,

You can use methods like:-

sforce.connection.create:Insert a record

sforce.connection.update:update a record

embemb

Hi

Thank you for your answer Rahul.

 

Sorry, actually i don't want to store; i mistake the word. I need to COLLECT data.

 

 

Rahul SharmaRahul Sharma

I'm confused now.

Can you explain on what you are trying to do?

embemb

More precisely i need to collect data (after database query), and then pass the data in a javascript variable.

 

Example (which does'nt work of course, it's just for explain the idea):

 

In the controller:

 

select my_fields from my data_base

 

<script>

var test = !my_fields_data;

</script>

 

 

 

In the VF page:

 

<script>
alert(test);
</script>



Rahul SharmaRahul Sharma

try this:

var theQuery = "SELECT LastName from Contact limit 10"; 
var result = sforce.connection.query(theQuery) 
var records = result.getArray("records"); alert(records);

now records will contain array, similar to a list.

hope it helps.

embemb

I put the following code, in the page (without use Controller)

But it does'nt show anything...

 

<apex:page >
<apex:pageBlock >
<script>
var theQuery = "SELECT LastName from Contact limit 10";
var result = sforce.connection.query(theQuery)
var records = result.getArray("records"); alert(records);
</script>
</apex:pageBlock>

</apex:page>

 

Rahul SharmaRahul Sharma

you have not included the script required for performing ajax call.

try this:

<apex:page >
<apex:includeScript value="/soap/ajax/20.0/connection.js"/> <apex:includeScript value="/soap/ajax/20.0/apex.js"/> 
<apex:pageBlock >
<script>
sforce.connection.sessionId = '{!$Api.Session_ID}';
var theQuery = "SELECT LastName from Contact limit 10";
var result = sforce.connection.query(theQuery)
var records = result.getArray("records"); alert(records);
</script>
</apex:pageBlock>

</apex:page>

 

This was selected as the best answer
embemb

Thank you Rahul!

 

it works!

 

Have a nice... evening, if you are in India ?

 

Emmanuel (from France)

Rahul SharmaRahul Sharma

yes, from india.

Have a nice day!!

embemb

Rahul,

 

i just see that the data of the variable "record" is the query. But I need to get the result of the query, note the query itself.

 

For examlple, i get a record :

{type:'Calendrier__c', Id:'a0FU0000000Y0l5MAC', nom__c:null, id__c:'5454', Tache__c:'Banque BCP - Financement des TPE', SystemModstamp:'2011-10-28T07:30:52.000Z', Ressource__c:'FRANCO-IDRISS Béatrice', OwnerId:'005U0000000hDs5IAE', Name:'Banque BCP - Financement des TPE', LastModifiedDate:'2011-10-28T07:30:52.000Z', LastModifiedById:'005U0000000hDs5IAE', LastActivityDate:null, IsDeleted:'false', Heure_Fin__c:'18:00:00', Heure_Debut__c:'09:00:00', Date__c:'2011-05-31', CreatedDate:'2011-10-28T07:30:52.000Z', CreatedById:'005U0000000hDs5IAE', Categorie__c:'Intra-fr-Confirmer', }

 

I need to have only the data. not the colomn name.

 

 

Rahul SharmaRahul Sharma

Emmanuel, 

Thats not a query, thats the array of result retrived from query.

for the data you can access as you do in apex, for eg:

records[0].type, 

records[0].Id, 

records[0].com__c, ...

embemb

Rahul,

 

You are right!

Thank you very much!

 

By the way... do you know if this query can use sharing rules?

I need to 'respect' the sharing settings to access some data.

For example, "user_2" can access only some accounts and not others accounts.

Thats why i would have choose first an apex query.

 

Emmanuel

mmrrmmrr

This code opens a new window when a record has stage  - closed from the standard Opportunity page layout, but I want to check one more condition to open the window  which I am not able to achieve.  The window works only without the highlighted part.. Can you help in any way?

 

<apex:page standardController="opportunity" >
<script src="/soap/ajax/22.0/connection.js"></script>
<script src="/soap/ajax/22.0/apex.js"></script>
<script>
var arrId = '{!$CurrentPage.parameters.id}';
var queryresult = sforce.connection.query("Select StageName, updated__c from Opportunity where id='"+arrId+"'");
var records = queryresult.getArray("records");
if(records[0].StageName=='Closed ' &&  records[0].updated__c==false  )
{
<!---window.open('/apex/popup1?id='+arrId);--->
window.open('/apex/popup1?id='+arrId ,'lookup','width=800,height=480,scrollbars=yes,left=200,top=100');
}

</script>

</apex:page>

Rahul SharmaRahul Sharma

Hi mmrr,

Are you opening this page on button click of custom button located on standard page.

What are you trying to do.

mmrrmmrr

The requirement is to popup the new window on Save of the record with the specified conditions satisfied, as I did not find a way to override the save functionality in a standard page, I had inserted this VF page in the standard page layout. So whenever a record with the conditions satisfied is opened or saved, this popup comes.  I am not supposed to have a custom button/link, as the window has mandatory fields which the user might not be aware of.

Abhinav SharmaAbhinav Sharma

Hi, I find this really useful for me.
http://cloudisfuture.blogspot.com/