Last active
December 31, 2015 14:49
-
-
Save pchittum/8002683 to your computer and use it in GitHub Desktop.
INSSET Salesforce Programmer Training
This file contains 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 HandleProductPriceChange on Merchandise__c (after update) { | |
List<Line_Item__c> openLineItems = | |
[SELECT j.Unit_Price__c, j.Merchandise__r.Price__c | |
FROM Line_Item__c j | |
WHERE j.Invoice__r.Status__c = 'Negotiating' | |
AND j.Merchandise__r.id IN :Trigger.new | |
FOR UPDATE]; | |
for (Line_Item__c li: openLineItems) { | |
if ( li.Merchandise__r.Price__c < li.Unit_Price__c ){ | |
li.Unit_Price__c = li.Merchandise__r.Price__c; | |
} | |
} | |
//moyens d'évoquer modification base de données | |
// update openLineItems; | |
// Database.update(openLineItems); | |
//failures in the above examples throw DMLException; | |
List<Database.Saveresult> srs = Database.update(openLineItems,false); | |
for (Database.SaveResult sr : srs) { | |
if (!sr.isSuccess()){ | |
System.debug('The line item record ' +sr.getId()+ ' did not update successfully ' +sr.getErrors()); | |
} | |
} | |
} |
This file contains 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
/* | |
The following represents two separate classes in Apex. You must create them both. | |
You cannot add these to the same class. | |
*/ | |
public class MathHelper{ | |
public static Integer add(Integer a, Integer b){ | |
return a+b; | |
} | |
} | |
@istest | |
public class MathHelperTest{ | |
@istest | |
public static void testAddTwoNumbers(){ | |
Integer val1 = 2; | |
Integer val2 = 3; | |
Integer product = MathHelper.add(val1,val2); | |
System.assert(product==5, 'the product does not equal 5'); | |
System.assertEquals(5, product); | |
System.assertNotEquals(6, product); | |
//code to show that any changes written to the DB are not committed during test execution | |
Merchandise__c m = new Merchandise__c(name='Père Noël',Price__c=10,Quantity__c=100); | |
insert m; | |
Merchandise__c merchandiseFromDB = [SELECT name from Merchandise__c where name = 'Père Noël']; | |
System.debug(merchandiseFromDB); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment