Ok, I know I’m a terrible coder, and that’s partly why I’m posting this. I know I’ll get things working but I don’t know standards / best practices. Some things I know, like use a transaction scope or make the company explicit, but there are things like naming conventions or commenting conventions that I’ve just never learned, being all self-taught, so please feel free to give any level of feedback.
The challenge is to update Salesforce when one of many things occurs. It took a lot of scouring of this forum and the net, with many thanks to @timshuwy, @Aaron_Moreng, @Mark_Wonsil, @ckrusen, @jgiese.wci , @josecgomez, @hkeric.wci and many others from the usual cast of suspects. I started out not knowing which end of an API you blow on and thinking Json was a weird way to spell my kid’s name.
I have this now, activated by a UBAQ just as a repeatable trigger.
//SF Salesforce Thingy
//Login & get Bearer Token
string loginMsg = "";
string appUrl = "https://test.salesforce.com/services/oauth2/token" ;
var crmClient = new RestClient(appUrl);
var request = new RestRequest (Method.POST) { RequestFormat = DataFormat.Json };
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", "3MVG9ayzKZt5EleF5ZgnAfpIhI9xobfuscatedbunch_6leLRZ8oftextnotthisi1gzRRipKiUzFSojROIURb8vz");
request.AddParameter("client_secret", "F53A22ACB8Anotsecret2deletedstufftooF2D56ifididn'tchangethisA1FB90D3FD39E5A9171E6F5blahblahblahE330B");
request.AddParameter("username", "myusername");
request.AddParameter("password", "mypassword");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
var response = crmClient.Execute(request);
string msg = response.Content.ToString();
Ice.Diagnostics.Log.WriteEntry(msg);
string jmsg = @"{'login1':[" + msg + "]}";
DataSet crmData = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>(jmsg);
string access_token = crmData.Tables[0].Rows[0].ItemArray[0].ToString();
string instance_url = crmData.Tables[0].Rows[0].ItemArray[1].ToString();
string id = crmData.Tables[0].Rows[0].ItemArray[2].ToString();
string token_type = crmData.Tables[0].Rows[0].ItemArray[3].ToString();
// get account info
var AcctClient = new RestClient("https://companyname--erp.my.salesforce.com/services/data/v49.0/sobjects/Account/[salesforce account id]" );
var AcctReq = new RestRequest(Method.GET) { RequestFormat = DataFormat.Json };
AcctReq.AddHeader("Authorization", "Bearer " + access_token);
AcctReq.AddHeader("Content-Type", "application/json");
response = AcctClient.Execute(AcctReq);
msg = response.Content.ToString();
Ice.Diagnostics.Log.WriteEntry(msg);
JObject acct = JObject.Parse(msg);
//remove the attributes node from the response or it can't be deserialized easily
acct.Remove("attributes");
msg = acct.ToString();
jmsg = @"{'login1':[" + msg + "]}";
DataSet acctData = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>(jmsg);
string outMsg = "";
for (int q = 0; q < acctData.Tables[0].Columns.Count; q++)
{
outMsg += acctData.Tables[0].Columns[q].Caption.ToString() + " - " + acctData.Tables[0].Rows[0].ItemArray[q].ToString() + Environment.NewLine;
}
//send the account details list to the server logs
Ice.Diagnostics.Log.WriteEntry(outMsg);
I had to include Newtonsoft which I thought was missing but seems to just require waiting a while for it to load. Also couldn’t find the JObject classes until adding Newtonsoft.Json.Linq.
Notice that in a Postman collection some things show as headers but didn’t work until I changed AddHeader for AddParameter. I have no idea why, I got a hint of it online somewhere and it worked.
Obviously still a lot of work to do - there are 10 calls in all and this only reads - but thought this might help someone and would like feedback. I have got Postman to create new accounts in Salesforce so it can’t be too hard. As is, this logs in and gets the account details from Salesforce, and makes them available to have your way with in Epicor.