Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CloneOfferLEXComponentController {
- @AuraEnabled
- public static String serverCloneOffer(String offerId) {
- pba__Offer__c offer = [SELECT Id, Buyer_Email_Address__c, Buyer__c, Coop_Agent__c, Offer_Type__c, pba__OfferedAmount_pb__c, pba__Listing__c FROM pba__Offer__c WHERE Id = :offerId];
- String response; // response after trying to save the cloned offer
- // if the offer doesn't have a listing, we can't clone the offer
- if(offer.pba__Listing__c != null) {
- pba__Listing__c listing = [SELECT Id, Auction__c, Bid_Coordinator__c, Stage__c FROM pba__Listing__c WHERE Id =:offer.pba__Listing__c];
- pba__Offer__c clonedOffer = new pba__Offer__c(
- Buyer__c = offer.Buyer__c,
- Coop_Agent__c = offer.Coop_Agent__c,
- Stage__c = 'Pending Review',
- pba__OfferedAmount_pb__c = offer.pba__OfferedAmount_pb__c,
- Offer_Type__c = listing.Stage__c,
- pba__Listing__c = listing.Id,
- Bid_Coordinator__c = listing.Bid_Coordinator__c,
- Auction__c = listing.Auction__c,
- Recaptured_Buyer__c = true
- );
- // this is done to cause the cloning to fail, for code coverage
- if(Test.isRunningTest() && offer.Buyer_Email_Address__c == 'fail1@email.com') {
- clonedOffer.Stage__c = 'Reviewed';
- clonedOffer.pba__Status__c = 'Subject To';
- }
- Database.SaveResult sr = Database.insert(clonedOffer, false);
- if (sr.isSuccess()) {
- response = sr.getId();
- } else {
- String failureDetail;
- for(Database.Error err : sr.getErrors()) {
- String errorFields = '';
- for(String eF: err.getFields()) {
- errorFields = errorFields + ef + 'n';
- }
- failureDetail = err.getStatusCode() + ': ' + err.getMessage() + '. n' + errorFields;
- }
- AuraHandledException e = new AuraHandledException(failureDetail);
- e.setMessage(failureDetail);
- throw e;
- }
- } else {
- AuraHandledException e = new AuraHandledException('There is no listing associated with this offer.');
- e.setMessage('There is no listing associated with this offer.');
- throw e;
- }
- return response;
- }
- }
- @isTest
- public class CloneOfferLEXComponentControllerTest {
- @isTest static void cloningTest() {
- Account seller = new Account (Name = 'Test Seller');
- insert seller;
- Contact c = new Contact(LastName = 'lastNameTest', Email = 'testBuyerEmail@test.com');
- insert c;
- List<pba__Listing__c> listings = new List<pba__Listing__c>();
- listings.add(new pba__Listing__c (
- Stage__c = 'Pre-Auction',
- pba__Status__c = 'Available',
- Seller__c = seller.Id,
- pba__PostalCode_pb__c = '12345'
- ));
- insert listings;
- List<Auction__c> auctions = new List<Auction__c>();
- auctions.add(new Auction__c (
- Auction_Program__c = 'Dual Path',
- Listing__c = listings[0].Id,
- Auction_Number__c = '5'
- ));
- insert auctions;
- List<pba__Offer__c> offers = new List<pba__Offer__c>();
- // this offer should clone successfully
- offers.add(new pba__Offer__c (
- Auction__c = auctions[0].Id,
- pba__Listing__c = listings[0].Id,
- Buyer_Email_Address__c = 'succeed1@email.com',
- Stage__c = 'Cancelled',
- pba__Status__c = 'Withdrawal',
- Buyer__c = c.Id,
- pba__OfferedAmount_pb__c = 100
- ));
- // this offer will not clone, because we are changing the stage/status
- // in the controller.
- offers.add(new pba__Offer__c (
- Auction__c = auctions[0].Id,
- Buyer_Email_Address__c = 'fail1@email.com',
- pba__Listing__c = listings[0].Id,
- Stage__c = 'Cancelled',
- pba__Status__c = 'Withdrawal',
- Buyer__c = c.Id,
- pba__OfferedAmount_pb__c = 100
- ));
- // this offer will not clone, because it has no listing
- offers.add(new pba__Offer__c (
- Auction__c = auctions[0].Id,
- Buyer_Email_Address__c = 'fail2@email.com',
- Stage__c = 'Cancelled',
- pba__Status__c = 'Withdrawal',
- Buyer__c = c.Id,
- pba__OfferedAmount_pb__c = 100,
- Offer_Type__c = 'Pre-Auction'
- ));
- insert offers;
- Test.startTest();
- CloneOfferLEXComponentController.serverCloneOffer(offers[0].Id);
- try{
- CloneOfferLEXComponentController.serverCloneOffer(offers[1].Id);
- }catch (Exception e) {
- Boolean error = e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION') ? true : false;
- System.AssertEquals(error, true);
- }
- try {
- CloneOfferLEXComponentController.serverCloneOffer(offers[2].Id);
- }catch (Exception e) {
- Boolean error = e.getMessage().contains('There is no listing associated with this offer') ? true : false;
- System.AssertEquals(error, true);
- }
- Test.stopTest();
- // should be the 3 original test offers, and 1 clone, so 4 offers total.
- System.assertEquals(4, [SELECT count() FROM pba__Offer__c]);
- // on an offer, Recaptured_Buyer__c = true signifies the offer was cloned. There should only be one.
- System.assertEquals(1, [SELECT count() FROM pba__Offer__c WHERE Recaptured_Buyer__c = true]);
- }
- }
- try{
- CloneOfferLEXComponentController.serverCloneOffer(offers[1].Id);
- }catch (Exception e) {
- Boolean error = e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION') ? true : false;
- System.AssertEquals(error, true);
- }
Add Comment
Please, Sign In to add comment