I am facing a weird issue and was wondering if anyone had faced this error. Here are the details:
I have written a Custom Rest Service in my Salesforce Sandbox org named CustomAccountService
which has a Post Method in it. The method takes a list of Accounts
. All I am doing is updating a custom field on Account
object based on an InternalKey
. This is how my Service looks like:
@RestResource(urlMapping='/Account/')
global with sharing class CustomAccountService {
//Private class used as request dto
global class MerchantStatusUpdateMessage {
public String BeyondClientId { get; set; }
public String MerchantStatus { get; set; }
}
@HttpPost
global static Map <String,String> doPost(List<MerchantStatusUpdateMessage> merchantStatusUpdateMessage) {
//response returned by api endpoint
Map <String,String> response = new Map <String,String>();
try {
//Variable to collect all the ClientIds
Set<String> beyondClientIds = new Set<String>();
//Variable to hold a key value map
Map<String, String> clientId_StatusMap = new Map<String, String>();
for (MerchantStatusUpdateMessage request : merchantStatusUpdateMessage) {
//Collect all ClientIds
beyondClientIds.add(request.BeyondClientId);
//Create a key value map based for ClientId -> MerchantStatus
clientId_StatusMap.put(request.BeyondClientId, request.MerchantStatus);
}
//Get all the accounts based on ClientIds
List<Account> accounts = [SELECT Id, Name, Beyond_Unique_ID__c, Payment_Status__c FROM Account WHERE Beyond_Unique_ID__c = :beyondClientIds];
//Update status for each client
for (Account account : accounts) {
if(clientId_StatusMap.containsKey(account.Beyond_Unique_ID__c)) {
account.Payment_Status__c = clientId_StatusMap.get(account.Beyond_Unique_ID__c);
}
}
//Update changes in the database
update accounts;
//Mark isSuccess as True
response.put('isSuccess','True');
return response;
}
catch (Exception e) {
System.Debug ('------Exception-----'+e.getMessage ());
response.put('isSuccess','False');
//Mark isSuccess as false
return response;
}
}
}
Now, I am consuming this API from one of our Azure function (c#). There are two steps, first to make a login call and get the Token. Next call is the actual post request using token. When I run it locally, its working fine and updating the status in Salesforce. Here is the sample code:
HttpClient httpClient = new HttpClient();
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", _salesforceClientId),
new KeyValuePair<string, string>("client_secret", _salesforceClientSecret),
new KeyValuePair<string, string>("username", _salesforceUserName),
new KeyValuePair<string, string>("password", _salesforcePassword),
new KeyValuePair<string, string>("grant_type", _salesforceGrantType)
});
log.Info(formContent.ReadAsStringAsync().Result);
//Call to get access token
var loginResponse = httpClient.PostAsync(_salesforceLoginUrl, formContent).Result;
if (loginResponse != null)
{
log.Info(loginResponse.Content.ReadAsStringAsync().Result);
var token = JsonConvert.DeserializeObject<SalesforceLoginResponse>(loginResponse.Content.ReadAsStringAsync().Result).access_token;
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
var sfAccountUpdateUrl = string.Concat(_salesforceEntityUpdateUrl, "/services/apexrest/Account/");
//Call to update Entity in Salesforce
var entityUpdateResponse = httpClient.PostAsync(sfAccountUpdateUrl,
new StringContent(JsonConvert.SerializeObject(new { merchantStatusUpdateMessage = merchantStatusUpdateMessage }), Encoding.UTF8, "application/json")).Result;
if(entityUpdateResponse.StatusCode != HttpStatusCode.OK)
throw new Exception($"Error occured while updating entity status in Salesforce");
}
But now when I build the same code to one of our Dev environment and try to make the call from Dev server, I get the following error message:
An internal server error has occurred An internal server error has occurred An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. Thank you again for your patience and assistance. And thanks for using salesforce.com! An internal server error has occurred An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. Thank you again for your patience and assistance. And thanks for using salesforce.com! Error ID: 1245306877-10173 (1478489697)
Also, even if make the same call from POSTMAN, it works fine and returns token back. Any idea what is going wrong here?