This code implements a helper class in Salesforce's Apex programming...

August 26, 2025 at 06:25 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>();//D-17211ByAnurag5/17/2021for(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);//ENDD-17211-Anurag5/17/2021}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 code implements a helper class in Salesforce's Apex programming language, LoanOppSyncHelper, that performs synchronization between "Loan" and "Opportunity" objects in Salesforce. Let's break it down:

  1. Purpose:

    • The code updates Opportunity records based on changes in corresponding Loan records.
    • It ensures that data between the Loan object and the Opportunity object remains consistent for specific fields and statuses.
  2. Core Components:

    • Constants: The class defines a set of constant strings representing various stages of a loan and opportunity. These constants are used to facilitate synchronization and comparison.
    • Allowed Stages: A Set called allowedStages limits which opportunity stages are eligible for synchronization.
    • Loan and Opportunity Relationships: The code maps loans to opportunities based on field values linking the two.
    • Mapping Configuration:
      • Loan_Opportunity_Stage_Map__c: Maps loan stages to corresponding opportunity stages.
      • Loan_Opportunity_Reverse_Field_Map__c: Specifies which fields should synchronize between loans and opportunities.
  3. Primary Method: syncOpportunityFromLoan

    • This method is triggered when Loan records are updated. It uses the triggerNew and triggerOld parameters to process new and old records, respectively.
    • Initialization: Extracts relevant fields from Loan records (Product Line, Product, Stage, etc.) and fetches corresponding opportunities and product records.
    • Loan and Opportunity Synchronization:
      • Uses the stage mapping (Loan_Opportunity_Stage_Map__c) to update the StageName field in opportunities.
      • Ensures that fields specified in the reverse field map (Loan_Opportunity_Reverse_Field_Map__c) are updated in opportunities based on values in loans.
      • Updates the Opportunity Name dynamically based on the loan amount.
      • Flags opportunities to bypass validation rules temporarily when updates are applied.
    • Field-Level Synchronization: For several opportunity fields (e.g., SunTrust_Amount__c, STI_Product__c), updates are applied only if differences are detected.
    • Mass Update Handling: Collects all updates in a list (oppyList) and performs a bulk update, ensuring efficiency.
  4. Special Cases:

    • Ensures opportunities are updated only if they meet specific conditions. For example:
      • The loan must have a specific status (Open or Hold).
      • Certain stage transitions are not allowed (e.g., if the loan stage is invalid or not mapped).
    • Handles name truncation if the updated name exceeds the character limit (120 characters).
  5. Error Handling:

    • The class includes exception handling mechanisms to log or report any errors that occur during the processing. If an error message is generated, it can be added to the first Loan record to notify users.
  6. Flags and Validation Bypass:

    • Temporarily bypasses validation rules (bypassVRfromLoan__c, Bypass_nCino_VR__c) to ensure updates succeed without triggering unnecessary restrictions during the synchronization.
    • Uses the Loan_To_Opportunity_Bypass_Flag__c object to control the bypass process.
  7. Limits:

    • The method enforces governor limits by using collections (Set, Map) and batch processing concepts, ensuring no more than 50,000 records are queried or processed at a time.

Summary:

This Apex class synchronizes specific fields between Loan and Opportunity objects in Salesforce. It validates loans, updates related opportunities based on predefined mappings, and optimizes for bulk operations. The design considers Salesforce-specific constraints like governor limits and validation bypassing, making it suitable for integration-heavy workflows.

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