Skip to content

Instantly share code, notes, and snippets.

@theWill
Created September 11, 2014 22:31
Show Gist options
  • Save theWill/b3252b132e0dcb171cc4 to your computer and use it in GitHub Desktop.
Save theWill/b3252b132e0dcb171cc4 to your computer and use it in GitHub Desktop.
Estimator - query
// doing the DataService.getResourcesForEstimate to do a query against SFDC
// has a subquery for Estimate_Resource_Weeks__r that are returning results but the data is overwritten somehow
// if the DataService.getWeeksForEstimate block of code is commented out then the results come through correctly
// Note: this is not the orginal file but should be the relevant code needed...
DataService.getResourcesForEstimate(DataService.recordId).then(function (resources){
console.log("resources", resources)
//TODO - comment out this part causes the code to NOT lose data
DataService.getWeeksForEstimate(DataService.recordId).then(function (weeks){
// we should set weeks first, because setResources function depends on weeks being sorted
DataService.setWeeks(weeks);
DataService.setResources(resources);
if (record.Ratecard_JSON__c) {
var ratecards = JSON.parse(record.Ratecard_JSON__c);
DataService.setRatecard(ratecards);
loadedRecord.isLoaded = true;
callback && $timeout(callback);
}
else {
//old estimate which wasn't created with a Ratecard_JSON__c record - load the USD one and save it
DataService.getRateCard('USD').then(function(ratecards) {
$log.log('loaded ratecard', ratecards);
var fieldVals = {
Ratecard_JSON__c: JSON.stringify(ratecards)
};
DataService.commit2SFDC(fieldVals, function commit2SFDC() {
DataService.setRatecard(ratecards);
loadedRecord.isLoaded = true;
callback && $timeout(callback);
});
});
}
});
//TODO - end of comment block
});
getResourcesForEstimate: function(estimateId){
return resourceQuery(estimateId).then(function(response) {
if (response && response.data && response.data.records) {
var resp = response.data.records;
return resp;
} else {
return $q.reject(response.data);
}
});
},
var resourceQuery = function(estimateId) {
return SalesforceService.query("SELECT Id, Name, Role__c, Skill__c, Estimate__c, Order__c, Rate__c, Cost__c, (Select Hours__c FROM Estimate_Resource_Weeks__r) FROM CMC_Estimate_Resource__c where Estimate__c = '" + estimateId + "' ORDER BY Order__c");
};
//Use the Query resource to execute a SOQL Query
SalesforceService.query = function(q) {
var escapedQuery = "/" + sfVersion + "/query/?q=" + q.replace(/ /g, "+");
return $http.get(cmcSFProxyEndPoint, {
params: {
q: escapedQuery
}
});
};
getWeeksForEstimate: function(estimateId){
return weeksQuery(estimateId).then(function(response) {
if (response && response.data && response.data.records) {
// auto convert date type whenever fetched from storage
angular.forEach(response.data.records, function (week) {
console.log("week", week);
if (week.Date__c) {
var utc = new Date(week.Date__c);
// convert date because there is no tz component displayed in UI
// this makes sure anyone in the world will see the same Day/Month/Year value for this record
// Note: when saving, it also has to be in UTC to be consistent
week.Date__c = new Date(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate());
}
});
return response.data.records;
} else {
return $q.reject(response.data);
}
});
},
var weeksQuery = function(estimateId) {
return SalesforceService.query("Select Id, Name, Phase__c, Date__c, Milestone__c, Order__c, Estimate__c FROM CMC_Estimate_Week__c where Estimate__c = '" + estimateId + "' ORDER BY Order__c");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment