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
Raju Sudheer 3Raju Sudheer 3 

how to use sforce connection

Amit Chaudhary 8Amit Chaudhary 8
Example 1:-
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var newRecords = []; 
var c = new sforce.SObject("Opportunity"); 
c.id ="{!Opportunity.Id}"; 
c.Voice_Referal__c = true; 
newRecords.push(c); 
result = sforce.connection.update(newRecords); 
window.open('/apex/SDOC__SDCreate1?id={!Opportunity.Id}&Object=Opportunity');
 
Example 2:-https://success.salesforce.com/answers?id=90630000000gwO5AAI
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}

//Grab the status value you're interested in, and I'm blanking on how to do this at the moment

if(status <> "Particular Value"){
alert("Nice try, think again.");
}
else{
window.open('/apex/<whatever your page is>');
}

Example 3:- http://https://success.salesforce.com/answers?id=90630000000CjubAAC
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

if("{!Contact.New_Hire_Start_Date__c}" !== ""){
    var contactToUpdate = new sforce.SObject("Contact");

    contactToUpdate.Id = "{!Contact.Id}";
    contactToUpdate.Recruiting_Status__c = "NURF Initiated";
    contactToUpdate.Workflow_Trigger_FOR_NURF__c = "1";

    var result = sforce.connection.update([contactToUpdate]);

    if (result[0].success === "false"){
        alert(result[0].errors.message);
    }
    else{
        location.reload(true);
    }
}
else{
    alert("The New Hire Start Date field has to be checked!");
}

Example 4:-
http://salesforce.stackexchange.com/questions/8521/how-to-build-a-salesforce-custom-button-with-validation-rules
http://salesforce.stackexchange.com/questions/4497/button-validation-logic


Please let us know if this will help you,

Thanks,
Amit Chaudhary
 
SFDC developer999SFDC developer999
Hi Amit,
  what is the usage of this test : {!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}

  a button is defined with onclick javascript and an email should be sent after button is clicked:


OnClick JavaScript{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
 result = sforce.connection.update([object]);
 
However email was not sent, where to look at ? where Can I find the connection.js ?
 
Satya Ranjan MohantySatya Ranjan Mohanty
HI Amit,

Can you please guide me how to implement  this type of functionality in lightning,as you know javascript buttons will not working lightning.

Thanx in advance
satya
Phil Medina 2Phil Medina 2
I will keep it very generic.
1. Create apex class to handle the logic. (validations and checks, actions...)
2. Create new lightning component to display the button.
3. On the controller of the lightning component you created call the class you created in step 1.
     (You are giving the new button direction of what to do when clicked.)
4. Create a new action with action type of lightning component.  Then point to your lightning component name from step 2.
5. Add component to lightning page.