Last active
December 26, 2015 11:59
-
-
Save ryanbrainard/7148258 to your computer and use it in GitHub Desktop.
Force.com Code Samples
This file contains hidden or 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
<apex:page standardController="Invoice__c" sidebar="false" showHeader="false" > | |
{!Invoice__c.name}<br/> | |
{!Invoice__c.Status__c} | |
<ul> | |
<apex:repeat value="{!Invoice__c.Line_Items__r}" var="item"> | |
<li>{!item.name}</li> | |
</apex:repeat> | |
</ul> | |
</apex:page> |
This file contains hidden or 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
SELECT | |
Name, | |
Total_Price__c, | |
CreatedById | |
FROM Invoice__c | |
WHERE Status__c = 'Opened' |
This file contains hidden or 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
SELECT | |
Name, | |
Total_Price__c, | |
CreatedBy.Name | |
FROM Invoice__c | |
WHERE Status__c = 'Opened' |
This file contains hidden or 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
SELECT | |
Name, | |
Total_Price__c, | |
CreatedBy.Name, | |
(SELECT Total_Price__c FROM Line_Items__r) | |
FROM Invoice__c | |
WHERE Status__c = 'Opened' |
This file contains hidden or 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
trigger BlockDeleteIfHasLineItems on Merchandise__c (before delete) { | |
Map<Id, Merchandise__c> merchandiseBeingDeleted = Trigger.oldMap; | |
List<Line_Item__c> childLineItems = [SELECT Merchandise__c FROM Line_Item__c WHERE Merchandise__c IN :merchandiseBeingDeleted.keySet()]; | |
for (Line_Item__c li : childLineItems) { | |
merchandiseBeingDeleted.get(li.Merchandise__c).addError('Child line item exists'); | |
} | |
} |
This file contains hidden or 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
@isTest | |
private class BlockDeleteIfHasLineItemsTest { | |
private static testmethod void testDeleteIsBlockedWithLineItems() { | |
Merchandise__c m = new Merchandise__c(); | |
m.Name = 'Jewery'; | |
m.Price__c = 123; | |
m.Quantity__c = 100; | |
INSERT m; | |
Invoice__c i = new Invoice__c(); | |
INSERT i; | |
Line_Item__c li = new Line_Item__c(); | |
li.Invoice__c = i.id; | |
li.Merchandise__c = m.id; | |
li.Quantity__c = 2; | |
INSERT li; | |
try { | |
DELETE m; | |
System.assert(false); | |
} catch(DmlException e) { | |
// expected | |
} | |
} | |
private static testmethod void testDeleteIsNotBlockedWithoutLineItems() { | |
Merchandise__c m = new Merchandise__c(); | |
m.Name = 'Jewery'; | |
m.Price__c = 123; | |
m.Quantity__c = 100; | |
INSERT m; | |
DELETE m; | |
} | |
} |
This file contains hidden or 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
@RestResource(urlMapping='/warehouse/items/*') | |
global with sharing class WarehouseREST { | |
@HttpGet | |
global static List<Line_Item__c> getOpenItemsForMerchandise() { | |
String merchandiseId = RestContext.request.requestURI.substring(RestContext.request.requestURI.lastIndexOf('/')+1); | |
return [ | |
SELECT | |
Invoice__r.Name, | |
Invoice__r.Total_Price__c | |
FROM Line_Item__c | |
WHERE | |
Invoice__r.Status__c = 'Opened' AND | |
Merchandise__c = :merchandiseId | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment