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
Sahil YadavSahil Yadav 

Rest API Coding

Hello Folks, I will be writing one rest apex class based on my business requirement but wanted to know a better way of how can we make our code much better.
{

  "Model": {

    "$type": "BuyerPortal.Profile.Shared.SalesforceModel, BuyerPortal.Profile.Shared",

    "BuyerId": null,

    "EventName": "BuyerMarketExpansionUpdate",

    "When": "2022-09-27T20:39:41.6733477+00:00",

    "SegmentationInfo": null,

    "MarketExpansionInfo": {

      "AuthUserKey": "1a396649-0d4e-4206-b93e-168a59ca816a",

      "LegalFirstName": "RMMMMMM",

      "LegalLastName": "MoMMMroMt",

      "Email": "",

      "BuyerId": null,

      "PrimaryMarket": 3,

     "ExpansionMarket": 2,

      "UserAcceptanceHistory": {

        "AcceptAuctionRules": true,

        "AcceptAuctionRulesDateTime": "2022-09-27T20:38:58.7321995+00:00",

        "AcceptPrivacyPolicy": true,

        "AcceptPrivacyPolicyDateTime": "2022-09-27T20:38:58.7322046+00:00",

        "IPAddress": "::1",

        "DeviceSource": 0,

        "AcceptPromotionalOffers": false,

        "AcceptPromotionalOffersDateTime": null,

        "AcceptNonDealerDeclaration": false,

        "AcceptNonDealerDeclarationDateTime": null

      }

    },

    "AuctionAccessCompanyMismatch": null,

    "AuthUserKey": "1a396649-0d4e-4206-b93e-168a59ca816a",

    "RegistrationModel": null

  },

  "CorrelationId": null,

  "Roles": null,

  "SessionId": null,

  "From": null,

  "DomainName": null,

  "RetryOnFailure": false,

  "DeliverAfterEpoch": {

    "$type": "IAAI.Base.Epoch, IAAI.Base",

    "Seconds": 0

  },

  "Token": null,

  "MaxRetryCount": 0

}

This above is the payload which salesforce willl consume and perform an updation on that particular lead record.
Authuserkey is the common token in between of salesforce and external system .
so i need to fetch those lead record   which matches an authuser key and also two attribute is been passing in payload and based on that i need to update the salesforce records.
I have wrote an rest apex class but wanted to make sure i am on the correct way or not.

When we receive a '1' for 'Primary Market or Expansion Market we would expect to see 'US' in the User Interface 
When we receive a '2' for 'Primary Market or Expansion Market we would expect to see 'CA' in the User Interface 
When we receive a '3' for 'Primary Market or Expansion Market we would expect to see 'UK' in the User Interface

 
@RestResource (urlMapping = '/createBuyerLead/')
global with sharing class LeadClass {
      @httpPatch
    global static void updatePrimaryAndExpansionMarket(){
          String fname ,lname, authuserkey, company, primarymarket, expansionmarket;
        RestRequest request = RestContext.request;
        RestResponse response = restContext.response;
        String requestbody = request.requestBody.tostring();
        JSONParser parser = JSON.createParser(requestbody);
         //List<Lead> leadList = [Select id, name ,AuthUserKey__c, Primary_Market__c ,Expansion_Market__c  from lead where AuthUserKey__c =:authuserkey Limit 1 ];
        //System.debug('@@'+leadList);
        //System.debug('@@'+parser.readValueAs(JSONParser));
        while(parser.nextToken() != null){
            if(parser.getCurrentToken() != JSONToken.END_OBJECT){
                String fieldname = parser.getCurrentName();
                System.debug('@fieldname@'+fieldname);
                
                String fieldvalue = parser.getText();
                System.debug('@fieldvalue@'+fieldvalue);
                if(fieldname == 'AuthUserKey'){
                    authuserkey = fieldvalue; 
                }
                List<Lead> leadList = [Select id, name ,AuthUserKey__c, Primary_Market__c ,Expansion_Market__c  from lead where AuthUserKey__c =:authuserkey Limit 1 ];
                if(fieldname == 'PrimaryMarket'){
                     primarymarket = fieldvalue;
                    if(primarymarket == '1'){
                   leadList[0].Primary_Market__c = 'US';
                    }
                    else if(primarymarket == '2'){
                   leadList[0].Primary_Market__c = 'CA';
                    }
                    else if(primarymarket == '3'){
                   leadList[0].Primary_Market__c = 'UK';
                    }
                    
                }
                
                 else if(fieldname == 'ExpansionMarket'){
                     expansionmarket = fieldvalue;
                    if(expansionmarket == '1'){
                   leadList[0].Expansion_Market__c = 'US';
                    }
                    else if(expansionmarket == '2'){
                   leadList[0].Expansion_Market__c = 'CA';
                    }
                    else if(expansionmarket == '3'){
                   leadList[0].Expansion_Market__c = 'UK';
                    }
                }
                
               
                try{
                   update leadList[0];
                    System.debug('Leadlist'+leadList[0]);
                 
                }
                catch(Exception ex){
                    
                }
                
            }
        }
        
        
    }
       

}

Any Suggestion or improvement highly appreciated !!!