+ Start a Discussion
Forrest MulduneForrest Muldune 

Update and Review Apex code

All

I am kind of new to Apex coding and I was wondering if anyone could help me with the updating the apex trigger below my Request.

In the Consumer__c picklist field in the Loan__c custom object, contains the values
  •  Not reviewed
  •  Consumer
  •  Non-consumer
  •  Insufficient information 
  • Ambiguous
  •  
  • Request 1: When a user selects “Non-consumer” in the Consumer__c picklist field in the Loan__c object , in the Opportunity object, the Consumer_Loan__c checkbox  = TRUE and the Consumer_Loan_Hidden__c = FALSE. If “Non-consumer” in the Consumer__c picklist field in the Loan__c object is not selected and save , then Consumer_Loan__c checkbox  = FALSE
 
Request 2: When a user selects “Ambiguous”, “'Insufficient information”, or picklist is blank with no value,  in the Consumer__c picklist field in Loan__c object , in the Opportunity object, the Consumer_Loan_Hidden__c checkbox  = TRUE.  If “Ambiguous”, “'Insufficient information”, or picklist is blank with no value,  in the Consumer__c picklist field in Loan__c object is not selected or save, then Consumer_Loan_Hidden__c checkbox  = FALSE


trigger updateDealWithLoanInfo2 on Loan__c (after insert, after update) {
    if(Trigger.isAfter && ( Trigger.isInsert || Trigger.isUpdate )) {
        map<id,id> dealMap = new map<id,id>();
        set<id> consumerDeals = new set<id>();
        set<id> consumerDeals2 = new set<id>();
        set<id> participatedDeals = new set<id>();
        list<Loan__c> loans = new List<Loan__c>(); 
        list<Opportunity> deals = new List<Opportunity>(); 
        list<Opportunity> updatedDeals = new List<Opportunity>(); 
 
        for(Loan__c l : Trigger.new) {
            if(l.Deal__c <> NULL) {
                dealMap.put(l.Deal__c, l.Id);
            }
        }
        if(dealMap.size()>0){
            loans = [Select Deal__c, Consumer__c, Participated__c from Loan__c where Deal__c in :dealMap.keySet()];
            for (Loan__c l2 : loans){
                if(l2.Consumer__c != 'Non-consumer'){
                    consumerDeals.add(l2.Deal__c);
                }
                if(l2.Consumer__c != 'Ambiguous' && l2.Consumer__c != 'Insufficient information' && l2.Consumer__c != null ){
                    consumerDeals2.add(l2.Deal__c);
                }
                if(l2.Participated__c == TRUE){
                    participatedDeals.add(l2.Deal__c);
                }
            }
            deals = [Select Id, Consumer_Loan__c, Consumer_Loan_Hidden__c,  Participated_Loan__c from Opportunity where Id in :dealMap.keySet()];
            for (Opportunity o : deals){
                if(consumerDeals.contains(o.Id)){
                    o.Consumer_Loan__c = TRUE;
                }else{
                    o.Consumer_Loan__c = FALSE;
                }
                 if(consumerDeals2.contains(o.Id)){
                    o.Consumer_Loan_Hidden__c = TRUE;
                }else{
                    o.Consumer_Loan_Hidden__c = FALSE;
                }
                if(participatedDeals.contains(o.Id)){
                    o.Participated_Loan__c = TRUE;
                }else{
                    o.Participated_Loan__c = FALSE;
                }
                updatedDeals.add(o);
            }
            if(updatedDeals.size()>0){
                update updatedDeals;
            }
        }
    }
}


I appreciate your help on this
ManojjenaManojjena
HI Forrest Muldune,
I have a question here ,I saw opportunity is the parent of Loan .Suppose opportunity one has already 5 Loan which status are ambiguous .

Now you creating one more loan with cosumer then what will be the opportunity status ?

Do you think that one opportunity can have only one loan ?

Please clarify .
Forrest MulduneForrest Muldune
Manoj,

An Opportunity can have one or more loans. 

Example: 1 .  if One Opportunity has 5 Loans where the Consumer__c picklist field  in Loans = "Ambigious" or "“Insufficient information” or "Blank" , then the Consumer_Loan_Hidden__c = TRUE and  the Consumer_Loan__c checkbox  = FALSE  in Opportunity .

2.  If a user adds one more Loan record and selects "Non-consumer" in the Consumer__c picklist field in loans, then the Consumer_Loan__c checkbox  = TRUE and the Consumer_Loan_Hidden__c = FALSE , even though the other 5 loans has "Ambigious" or "“Insufficient information” or "Blank" in the Consumer__c picklist field  in Loans. In addition, if a user changes the loan record from "Non-consumer" to "Ambigious" or "“Insufficient information” or "Blank", then the Consumer_Loan_Hidden__c = TRUE and  the Consumer_Loan__c checkbox  = FALSE  in Opportunity .

I hope this helps .


 
Forrest MulduneForrest Muldune
When I wrote the word "Blank'" , this means there is no value in the Consumer__c picklist field in loans