Arjun Gupta August 17 -Technical PD1 Batch

Discussion in 'Salesforce Certification' started by Arjun Gupta_1, Aug 17, 2019.

  1. David Hamilton_1

    Joined:
    Jul 31, 2019
    Messages:
    4
    Likes Received:
    0
    So expanding on our project. I had added a custom field "Bill_Month__c" This is a selectable field to match the billing dates. The idea behind this is two fold.

    1) Easily see all bills for an individual based on month.
    2) To keep a duplicate record from being crated for the same customer and month. In order to accomplish this I added an additional method to my Handler Class. This method is trigger Before an Insert or Update. In order to get it to work I created two variables and two IF statements. How can this be improved on?


    public static void checkDupRecord(List<Bill__c> BillRecords){

    for(Bill__c billRecord : billRecords){

    Bill__c[] dupRec = [Select Id From Bill__c Where Customer__c =: billRecord.Customer__c And Bill_Month__c =: billRecord.Bill_Month__c];
    // Sized Used so no error woudl be thrown in the event no record found for insert.
    If (dupRec.size() > 0){
    ID dupId = [Select Id From Bill__c Where Customer__c =: billRecord.Customer__c And Bill_Month__c =: billRecord.Bill_Month__c].Id;
    If (dupId != billRecord.id){
    billRecord.addError('This Bill Record for this Month has already been processed '+dupRec);
    }
    }
    }
    }
     
    #51
  2. Arjun Gupta_1

    Arjun Gupta_1 Customer
    Customer

    Joined:
    Jan 30, 2019
    Messages:
    91
    Likes Received:
    42
    Hey Soujanya, Whenever you check an assert, make sure you query again on the object to see what got stored latest
     
    #52
  3. Arjun Gupta_1

    Arjun Gupta_1 Customer
    Customer

    Joined:
    Jan 30, 2019
    Messages:
    91
    Likes Received:
    42
    Hey David, i would recommend you to once see the duplicate rules on objects in Salesforce config too
     
    #53

Share This Page