I have a function that queries Salesforce contacts. In general, it works. But now I needed to query for a newly created test contact associated with a test account. I created both in Salesforce, and for some reason the contact is not found. I cannot figure out what I may be missing comparing to the existing accounts and contacts. Maybe somebody could give me a clue what I should look at?
Here is the code:
private List<Contact> GetContactInfoByEmail(string email, ILogger log)
{
string queryMessage = $"SELECT Id, Contact.Account.Name, Contact.Account.Type, Contact.Account.Address_Number__c, Contact.Account.MVP_ID__c, Pricebook_Authorized__c, Portal_Authorized__c FROM Contact WHERE Email = '{email}'";
JObject obj = JObject.Parse(QueryRecord(_httpClient, queryMessage, log));
Console.WriteLine(obj["records"]);
List<Contact> contacts = obj["records"].ToObject<List<Contact>>();
return contacts;
}
private string QueryRecord(HttpClient client, string queryMessage, ILogger log)
{
string restQuery = $"{_sfServiceUrl}{ApiEndpoint}query?q={queryMessage}";
log.LogInformation($"restQuery: {restQuery}");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, restQuery);
request.Headers.Add("Authorization", "Bearer " + _sfAuthToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.SendAsync(request).Result;
return response.Content.ReadAsStringAsync().Result;
}
Does the query work ok in Salesforce developer console, are you sure there are no typos in the email?
Is the C# connecting user a sysadmin? Maybe the contact is in the database all right but isn't returned due to "sharing rules" not making it visible for your API user. Does the account and contact owner have a "role" specified in SF setup?
Edit to answer the comment.
Look at the user that owns the Account/Contact. Go to Setup and search for this user in Setup's top search. Then
Generally speaking this sounds like a sharing issue. You will need admin's help. You can look at Setup -> Sharing Settings and figure out what's needed to make this Contact visible to the connecting user. Could be lack of Role (either add a Role to the owner or change owner to somebody else), could be that you didn't populate a field that's used in "sharing rules". SF security "who can see what" is a big topic. There's a self-paced training if you're adventurous, for example https://trailhead.salesforce.com/en/content/learn/modules/data_security/data_security_records?trail_id=force_com_admin_intermediate (it's a lesson 5 out of a bigger "trail" so maybe start from 1st)
P.S. It's stupid but I have to ask. Are you sure your program connects to right Salesforce org? You could check logging in user's login history (find that user in setup then scroll all the way down)