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
Rohan TelangRohan Telang 

I have written REST API for Account and it's related Contact. Now I have the following requirement. Kindly help me regarding this.

From 3rd party, they are sending the data and it's getting stored in Salesforce. Now the requirement is, They are sending "Email Id" also.So i have to check whether that email id is already present in Salesforce. If it is already present then I have to fetch the data regarding that "Email id" from Salesforce and then do the update in the same data.

I have to do changes in below code of API.

@RestResource(urlMapping='/EGS_APIForOldCustomerWebTeam/*')
global class EGS_APIForOldCustomerWebTeam  {
    
    public class AccountData{
      //public Id accountId;
        public String customer_code;
        public String web_customer_id;
        public String web_customer_mapping_id;
        public String account_name;
        public String account_source;
        public String email;
        public String email_pec;
        public String vat_number;
        public String social_security_number;
        public String phone;
        public String mobile_phone;
        public String type;
      //public String billingAddress;
        public String billing_street_address;
        public String billing_city;
        public String billing_state;
        public String billing_zipcode;
        public String billing_country;
      //public String shippingAddress;
        public String shipping_street_address;
        public String shipping_city;
        public String shipping_state;
        public String shipping_zipcode;
        public String shipping_country;
        public String product_category;
        public String product_subcategory;
      //public String country;
      //public String stateProvince;
      //public String country1;
        public String first_name;
        public String last_name;
        public String software;
        public String activation_key;
        public String web_Account_Id;
        public String modules;
    }


    
    @httpPost
    global static void createAccountData(){
        try{
            RestRequest req = RestContext.request;
            String jsonString = req.requestBody.tostring();
            List<AccountData> receivedData= (List<AccountData>)System.JSON.deserialize(jsonString, List<AccountData>.class);
            
            List<Account> accountList = new List<Account>();
            List<Contact> conList=New List<Contact>();
            
            for(AccountData accountData : receivedData){
                Account acc=New Account();
                
                acc.Name=accountData.account_name;
              //acc.Customer_Code__c=accountData.customer_code;
                acc.AccountSource=accountData.account_source;
                acc.Email__c=accountData.email;
                acc.Email_PEC__c=accountData.email_pec;
                acc.VAT_number__c=accountData.vat_number;
                acc.Social_security_number__c=accountData.social_security_number;
                acc.Phone=accountData.phone;
                acc.Mobile_phone__c=accountData.mobile_phone;
                acc.Type=accountData.type;
              //acc.BillingAddress=accountData.billingAddress;
                acc.BillingStreet=accountData.billing_street_address;
                acc.BillingCity=accountData.billing_city;
                acc.BillingState=accountData.billing_state;
                acc.BillingPostalCode=accountData.billing_zipcode;
                acc.BillingCountry=accountData.billing_country;
              //acc.Country_C__c=accountData.country;
              //acc.State_province__c=accountData.stateProvince;
              //acc.ShippingAddress=accountData.shippingAddress;
                acc.ShippingStreet=accountData.shipping_street_address;
                acc.web_customer_id__c=accountData.web_customer_id;
                acc.web_customer_mapping_id__c=accountData.web_customer_mapping_id;
                acc.ShippingCity=accountData.shipping_city;
                acc.ShippingState=accountData.shipping_state;
                acc.ShippingPostalCode=accountData.shipping_zipcode;
                acc.ShippingCountry=accountData.shipping_country;
                acc.Product_Category__c=accountData.product_category;
                acc.Product_Subcategory__c=accountData.product_subcategory;
                acc.Software__c=accountData.software;
                acc.Activation_Key__c=accountData.activation_key;
                acc.Web_Account_Id__c=accountData.web_Account_Id;
                acc.Modules__c=accountData.modules;
                
              //acc.Country__c=accountData.country;
                accountList.add(acc);
                
                
            }
            if(!accountList.isEmpty()){
            insert accountList;
            }
            
            for(AccountData accountData : receivedData){
                Contact varCon = New Contact();
                varCon.FirstName = accountData.first_name;
                varCon.LastName = accountData.last_name;
                varCon.Email = accountData.email;  
                varCon.MobilePhone = accountData.mobile_phone;
                varCon.Phone = accountData.phone;
                varCon.AccountId = accountList[0].id;
                conList.add(varCon);
            
            }
            
            if(!conList.isEmpty()){
            insert conList;    
            }
            
            RestResponse res = RestContext.response;
            res.statusCode = 200;
            String webCustomerId = (accountList[0].web_customer_id__c);
            system.debug('WebCustomerId '+webCustomerId);
            String webCustomerMappingId = accountList[0].web_customer_mapping_id__c;
            String customerCode = accountList[0].Customer_code__c;
            //res.responseBody= Blob.valueOf('Web Customer Id = '+webCustomerId+',  Salesforce Account Id = '+accountList[0].id+', Web Customer Mapping Id = '+webCustomerMappingId+', Customer Code = '+customerCode);
               Map<String,String> varMap=New Map<String,String>();
            varMap.put('web_customer_id',accountList[0].web_customer_id__c);
            varMap.put('salesforce_account_id',accountList[0].id);
            varMap.put('web_customer_mapping_id',accountList[0].web_customer_mapping_id__c);
            varMap.put('customer_code',accountList[0].Customer_code__c);
            res.responseBody=Blob.valueOf(JSON.serialize(varMap));
        }catch(Exception e){
            RestResponse res = RestContext.response;
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage());
        }
     }
  }
    
    
    
 
Please help me. Thank you in advance !!!!