You are on page 1of 4

Ajax :

---------------------------------------------------------------------1.Creating a new Records using Ajax in javascript


-----------------------------------------------------------------------<apex:page id="page">
<script src="/soap/ajax/36.0/connection.js" type="text/javascript">
</script>
<script>
function callme(){
sforce.connection.sessionId='{!$API.session_Id}';
var name=document.getElementById('page:fm:name').value;
var indust=document.getElementById('page:fm:indus').valuel
var account1 = new sforce.SObject("Account");
account1.Name=name;
account1.Industry=indust;
var result=sforce.connection.create([account1]);
document.getElementById('page:fm:res').innerHTML=result;
}
</script>
<apex:form id="fm">
Account&nbsp;&nbsp; : <apex:inputText id="name" />
Industry : <apex:inputText id="indus" />
<apex:commandButton value="Ajax Call" oncomplete="callme()"/>
<apex:outputLabel id="res" />
</apex:form>
</apex:page>
------------------------------------------------------------------------------------2. Create a VF page with ajax tool kit to fetch all the account records and dip
lay
their names in VF page
-------------------------------------------------------------------------------------<apex:page id="page">
<script src="/soap/ajax/36.0/connection.js" type="text/javascript">
</script>
<script>
function callme(){
sforce.connection.sessionId='{!$API.session_Id}';
var queryResult=sforce.connection.query('select name ,industry from Acco
unt');
var records=queryResult.getArray('records');
var result='';
for(var i=0;i<records.length;i++){
result=result+'<br/>'+records[i].Name;
}
document.getElementById('page:fm:res').innerHTML=result;
}
</script>
<apex:form id="fm">
<apex:commandButton value="Ajax Call" oncomplete="callme()"/>
<apex:outputLabel id="res" />
</apex:form>
</apex:page>
-----------------------------------------------------------------------------------3. Create a dynamic soql query using ajax to fetch all the account records whos
e industry is matching with input value and display the account names

-------------------------------------------------------------------------------------<apex:page id="page">
<script src="/soap/ajax/36.0/connection.js" type="text/javascript">
</script>
<script>
function callme(){
sforce.connection.sessionId='{!$API.session_Id}';
var myIndustry=document.getElementById('page:fm:myIndustry').value;
var query='select name ,industry from Account where industry=\''+myIndus
try+'\'';
var queryResult=sforce.connection.query(query);
var records=queryResult.getArray('records');
var result='';
for(var i=0;i<records.length;i++){
result=result+'<br/>'+records[i].Name;
}
document.getElementById('page:fm:res').innerHTML=result;
}
</script>
<apex:form id="fm">
<apex:inputText id="myIndustry" />
<apex:commandButton value="Ajax Call" oncomplete="callme()"/>
<apex:outputLabel id="res" />
</apex:form>
</apex:page>
--------------------------------------------------------------------------------------4: Create a dynamic soql using ajax sforce toolkit and fetch all the account rec
ords whoes phone no is matching with given input value and delete all those rec
ords
-----------------------------------------------------------------------------------<apex:page id="page">
<script src="/soap/ajax/36.0/connection.js" type="text/javascript">
</script>
<script>
function callme(){
sforce.connection.sessionId='{!$API.session_Id}';
var myPhone=document.getElementById('page:fm:myPhone').value;
var query='select id,name from Account where phone=\''+myPhone+'\''
;
var queryResult=sforce.connection.query(query);
var records=queryResult.getArray('records');
var Ids=[];
for(var i=0;i<records.length;i++){
Ids[i]=records[i].Id;
}
var result=sforce.connection.deleteIds(Ids);
document.getElementById('page:fm:res').innerHTML=result;
}
</script>
<apex:form id="fm">
<apex:inputText id="myPhone" />
<apex:commandButton value="Ajax Call" oncomplete="callme()"/>
<apex:outputLabel id="res" />
</apex:form>
</apex:page>

------------------------------------------------------------------------------------5. Craete a VF page to establish a connection with third party salesforce organi
zation
using ajax sforce-tool kit and insert the records based on the input values
-------------------------------------------------------------------------------------<apex:page id="page" >
<script src="/soap/ajax/36.0/connection.js" type="text/javascript">
</script>
<script>
function callme(){
var sessionData=sforce.connection.login
('batch0265@capital.com','salesforce12385BGecnBlWIFv9O');
sforce.connection.sessionId=sessionData.sessionId;
var name=document.getElementById('page:fm:name').value;
var indust=document.getElementById('page:fm:indus').valuel
var account1 = new sforce.SObject("Account");
account1.Name=name;
account1.Industry=indust;
var result=sforce.connection.create([account1]);
document.getElementById('page:fm:res').innerHTML=result;
}
</script>
<apex:form id="fm">
Account&nbsp;&nbsp; : <apex:inputText id="name" />
Industry : <apex:inputText id="indus" />
<apex:commandButton value="Ajax Call" oncomplete="callme()"/>
<apex:outputLabel id="res" />
</apex:form>
</apex:page>
-------------------------------------------------------------------------------------

You might also like