We are using angular to display UI and Salesforce is being used to maintain database. We have a REST API class created which has the urlMapping. This class is used to find the records. Now we need to call this API (url- /v1/UpdateCustomerRecords/*) in angular but we have no idea how to start. Below is the code snippet. Please suggest.

*  Example json Query  *
"Customers": {
    "updateCustomers": [{
        "accountId": "0012500000dOg5l",
        "CustomerId": "a2D20000000ABCl"
    }, {
        "accountId": "0012500000dOg5l",
        "CustomerId": "a2D20000000JG5r"
*accountId = Id of the currently logged in user
*CustomerId = Id of the Customer record that the user selected for reassigning
global with sharing class REST_UpdateCustomerRecords_V1 {

global static CustomerResponse doPatch(Customers Customers) {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    CustomerResponse response = new CustomerResponse();

    response.updateCustomers = Customers.updateCustomers;

    //Make sure the data passed for this call is valid. If it is not then return an error stating the error

    if(response.status == 'Failure'){

        return response;


    List<Customer__c> cusList = new List<Customer__c>();
    Account currentAccount = new Account();

    //Start looping through the records to get updated
    for(integer i = 0; i < response.updateCustomers.size(); i++){

        Customer__c oldCustomer = [SELECT Id, Account__c FROM Customer__c WHERE Id = :response.updateCustomers[i].CustomerId LIMIT 1];

        currentAccount.Id = response.updateCustomers[i].accountId;
        currentAccount.Customer_details_to_be_moved_from__c = oldCustomer.Account__c;

        //Each time, create a Customer__c variable using the Customer Id and the Account Id. Then add that variable to a list of Customer__c
        Customer__c currentCustomer = new Customer__c(Id = response.updateCustomers[i].CustomerId, Account__c = response.updateCustomers[i].accountId);



    //Try to update those Customer records

        update cusList;
        update currentAccount;

        if(cusList.size() > 1){

            //If the update works then return a Success message and state how many Customers were updated
            response.Status = 'Success';
            response.Message = cusList.size() + ' Customer records are now visible to you';
            response.errorCode = '000';


        else if(cusList.size() == 1){

            //If the update works then return a Success message and state that Customer is now visible
            response.Status = 'Success';
            response.Message = 'That Customer record is now visible to you';
            response.errorCode = '000';



    return response;

global class CustomerResponse {
    public List<updateCustomers> updateCustomers;
    public String status;
    public String message;
    public String errorCode;

global class Customers{

    public List<updateCustomers> updateCustomers = new List<updateCustomers>();


global class updateCustomers{

    public string accountId;
    public string CustomerId;

  • So basically you want to make an api call from JS, right? – Mr.Frodo Jan 27 '17 at 7:01
  • Yes, We are using JS for front end – Salesforce Learner Jan 27 '17 at 7:02

Step 1: Authentication

Before making an API call you would also need to perform authentication into salesforce. This part requires much attention from your side as you have to decide which method you will use for authentication etc, There are typically three:

Web server flow, where the server can securely protect the consumer secret.

User-agent flow, used by applications that cannot securely store the consumer secret.

Username-password flow, where the application has direct access to user credentials.

Step 2: After Authentication, Make a call:

In JS you can make an API call like this:

var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "Your Rest URL Here", false);
    xhttp.setRequestHeader("Content-type", "application/json");
    var response = JSON.parse(xhttp.responseText);

The base endpoint for Apex REST is https://instance.salesforce.com/services/apexrest/


  1. For calling the API from JS follow below link:

How to call a REST web service API from Javascript

  1. How to call Apex Rest class from outside of salesforce take a look at following blog, Try workbench example given in the blog to have in-depth understanding.

Rest API in Salesforce | Execute Rest API on workbench

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.