You need to sign in to do that
Don't have an account?
Sahil Yadav
How should we use Wrapper Class in Rest Apex Class ??
Hello Folks, I will be working on Integration Part and I would have come accross the scenario where external system will send some payload to salesforce and based on payload we need to update that specific record in salesforce.
Now authuser key is common on both the system and based on that that authuserkey we need to fetch that specific lead record .
Rest Apex Code :-
But its not updating an appropriate lead record .
Authuser key is showing as null where as in payload we are sending the values
Any Suggestion or Help would be highly Appreciated !!!!
Payload Send from External System { "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 }
Now authuser key is common on both the system and based on that that authuserkey we need to fetch that specific lead record .
When ASAP sends over the following enumerations for 'Primary Market' and/or Expansion Market then we should display the respective text: 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
Rest Apex Code :-
@RestResource (urlMapping = '/createBuyerLead/') global with sharing class LeadClass { @HttpPatch global static String updatePrimaryAndExpansionMarket(){ //String leadFirstName, leadLastName, leadAuthUserKey , leadPrimaryMarket , leadExpansionMarket; RestRequest request = RestContext.request; RestResponse response = RestContext.response; Blob blobrequestBody = request.requestBody; String requestBody = blobrequestBody.toString(); Map<String, Object> requestBody1 = (Map<String,Object>)JSON.deserializeUntyped(requestBody); System.debug('requestBody1 : '+ requestBody1); Map<String, Object> requestBody2 = new Map<String , Object>(); for(String s : requestBody1.keySet()){ requestBody2.put(s.toLowerCase(), requestBody1.get(s)); } System.debug('requestBody2'+requestBody2); String leadAuthUserKey = String.valueOf(requestBody2.get('AuthUserKey')); System.debug('leadAuthUserKey'+leadAuthUserKey); String leadFirstName = String.valueOf(requestBody2.get('LegalFirstName')); String leadLastName = String.valueOf(requestBody2.get('LegalLastName')); String leadPrimaryMarket = String.valueOf(requestBody2.get('PrimaryMarket')); String leadExpansionMarket = String.valueOf(requestBody2.get('ExpansionMarket')); Lead leadRecord = [Select id, name, Primary_Market__c, Expansion_Market__c, authuserkey__c from lead where authuserkey__c =:leadAuthUserKey LIMIT 1 ]; System.debug('Lead Name - '+leadRecord.Name); if(leadPrimaryMarket == '1'){ leadRecord.Primary_Market__c = 'US'; } else if(leadPrimaryMarket == '2'){ leadRecord.Primary_Market__c = 'CA'; } else if(leadPrimaryMarket == '3'){ leadRecord.Primary_Market__c = 'UK'; } if(leadExpansionMarket == '1'){ leadRecord.Expansion_Market__c = 'US'; } else if(leadExpansionMarket == '2'){ leadRecord.Expansion_Market__c = 'CA'; } else if(leadExpansionMarket == '3'){ leadRecord.Expansion_Market__c = 'UK'; } update leadRecord; String leadId = leadRecord.id; return leadId; } }
But its not updating an appropriate lead record .
Authuser key is showing as null where as in payload we are sending the values
Any Suggestion or Help would be highly Appreciated !!!!
It looks like the issue is related to how you are deserializing the JSON payload and retrieving the values from it.
- Make sure that the structure of the JSON payload matches the structure you are trying to deserialize into. In your case, ensure that the structure of the payload matches the way you are accessing the fields like 'AuthUserKey', 'LegalFirstName', 'LegalLastName', 'PrimaryMarket', and 'ExpansionMarket' in your code.
- You can create a class that represents the structure of the payload, and then deserialize the JSON into an instance of that class.
you can check below sample example Then, deserialize the payload as follows: Make sure that the 'AuthUserKey' value in the payload matches the one in your Salesforce records. If they don't match, the query won't return the expected lead record.Related:http://cloudyworlds.blogspot.com/2012/09/how-to-generate-wrapped-data-from.html
https://www.apexhours.com/wrapper-class-in-salesforce/
Thanks.