Skip to main content
Mike Callaghan (SunTrust) 님이 #Apex에 질문했습니다
I have run into a myriad of issues with process builder and have decided to build a trigger.

LLC_BI__Loan__c is the 'Parent'

LLC_BI__Loan_Collateral2__c is the 'Child'

I wanted to update the Stage_Number__c field on the child record with a number based on the stage of the parent when a stage is changed.  

trigger LoanStageNumberUpdate on LLC_BI__Loan__c (after update) {

 

 for(LLC_BI__Loan__c loan: Trigger.new){

  LLC_BI__Loan__c oldloan = Trigger.oldMap.get(loan.Id);

  if(oldloan.LLC_BI__Stage__c != loan.LLC_BI__Stage__c)

   { 

    List<LLC_BI__Loan_Collateral2__c> collateral = [ SELECT Id, LLC_BI__Loan__c FROM LLC_BI__Loan_Collateral2__c WHERE LLC_BI__Loan__c = :loan.Id];

    List<LLC_BI__Loan_Collateral2__c> newcollateral = new List<LLC_BI__Loan_Collateral2__c>();

 

    for(LLC_BI__Loan_Collateral2__c col: collateral)

    {

        if(loan.LLC_BI__Stage__c == 'Pre-Underwriting'){col.Stage_Number__c = 1;}

            else if(loan.LLC_BI__Stage__c == 'Underwriting'){col.Stage_Number__c = 2;}

              else if(loan.LLC_BI__Stage__c == 'Credit Decision'){col.Stage_Number__c = 3;}   

                  else if(loan.LLC_BI__Stage__c == 'Client Decision'){col.Stage_Number__c = 4;}   

                      else if(loan.LLC_BI__Stage__c == 'Pre-Closing'){col.Stage_Number__c = 5;}   

                          else if(loan.LLC_BI__Stage__c == 'Doc Prep'){col.Stage_Number__c = 6;}  

                              else if(loan.LLC_BI__Stage__c == 'Doc Review'){col.Stage_Number__c = 7;}  

                                  else if(loan.LLC_BI__Stage__c == 'Post-Closing'){col.Stage_Number__c = 8;}   

                                      else if(loan.LLC_BI__Stage__c == 'Funding/Booking'){col.Stage_Number__c = 9;}   

    }

           {

       if (newcollateral.isEmpty() == false){

       update newcollateral;

      }

    }

  }

}

}
답변 7개
  1. 2016년 2월 9일 오후 10:11
    well - first of all - you will get into trouble by putting the SOQL in the for loop of the Trigger.new.  You should never do a SOQL in a loop.  There is a limit of 100 SOQL's in any invocation.  And a trigger can be called with up-to 200 records in the trigger.new list.

    But in this case - I think you should be able to just do one SOQL to get all of the details, and then as you're looping through those details, get the matching parent - and test the stage, then updating the detail stage number.

    More of something like this...

    trigger loanStageNumberUpdate ON LLC_BI__Loan__c (after update) {

    list<LLC_BI__Loan_Collateral2__c> Collaterals = new list<LLC_BI__Loan_Collateral2__c>([SELECT id,LLC_BI__Loan__c,Stage_Number__c FROM LLC_BI__Loan_Collateral2__c WHERE LLC_BI__Loan__c IN :trigger.newmap.keyset()]);

      for(LLC_BI__Loan_Collateral2__c collateral :Collaterlas) {

    if(trigger.newmap.containsKey(collateral.LLC_BI__Loan__c)) {

    string stage = trigger.newmap.get(Acollateral.LLC_BI__Loan__c).LLC_BI__Stage__c;

    if(stage == 'Pre-Underwriting') collateral.Stage_Number__c = 1;

    if(stage == 'Underwriting') collateral.Stage_Number__c = 2;

    if(stage == 'Credit Decision') collateral.Stage_Number__c = 3;

    if(stage == 'Client Decision') collateral.Stage_Number__c = 4;

    if(stage == 'Pre-Closing') collateral.Stage_Number__c = 5;

    if(stage == 'Doc Prep') collateral.Stage_Number__c = 6;

    if(stage == 'Doc Review') collateral.Stage_Number__c = 7;

    if(stage == 'Post-Closing') collateral.Stage_Number__c = 8;

    if(stage == 'Funding/Booking') collateral.Stage_Number__c = 9;

    }

    }

    if(collaterals.size()>0) {

    update collaterals;

    }

    }

     
0/9000