Introduction
Each time a record is created, I want to send data to the SharePoint list. Below is the code that creates the authentication without us worrying about saving the access_token and looking for Expires_in time (which is in seconds), convert it into hours and check each time when a call is failed, then handle the get new access token from the controller.
Using the above code in approach 2 using named creds, you should be able to send data as JSON using an HTTP callout to the SharePoint list.REST Callout.
So, we use the trigger to write the trigger code and callout code in the Apex controller.
The below code triggers when a new record is submitted into the object, which in turn authenticates and passes the data to Sharepoint using ODATA REST call.
Below is a bit about Salesforce Triggers:
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.
Typically, you use triggers to perform operations based on specific conditions, to modify related records or restrict certain operations from happening. You can use triggers to do anything you can do in Apex, including executing SOQL and DML or calling custom Apex methods.
Use triggers to perform tasks that can’t be done by using the point-and-click tools in the Salesforce user interface. For example, if validating a field value or updating a field on a record, use validation rules and workflow rules instead.
Triggers can be defined for top-level standard objects, such as Account or Contact, custom objects, and some standard child objects. Triggers are active by default when created. Salesforce automatically fires active triggers when the specified database events occur.
- Apex controller Code:
- global with sharing class Triggercallout {
- public SPSFOAuthTriggercallout() {
- System.debug('Testing SPSFOAuth2');
-
- }
- Public static String EventType {
- get;
- set;
- }
- @future(callout = true)
- global static void HTTPCallOutSPSF(String jsonString) {
-
- List accountList = (List < sObject__c > ) Json.deserialize(jsonString, List < sObject__c > .class);
- for (sObject__ckey: accountList) {
-
- system.debug(key.Event_Type__c);
- EventType = key.Event_Type__c;
- }
- System.debug('New record deserialied ' + accountList);
- System.debug('New record serialized ' + jsonString);
- string val = 'FormData';
- HttpRequest req = new HttpRequest();
- req.setEndpoint('callout:SPSF/_api/web/lists/getbytitle(\'' + String.escapeSingleQuotes(val) + '\')/items');
- req.setMethod('POST');
- req.setHeader('Content-Type', 'application/json;charset=utf-8');
- req.setHeader('Accept', 'application/json');
- JSONGenerator gen = JSON.createGenerator(true);
- gen.writeStartObject();
- gen.writeStringField('Title', EventType);
- gen.writeStringField('EventType', EventType);
- gen.writeEndObject();
- String jsonS = gen.getAsString();
- System.debug('jsongenerated is ' + jsonS);
- req.setBody(jsonS);
- Http http = new Http();
- HTTPResponse res = http.send(req);
- if (res.getStatusCode() == 201) {
- System.debug('This is the status response' + res.getStatus());
- System.debug('after data is received');
- }
- For(String key: res.getHeaderKeys()) {
- System.debug(key + ':' + res.getHeader(key));
- }
- System.debug('Request Body is ' + req.getBody());
- System.debug('Authorization is ' + req.getHeader('Authorization'));
- System.debug('Content-Type is ' + req.getHeader('Content-Type'));
-
- if (res.getStatusCode() != 200) {
- System.debug('Error ' + res.getStatusCode() + ' ' + res.getStatus());
- }
- }
- }
References
- https://sharepointtipstricksmore.blogspot.com/2020/02/sharepoint-salesforce-integration-part.html
- https://spshell.blogspot.com/2015/03/sharepoint-online-o365-oauth.html
- https://help.salesforce.com/articleView?id=admin_files_connect_sp_online_auth.htm&type=5