Last active
November 2, 2020 00:18
-
-
Save reetasingh/265f05a6643a612733af532421d636b6 to your computer and use it in GitHub Desktop.
CANVAS_TO_SALESFORCE_INTEGRATION
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This class makes an HTTP GET request to canvas API and inserts the data received from canvas API in salesforce | |
// create a class which can be called on a schedule | |
global class FinalTCS implements Schedulable, Database.AllowsCallouts{ | |
public void execute(SchedulableContext SC) { | |
callWebService(); | |
} | |
@future(callout=true) | |
public static void callWebService() { | |
// make a get request to canvas API | |
//CanvasCallout.getUsers();` | |
System.debug('Started the callout execution'); | |
Http http = new Http(); | |
HttpRequest request = new HttpRequest(); | |
request.setEndpoint('https://<canvas_link>/api/v1/courses'); | |
request.setMethod('GET'); | |
request.setHeader('Authorization', <authorization_token>); | |
HttpResponse response = http.send(request); | |
System.debug('Response code '+ response.getStatusCode()); | |
if (response.getStatusCode() == 200) { | |
// insert the data in salesforce | |
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody()); | |
System.debug('Received the following response:'); | |
Map<String, Object> usersMap = new Map<String, Object>(); | |
for (Object result: results) { | |
System.debug(result); | |
Map<String, Object> user = (Map<String, Object>) result; | |
usersMap.put(String.valueOf(user.get('name')), result); | |
} | |
Set<String> names = usersMap.keySet(); | |
List<SObject> contacts = [Select ID, Name,Test__c ,COVID_19__c from Cycle__c Where Name IN :names]; | |
System.debug('Found cycles ' + contacts.size()); | |
Map<String, SObject> contactMap = new Map<String, SObject>(); | |
for(SObject contact : contacts){ | |
contactMap.put(String.valueOf(contact.get('Name')), contact); | |
} | |
System.debug('Existing cycles list '+ JSON.serialize(contactMap)); | |
List<SObject> contactsToUpdate = new List<SObject>(); | |
System.debug('Users list size fetched is ' + usersMap.size()); | |
for(String name : usersMap.keySet()){ | |
if(contactMap.containsKey(name)){ | |
System.debug('Name '+ name); | |
System.debug('ContactMap again ' + JSON.serialize(contactMap)); | |
SObject existingContact = contactMap.get(name); | |
System.debug('existingContact before '+ JSON.serialize(existingContact)); | |
existingContact.put('Test__c', String.valueOf(usersMap.get('name'))); | |
System.debug('existingContact after '+ JSON.serialize(existingContact)); | |
contactsToUpdate.add(existingContact); | |
} else { | |
//No need to create new contacts! | |
} | |
} | |
System.debug('Final list to update ' + JSON.serialize(contactsToUpdate)); | |
Database.update(contactsToUpdate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment