This Apex class, `LoanOppSyncHelper`, is a Salesforce Apex helper class...

August 26, 2025 at 06:26 PM

publicwithsharingclassLoanOppSyncHelper{publicstaticfinalStringUNDERSCORE='_';publicstaticfinalStringPRE_UNDERWRITING='Pre-Underwriting';publicstaticfinalStringUNDERWRITING='Underwriting';publicstaticfinalStringCLIENT_DECISION='ClientDecision';publicstaticfinalStringPRE_CLOSING='Pre-Closing';publicstaticfinalStringCREDIT_DECISION='CreditDecision';publicstaticfinalStringDOC_PREP='DocPrep';publicstaticfinalStringDOC_REVIEW='DocReview';publicstaticfinalStringPOST_CLOSING='Post-Closing';publicstaticfinalStringBOOKING_APPROVAL_ONLY='Booking-ApprovalOnly';publicstaticfinalStringOPPY_STAGE_1='2-ScreenorDevelopProposal';publicstaticfinalStringOPPY_STAGE_2='3-PitchedorInCredit';publicstaticfinalStringOPPY_STAGE_3='4-ApprovedorEngaged';publicstaticfinalStringOPPY_STAGE_4='5-AcceptedorInMarket';publicstaticfinalSet<String>CIB_OPP_RECORD_TYPES=newSet<String>{'AF','SLF','General'};publicstaticvoidsyncOpportunityFromLoan(List<LLC_BI__Loan__c>triggerNew,Map<Id,LLC_BI__Loan__c>triggerOld){list<opportunity>oppyList=newlist<opportunity>();map<String,Id>prodKey_prodId_map=newmap<String,Id>();map<id,opportunity>loanId_oppyObj_map=newmap<id,opportunity>();set<String>prodLinesList=newSet<String>();set<String>prodTypesList=newSet<String>();set<String>prodNamesList=newSet<String>();set<String>allowedStages=newSet<String>{OPPY_STAGE_1,OPPY_STAGE_2,OPPY_STAGE_3,OPPY_STAGE_4};set<id>idList=newset<Id>();booleanflag=false;StringstageName;opportunityoppyObj=newopportunity();Map<Id,String>loanStagesinAction=newMap<Id,String>();for(LLC_BI__Loan__ctemp:triggerNew){idList.add(temp.id);prodLinesList.add(temp.LLC_BI__Product_Line__c);prodTypesList.add(temp.LLC_BI__Product_Type__c);prodNamesList.add(temp.LLC_BI__Product__c);loanStagesinAction.put(temp.Id,temp.LLC_BI__Stage__c);}idList.remove(null);if(idList!=null&&!idList.isEmpty()){List<opportunity>oppList=[SELECTId,Name,LLC_BI__Loan__r.LLC_BI__Stage__c,Precision_Lender_Shortkey__c,CloseDate,Amount,Sub_ProductLU__c,Sub_ProductLU__r.Name,StageName,SunTrust_Amount__c,Transaction_Size__c,recordtype.name,recordtype.developername,Initial_Application_Date__c,STI_Product__c,Opportunity_LOB__c,Appraisal_Disclosure_Mail_Date__cFROMOpportunitywhereLLC_BI__Loan__cIN:idListANDStageNameIN:allowedStageslimit50000];for(OpportunitytempOppy:oppList){loanId_oppyObj_map.put(tempOppy.LLC_BI__Loan__c,tempOppy);}}if(loanId_oppyObj_map!=null&&!loanId_oppyObj_map.isEmpty()){for(Product2productObj:[SelectId,nCino_Product__c,ProductCode,Family,NameFromProduct2WhereNameIN:prodNamesListANDProductCodeIN:prodLinesListANDFamilyIN:prodTypesListlimit50000]){prodKey_prodId_map.put(productObj.Name+UNDERSCORE+productObj.productCode+UNDERSCORE+productObj.Family,productObj.id);}Map<String,String>mapLoanOppStage=newMap<String,String>();for(Loan_Opportunity_Stage_Map__cstage:[SELECTId,Loan_Stage__c,Opportunity_Stage__cFROMLoan_Opportunity_Stage_Map__c]){mapLoanOppStage.put(stage.Loan_Stage__c,stage.Opportunity_Stage__c);}system.debug('mapLoanOppStage++'+mapLoanOppStage);List<Loan_Opportunity_Reverse_Field_Map__c>listLoanOppReverse=newList<Loan_Opportunity_Reverse_Field_Map__c>();listLoanOppReverse=[SELECTId,Name,Source_Field__c,Target_Field__c,Conditional_Statement__c,At_What_Loan_Stage__cFROMLoan_Opportunity_Reverse_Field_Map__c];for(LLC_BI__Loan__ctempLoan:triggerNew){if(null==tempLoan||null==tempLoan.Opportunity__c||null!=tempLoan.LLC_BI__Highest__c)continue;if(null==tempLoan.LLC_BI__Status__c||(!tempLoan.LLC_BI__Status__c.equalsIgnoreCase('Open')&&!tempLoan.LLC_BI__Status__c.equalsIgnoreCase('Hold')))continue;if(!loanId_oppyObj_map.containsKey(tempLoan.id))continue;oppyObj=loanId_oppyObj_map.get(tempLoan.id);if(oppyObj!=null){flag=false;if(String.isNotBlank(tempLoan.LLC_BI__Stage__c)&&oppyObj.Opportunity_LOB__c!='CIB'){if(tempLoan.LLC_BI__Stage__c!=triggerOld.get(tempLoan.Id).LLC_BI__Stage__c&&mapLoanOppStage.get(tempLoan.LLC_BI__Stage__c)<>NULL&&oppyObj.StageName<>mapLoanOppStage.get(tempLoan.LLC_BI__Stage__c)){oppyObj.StageName=mapLoanOppStage.get(tempLoan.LLC_BI__Stage__c);oppyObj.bypassVRfromLoan__c=true;flag=true;oppyObj.Bypass_nCino_VR__c=true;oppyObj.Bypass_nCino_VR_Date_Time__c=System.now().addminutes(1);elseif(!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(PRE_UNDERWRITING)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(UNDERWRITING)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(CREDIT_DECISION)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(CLIENT_DECISION)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(PRE_CLOSING)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(DOC_PREP)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(DOC_REVIEW)&&!tempLoan.LLC_BI__Stage__c.equalsIgnoreCase(POST_CLOSING)){continue;}if(listLoanOppReverse<>NULL&&flag){for(Loan_Opportunity_Reverse_Field_Map__coFieldMap:listLoanOppReverse){if(oFieldMap.Source_Field__c<>NULL&&oFieldMap.Target_Field__c<>NULL){if(oFieldMap.Target_Field__c=='SunTrust_Amount__c'){System.debug('####Inside1');if(tempLoan.New_Money__c<=0){if(oppyObj.SunTrust_Amount__c!=tempLoan.SunTrust_Request_Amount__c){oppyObj.SunTrust_Amount__c=tempLoan.SunTrust_Request_Amount__c;flag=true;}}elseif(tempLoan.New_Money__c>0){if(oppyObj.SunTrust_Amount__c!=tempLoan.New_Money__c){oppyObj.SunTrust_Amount__c=tempLoan.New_Money__c;flag=true;}}}elseif(oFieldMap.Target_Field__c=='Name'){Stringstr1=oppyObj.Name.substringBeforeLast('-$');StringupdatedOppName=str1+'-$'+String.valueOf(oppyObj.SunTrust_Amount__c.format());System.debug('####Nameis='+updatedOppName);if(updatedOppName<>''&&updatedOppName<>NULL){if(updatedOppName.length()>120){if(oppyObj.Name!=updatedOppName.substring(0,120)){oppyObj.Name=updatedOppName.substring(0,120);flag=true;}}else{if(oppyObj.Name!=updatedOppName){oppyObj.Name=updatedOppName;flag=true;}}}}elseif(oFieldMap.Target_Field__c=='STI_Product__c'||oFieldMap.Target_Field__c=='Sub_ProductLU__c'){if(tempLoan.LLC_BI__Product__c!=oppyObj.Sub_ProductLU__r.Name){if(oppyObj.STI_Product__c!=tempLoan.LLC_BI__Product_Type__c){oppyObj.STI_Product__c=tempLoan.LLC_BI__Product_Type__c;flag=true;}if(oppyObj.Sub_ProductLU__c!=prodKey_prodId_map.get(tempLoan.LLC_BI__Product__c+'_'+tempLoan.LLC_BI__Product_Line__c+'_'+tempLoan.LLC_BI__Product_Type__c)){oppyObj.Sub_ProductLU__c=prodKey_prodId_map.get(tempLoan.LLC_BI__Product__c+'_'+tempLoan.LLC_BI__Product_Line__c+'_'+tempLoan.LLC_BI__Product_Type__c);flag=true;}}}elseif(oFieldMap.Target_Field__c=='Appraisal_Disclosure_Mail_Date__c'){if(oppyObj.Appraisal_Disclosure_Mail_Date__c!=tempLoan.Appraisal_Disclosure_Mail_Date__c){oppyObj.Appraisal_Disclosure_Mail_Date__c=tempLoan.Appraisal_Disclosure_Mail_Date__c;flag=true;}}else{System.debug('####Inside3');if(tempLoan.get(oFieldMap.Source_Field__c)!=triggerOld.get(tempLoan.Id).get(oFieldMap.Source_Field__c)){oppyObj.put(oFieldMap.Target_Field__c,tempLoan.get(oFieldMap.Source_Field__c));flag=true;}if(oppyObj.SunTrust_Amount__c==null&&oppyObj.SunTrust_Amount__c!=0){oppyObj.SunTrust_Amount__c=0;flag=true;}}}}if(flag){oppyList.add(oppyObj);}}}if(oppyObj.Precision_Lender_Shortkey__c!=tempLoan.Precision_Lender_Shortkey__c){oppyObj.Precision_Lender_Shortkey__c=tempLoan.Precision_Lender_Shortkey__c;oppyList.add(oppyObj);}}}if(!oppyList.isEmpty()){Loan_To_Opportunity_Bypass_Flag__cbyPassFlag=Loan_To_Opportunity_Bypass_Flag__c.getOrgDefaults();try{if(byPassFlag!=null&&!byPassFlag.ByPassFlag__c){byPassFlag.ByPassFlag__c=true;UPDATEbyPassFlag;}TS_OpportunityTriggerHandler.Test_BypassOpportunityFlag=TRUE;updateoppyList;TS_OpportunityTriggerHandler.Test_BypassOpportunityFlag=FALSE;}catch(Exceptione){StringErrorMessage=HandlingExceptions.createExceptionMsg(e);if(ErrorMessage!=Null){if(!UserDetails.getUserDetails(UserInfo.getUserId()).Profile.Name.equalsIgnoreCase('Integration')){triggerNew[0].addError(ErrorMessage);}}}finally{if(byPassFlag!=null&&byPassFlag.ByPassFlag__c){byPassFlag.ByPassFlag__c=false;UPDATEbyPassFlag;}}}}}}

This Apex class, LoanOppSyncHelper, is a Salesforce Apex helper class designed to synchronize Opportunity records with corresponding Loan records in Salesforce. Here's a breakdown of its functionality:


Key Points of Functionality:

  1. Constants and Configurations:

    • The class starts with several public static final String constants that define specific stage names or statuses (e.g., PRE_UNDERWRITING, UNDERWRITING).
    • It also defines lists, sets, and maps used for record processing or business logic, such as CIB_OPP_RECORD_TYPES and allowedStages.
  2. Primary Method (syncOpportunityFromLoan):

    • Purpose:
      • This method ensures that important field changes or stage updates in Loan records (LLC_BI__Loan__c) are reflected in their associated Opportunity records.
    • Inputs:
      • triggerNew: The new versions of Loan records (e.g., after an update).
      • triggerOld: A map of Loan records prior to changes.
    • Logic:
      • Initializes various collections (List, Set, and Map) to track loans, opportunities, product types, or updates.
      • Filters Opportunity records linked to the Loan records in the trigger and stores the relationships in the loanId_oppyObj_map.
  3. Business Logic:

    • Stage Mapping:
      • It leverages a Loan_Opportunity_Stage_Map__c custom object for mapping Loan stages to Opportunity stages. It updates opportunities only when the stage mapping exists and changes are valid.
    • Reverse Field Mapping:
      • Uses a Loan_Opportunity_Reverse_Field_Map__c custom object, which seems to define field-level mappings between the Loan and Opportunity.
      • Updates specific fields (e.g., Name, SunTrust_Amount__c, STI_Product__c) in Opportunity based on changes in Loan.
    • Field Updates:
      • If certain Loan fields (such as SunTrust_Amount__c, Precision_Lender_Shortkey__c, etc.) differ from the related Opportunity, those fields are updated in the Opportunity.
  4. Batching and Bulk Updates:

    • Tracks all modified Opportunity records and updates them in bulk to ensure Salesforce governor limits are respected.
  5. Validation and Conditions:

    • Contains conditions to ensure updates only occur if the Loan record meets criteria (Status of Open or Hold).
    • Skips processing if certain required fields are null or irrelevant.
  6. Error Handling:

    • Wraps part of its logic in a try-catch-finally block to catch and handle exceptions during updates. Errors here are managed and potentially added at the Loan record level for visibility.
  7. Helper Functionality:

    • Uses Loan_To_Opportunity_Bypass_Flag__c (a likely custom setting or metadata) to bypass certain validations temporarily during the sync process.

Overall Purpose:

The class is designed to:

  • Modify or synchronize Opportunity records based on updates to related Loan records.
  • Ensure that certain fields (stages, amounts, etc.) in opportunities accurately reflect details from loans.
  • Handle changes only if specific business logic conditions are satisfied.
  • Maintain integrity by validating required fields and using custom configuration records (Loan_Opportunity_Stage_Map__c, Loan_Opportunity_Reverse_Field_Map__c) as mappings and control logic.

This class is likely part of a larger Salesforce implementation automating processes in a financial or loan-related domain, ensuring consistency between different entity types.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node