Skip to content

Instantly share code, notes, and snippets.

@jsullivanlive
Last active August 29, 2015 14:23
Show Gist options
  • Save jsullivanlive/88668ea7c98aa11b7e04 to your computer and use it in GitHub Desktop.
Save jsullivanlive/88668ea7c98aa11b7e04 to your computer and use it in GitHub Desktop.
Update user story ranks from raw data using data in a json field
Map<String, Id> rank = new Map<String, Id>();
Map<Id, User_Story__c> stories = new Map<Id, User_Story__c>([
select Id, Customer_Story_ID__c, Customer_Story_Raw_Data__c
from User_Story__c
where Project__r.Name = 'projectname'
]);
for (User_Story__c us : stories.values()) {
if (String.isBlank(us.Customer_Story_Raw_Data__c)) continue;
Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(us.Customer_Story_Raw_Data__c);
// rally DragAndDropRank is not unique, so combine with id
String key = (String)data.get('DragAndDropRank') + us.Customer_Story_ID__c;
rank.put(key, us.Id);
}
List<String> sortedRanks = new List<String>();
sortedRanks.addAll(rank.KeySet());
sortedRanks.sort();
List<User_Story__c> toUpsert = new List<User_Story__c>();
for (Integer i=0; i<sortedRanks.size(); i++) {
String thisRank = sortedRanks.get(i);
toUpsert.add(
new User_Story__c(
Id = rank.get(thisRank),
Rank__c = i
)
);
}
upsert toUpsert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment