Thursday, August 9, 2018

Salesforce Web Service Assignment for Practice

WEB SERVICE INTEGRATION ASSIGNMENT FOR PRACTICE
(Sunil Sharma and Gaurav Thampi)
  1. DUMMY REQUIREMENT FOR WEB SERVICE INTEGRATION
A Client will pass a Company (Account) Name into Salesforce.  
If a match is found, Salesforce must provide the respective Account ID.  
If no match is found, Salesforce must create a new account and return the new Account ID.  
The Client may also want to match contacts by specifying the Contact First Name, Last Name, Email and Mailing Postal Code. This should be developed, but should be done in such a way that it can easily be commented out, if necessary.
Salesforce must search for a Contact matching the specified details.
If found, Salesforce must return the respective Contact ID.
If not, Salesforce must create a new Contact with respective details given by the Client and return the new Contact ID.
  1. FUNCTIONAL SPECIFICATION
Title
Find matching Account based on Company Name passed by Client and return respective Account ID, or else create new Account with specified Company Name and return the new Account ID.
If Client requires a matching related Contact ID with details(First Name, Last Name, Email and Postal Code) specified by him, respective Contact ID must also be returned. If no match is found a new Contact must be created with the respective details and the new Contact ID must returned along with the Account ID.
Description
Client will call our SDFC Rest Service.
REQUEST EXAMPLE:
URI: https://ap4.salesforce.com/services/apexrest/Accounts/ 
Verb: POST
Header1  Content-Type : application/json
Header2  Authorization : Bearer <Access Token received from first request>

Request Body:
{
“CompanyName”:”<String>”,
“First name”:”<String>”,
“Last name”:”<String>”,
“email”: “<Email_Address>”,
“Postal Code”:”<String>”,
}

RESPONSE EXAMPLE:
When the Client calls the service, they can expect to receive a response similar to:
{
“Account Id”:”<String>”,
“Contact Id”:”<String>”
}
  1. STEPS FOLLOWED FOR IMPLEMENTATION
  1. POST Request sent by Client to grant authorization. Put the consumer key, client ID, username, password and security token to get the access token and instance URL
  2. The next POST request is made using the instance URL to the apex class using URL mapping. The access token must be appended to the authorization header sent in the request in such a way :
Bearer <Access_Token>
The Account ID (and Contact ID if needed) is returned to the Client.
  1. CODE IMPLEMENTATION
  1. The POST request is converted to String and parsed into JSON format using JSONparser.
  2. A Wrapper class named ‘Wrapper’ is used for JSON parsing.
  3. A list of Accounts named ‘matchingAccount’ is created to store the Account matching the specified name. It is also used to check if any such specified Account exists.
  4. If there are no matches a new Account is created and stored in Account variable ‘newAccount’ and inserted into the database.
  5. A Wrapper class named ‘returnWrapper’ is created to store the Account ID and Contact ID to be returned to client.
  6. The respective Account ID is added to the returnWrapper instance.
  7. A list of Contacts named ‘matchingContact’ is created to store the Contact matching the specified First Name, Last Name, Email and Postal Code. It is also used to check if any such specified related Contact exists.
  8. If there are no matches a new Contact is created and stored in Contact variable ‘newContact’ and inserted into the database.
  9. The respective Contact ID is added to the returnWrapper instance.
  10. The returnWrapper instance named ‘returnIds’ is returned.
  1. CODE
/*
Created By : Gaurav Thampi, Sunil Sharma
Created Date : 06/08/18
Title : Web Service Integration
Purpose : To match an Account with a given Company Name and return respective ID.
                If no matches found, create a new Account with specified Name and return new 
    Account ID.
                      Client may also pass Contact First Name, Last Name, Email and Postal Code.
                If a match is found the respective Contact ID must be returned.
                If not, a new Contact must be created with the respective details and the new 
    Contact ID must be returned.
*/

@RestResource(urlMapping = '/Accounts/*')    //Url to hit
global class FinalReq
{
    @HttpPost    //for POST method
    global static ReturnWrapper createAccount()
    {
        RestRequest req = RestContext.request;
        String message = req.requestBody.toString();    //Converts request body to string
        JSONparser parser = JSON.createParser(message);    //Parses message into JSON format

        Wrapper thisWrap = (Wrapper)parser.readValueAs(Wrapper.class);
        //maps Wrapper class variables to JSON token values

        returnWrapper returnIds = new returnWrapper();
        //Wrapper to return Account Id and Contact Id
 
        List<Account> matchingAccount = [select id from Account where 
                                                       Name=:thisWrap.companyName];
        //List of Account with matching Company Name

        if(matchingAccount.size()>0)    //Checks if Account already exists
        {
            returnIds.accountId = matchingAccount[0].Id;
            //Assigns Account ID to Wrapper class to be returned
        }
        else
        {
            Account newAccount = new Account();    //Creating a new Account
            newAccount.Name = thisWrap.companyName;
            insert newAccount;    //Insert new Account into Database
            returnIds.accountId = newAccount.Id;
            //Assigns Account ID to Wrapper class to be returned
        }

        List<Contact> matchingContact = [select id from Contact where Id=:returnIds.accountId                                                      and LastName=:thisWrap.lastName 
                                                     and Email=:thisWrap.email
                                                                 and MailingPostalCode=:thisWrap.postalCode 
                                                     limit 1];
        //List of Contact related to this Account with matching Last Name, Email and Postal 
          Code

        if(matchingContact.size()>0)    //Checks if such a Contact already exists
        {
                returnIds.ContactId = matchingContact[0].Id;
                //Assigns Contact ID to Wrapper class to be returned
        }
        else
        {
            Contact newContact = new Contact();    //Creating a new Contact
            newContact.FirstName = thisWrap.FirstName;
            newContact.LastName = thisWrap.lastName;
            newContact.Email = thisWrap.email;
            newContact.MailingPostalCode = thisWrap.postalCode;
            newContact.AccountId = returnIds.accountId;
            insert newContact;    //Insert new Contact into Database
            returnIds.ContactId = newContact.Id;
            //Assigns Contact ID to Wrapper class to be returned
        }
           return returnIds;
           //Returns AccountId and ContactId using a Wrapper class
    }

    global class returnWrapper    //Wrapper class for returning Account ID & Contact ID
    {
        public Id accountId{get; set;}    //Stores Account ID to be returned
        public Id ContactId{get; set;}    //Stores Contact ID to be returned
    }

    public class Wrapper    //Wrapper class to map JSON Token values
    {
        public string companyName{get; set;}
        public string firstName{get; set;}
        public string lastName{get; set;}
        public string email{get; set;}
        public string postalCode{get; set;}
    }

}

  1. SCREENSHOTS (POSTMAN)
Request to get Access Token

Screenshot 6.1
Request Headers : Using access token received from first request (screenshot 6.1)
Screenshot 6.2
Request Body : Includes Company (Account) Name and Contact First Name, Last Name, Email
and Postal Code to be entered by User
Screenshot 6.3
Final Result : Account ID and Contact ID returned to Client
Screenshot 6.4
SCREENSHOTS (WORKBENCH)
We do not have to send the initial request to get the access token since we must login to our Org in Workbench. We must use the relative URI, i.e., /services/apexrest/<Class Name specified in URL Mapping> instead of the absolute URI needed in postman or any other RESTful HTTP client for testing web services.
The request is sent with the client specified details contained in the body, and the corresponding IDs are returned to the client by Salesforce.



USE CASES

  1. Existing Account , Existing Contact


Matching Account ID in URL
Matching Contact ID in URL

  1. Existing Account, New Contact

Matching existing Account

New Contact Created

  1. New Account, New Contact


New Account Created

New Contact Created

9 comments:

  1. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also 澳洲代写

    ReplyDelete
  2. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work Orla customer care

    ReplyDelete
  3. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. UK VPS

    ReplyDelete
  4. I Enjoyed Reading The Blog. I love your blog. Thanks for sharing all of the useful info and will come back to read more. Visit:- Salesforce Lightning Consulting Services

    ReplyDelete
  5. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. https://helpmewithmyessay.com/write-my-essay/

    ReplyDelete
  6. Also known as Online Assignment Help, it is the most innovative concept in education world that Trusted PhD Thesis Writing Service provides you the way of learning at your ease at any time across the globe with help of internet.

    ReplyDelete
  7. Your post has those facts which are not accessible from anywhere else. It’s my humble request to u please keep writing such remarkable articles 北美代写

    ReplyDelete
  8. Effectively assessing and putting away finished schoolwork, pressing the assignment book and course readings, and zipping the fastener for the following day prompts a sensation of fulfillment as the day closes. cause and effect essay

    ReplyDelete
  9. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Assignment Answer

    ReplyDelete

Governor Limits Example-Too Many DML Statements :151

Governor Limits Example : Too Many DML Statements :151 (Sunil Sharma) Example:Code Hitting Governor Limits System.LimitExceptio...