Skip to content

Instantly share code, notes, and snippets.

@leehildebrand
Last active September 23, 2022 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leehildebrand/5b503205cf1eeaaa82aae33faa33881d to your computer and use it in GitHub Desktop.
Save leehildebrand/5b503205cf1eeaaa82aae33faa33881d to your computer and use it in GitHub Desktop.
GrowthLink Sync Script
//SYNC UP WHERE CASE AND ISSUE EXIST
// Call Schema to get the report metadata
Reports.ReportMetadata reportMd = Reports.ReportManager.describeReport('00O3c000007LMmHEAW').getReportMetadata();
// Run the report and store the rows in a Reports.ReportFactWithDetail object
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)Reports.ReportManager.runReport('00O3c000007LMmHEAW', reportMd, true).getFactMap().get('T!T');
// For each row the report returns, update the Case
for(Reports.ReportDetailRow detailRow : factDetails.getRows()){
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type','application/json');
req.setEndpoint('https://growthlinkhca.my.salesforce.com/services/data/v51.0/sobjects/Issue__c/'+Id.ValueOf(String.ValueOf(detailRow.getDataCells()[0].getLabel())));
req.setBody('{}');
req.setMethod('PATCH');
req.setTimeout(120000);
Http h = new Http();
HttpResponse res = h.send(req);
system.debug(req.getEndpoint());
system.debug(res.getStatusCode());
system.debug(res.getBody());
req.setEndpoint('https://growthlinkhca.my.salesforce.com/services/data/v51.0/sobjects/Case/'+Id.ValueOf(String.ValueOf(detailRow.getDataCells()[1].getLabel())));
res = h.send(req);
system.debug(req.getEndpoint());
system.debug(res.getStatusCode());
system.debug(res.getBody());
}
//USE ANOTHER REPORT TO CATCH STRAGGLERS
// Call Schema to get the report metadata
reportMd = Reports.ReportManager.describeReport('00O3c000007v2BPEAY').getReportMetadata();
// Run the report and store the rows in a Reports.ReportFactWithDetail object
factDetails = (Reports.ReportFactWithDetails)Reports.ReportManager.runReport('00O3c000007v2BPEAY', reportMd, true).getFactMap().get('T!T');
// For each row the report returns, update the Case
for(Reports.ReportDetailRow detailRow : factDetails.getRows()){
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type','application/json');
req.setEndpoint('https://growthlinkhca.my.salesforce.com/services/data/v51.0/sobjects/Issue__c/'+Id.ValueOf(String.ValueOf(detailRow.getDataCells()[0].getLabel())));
req.setBody('{}');
req.setMethod('PATCH');
req.setTimeout(120000);
Http h = new Http();
HttpResponse res = h.send(req);
system.debug(req.getEndpoint());
system.debug(res.getStatusCode());
system.debug(res.getBody());
req.setEndpoint('https://growthlinkhca.my.salesforce.com/services/data/v51.0/sobjects/Case/'+Id.ValueOf(String.ValueOf(detailRow.getDataCells()[1].getLabel())));
res = h.send(req);
system.debug(req.getEndpoint());
system.debug(res.getStatusCode());
system.debug(res.getBody());
}
//SYNC UP WHERE CASE DOES NOT EXIST
// Call Schema to get the report metadata
reportMd = Reports.ReportManager.describeReport('00O3c000007uvWzEAI').getReportMetadata();
// Run the report and store the rows in a Reports.ReportFactWithDetail object
factDetails = (Reports.ReportFactWithDetails)Reports.ReportManager.runReport('00O3c000007uvWzEAI', reportMd, true).getFactMap().get('T!T');
// For each row the report returns, update the Case
for(Reports.ReportDetailRow detailRow : factDetails.getRows()){
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type','application/json');
req.setEndpoint('https://growthlinkhca.my.salesforce.com/services/data/v51.0/sobjects/Issue__c/'+Id.ValueOf(String.ValueOf(detailRow.getDataCells()[0].getLabel())));
req.setBody('{}');
req.setMethod('PATCH');
req.setTimeout(120000);
Http h = new Http();
HttpResponse res = h.send(req);
system.debug(req.getEndpoint());
system.debug(res.getStatusCode());
system.debug(res.getBody());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment