2

Javascript

<apex:page standardController="case">
<script type="text/javascript"> 

     function insertcase() 
         { 
         alert('came');
         var con = sforce.connection.login("username", "password");        

         var cas = new sforce.SObject("Case");
         alert(cas);
         cas.description = document.getElementById("description").value;
         alert(cas.description);
         alert('inside');           

         var caseResult = sforce.connection.create([cas]);  
         alert(caseResult);
         alert('If Creation');

         alert(cas.description);




                   else {
                   var a = sforce.connection.create([cas]);
                   alert('Else creation');
                   var b = a[0].id;
                   alert(b);
                   }  

             }
</script>


  <apex:form >

<apex:inputfield value="{!Description}"/>
  <apex:commandButton value="click me" onclick="insertcase();"/>
   </apex:form>


    </apex:page>

Problem: Case is not getting created.

I have to create a case using sforce without controller .

Please help me to solve this issue.

thanks in advance.

1

You forgot to add JS files for AJAX toolkit.. Add below script and session Id(If you are using inserting records in same org no need to use username password.)

<script type="text/javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}'; 
</script>
<script src="/soap/ajax/35.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/35.0/apex.js" type="text/javascript"></script>

If you want to create any record then use like this..

In this example I am creating Account record

<apex:page>

    <script src="/soap/ajax/35.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/35.0/apex.js" type="text/javascript"></script>
    <script type="text/javascript">
        sforce.connection.sessionId = '{!$Api.Session_ID}';
    </script>
    <script>
        function test() {
            var account = new sforce.SObject("Account");
            account.Name = "my new account";
            var result = sforce.connection.create([account]);

            if (result[0].getBoolean("success")) {
                log("new account created with id " + result[0].id);
            } else {
                log("failed to create account " + result[0]);
            }
        }
    </script>
    <input type="button" onclick="test()" value="Create Acc" />
</apex:page>
  • still its not working. I think the problem is with java script – Sandra Jan 27 '16 at 12:02
  • @Sandra if you are trying to create record then check my updated ans Example. like wise try to create Case record with all the required fields – Ratan Paul Jan 27 '16 at 12:05
  • @Sandra also always check browser console.. If you are getting error in javascript or not.. – Ratan Paul Jan 27 '16 at 12:07
  • <script type="text/javascript"> sforce.connection.sessionId = '{!$Api.Session_ID}'; function test() { alert("hello"); var account = new sforce.SObject("Account"); account.Name = "my new account"; var result = sforce.connection.create([account]); if (result[0].getBoolean("success")) { log("new account created with id " + result[0].id); alert("new account created with id " + result[0].id); } else { log("failed to create account " + result[0]); alert("failed to create account " + result[0]); } } </script> <apex:commandButton value="click me" onclick="test();"/> – Sandra Jan 27 '16 at 12:16
  • 1
    @ Ratan Thanks Its working, I will proceed with this further. I f any doubt will let u know.. – Sandra Jan 28 '16 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.