Skip to content

Instantly share code, notes, and snippets.

@jovabe
Last active August 29, 2015 13:57
Show Gist options
  • Save jovabe/9390465 to your computer and use it in GitHub Desktop.
Save jovabe/9390465 to your computer and use it in GitHub Desktop.
Apex Code Examples
public static void checkForOrderItemInfo(List<System_Order_Item_Info__c> orderItemInfos)
{
Set<Id> orderItemSet = new Set<Id>();
List<OrderItem> orderItems = new List<OrderItem>();
List<System_Order_Item_Info__c> allOrderItemInfos = new List<System_Order_Item_Info__c>();
Map<Id,Id> mapOrderItemToOrderItemInfo = new Map<Id,Id>();
List<OrderItem> orderItemsToUpdate = new List<OrderItem>();
// Extract all the parent order items for the order item infos being inserted or deleted
for(System_Order_Item_Info__c orderItemInfo : orderItemInfos)
{
orderItemSet.add(orderItemInfo.Order_Product__c);
}
orderItems = [
SELECT Id, Has_Order_Item_Info__c
FROM OrderItem
WHERE Id IN : orderItemSet
];
// Load all order item infos, not only the ones being inserted or deleted
allOrderItemInfos = [
SELECT Id, Order_Product__c
FROM System_Order_Item_Info__c
WHERE Order_Product__c IN : orderItemSet
];
// Map all order items to its order item info if it exists
for(System_Order_Item_Info__c orderItemInfo : allOrderItemInfos)
{
mapOrderItemToOrderItemInfo.put(orderItemInfo.Order_Product__c, orderItemInfo.Id);
}
// Loop over all order items, check if they have a mapping to an order item info,
// if yes, update the checkbox when needed
for (OrderItem orderItem : orderItems)
{
if (orderItem.Has_Order_Item_Info__c != (mapOrderItemToOrderItemInfo.get(orderItem.Id) != null) )
{
if (mapOrderItemToOrderItemInfo.get(orderItem.Id) != null)
{
orderItem.Has_Order_Item_Info__c = true;
}
else
{
orderItem.Has_Order_Item_Info__c = false;
}
orderItemsToUpdate.add(orderItem);
}
}
update orderItemsToUpdate;
}
List<Account> accs = [SELECT Id, Name, (SELECT Id FROM Contacts) contacts FROM Account WHERE Id = '001D0000010xNAQ'];
for (account acc : accs)
{
system.debug('ABSILOG *** Account: ' + acc.Name + ' , Contact Amount: ' + acc.contacts.size());
}
private static Object getValueInCorrectDataType(Sobject sOb, String sField, String sValue)
{
// Convert value from a string to the correct data type of the field
// and return the new data type
Object retOb = sValue;
Schema.Displaytype fieldType = sOb.getSOBjectType().getDescribe().Fields.getMap().get(sField).getDescribe().getType();
if (fieldType==Schema.Displaytype.Id)
{
Id retObBool = Id.valueOf(sValue);
retOb = retObBool;
}
if (fieldType==Schema.Displaytype.Boolean)
{
Boolean retObBool = Boolean.valueOf(sValue);
retOb = retObBool;
}
if (fieldType==Schema.Displaytype.Date)
{
Date retObDate = Date.valueOf(sValue);
retOb = retObDate;
}
if (fieldType==Schema.Displaytype.DateTime)
{
DateTime retObDateTime = DateTime.valueOf(sValue);
retOb = retObDateTime;
}
if (fieldType==Schema.Displaytype.Double)
{
Double retObDouble = Double.valueOf(sValue);
retOb = retObDouble;
}
if (fieldType==Schema.Displaytype.Currency)
{
Double retObDouble = Double.valueOf(sValue);
retOb = retObDouble;
}
if (fieldType==Schema.Displaytype.Integer)
{
Integer retObInteger = Integer.valueOf(sValue);
retOb = retObInteger;
}
return retOb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment