function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Forrest MulduneForrest Muldune 

Apex If and Then

In the code below, I am trying to figure out a way If Consumer_Loan__c = TRUE then it will require Consumer_Loan_Hidden__c = FALSE. 

I appreciate your help



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(consumerDealsYellow.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);
ManojjenaManojjena
Hi Forrest Muldune,

It is not clear to give you proper solution.
Still trywith below code it may help !
 
if(consumerDeals.contains(o.Id)){
                    o.Consumer_Loan__c = TRUE;
					o.Consumer_Loan_Hidden__c = FALSE
                }else{
                    o.Consumer_Loan__c = FALSE;
					o.Consumer_Loan_Hidden__c = True;
                }

Let m eknow if it help !
Else elabote bit more .
Forrest MulduneForrest Muldune
Manoj,

I appreciate the effort but it is still not coming out correctly.

For a more detailed explanation of my request, please view website 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BM7H 

Thank you very much.

 
ManojjenaManojjena
Hi Forrest Muldune,

Try with below code it will help !
 
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> consumerDealsYellow = 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 == 'Consumer'){
                    consumerDeals.add(l2.Deal__c);
                }
                if(l2.Consumer__c == 'Ambiguous' || l2.Consumer__c == NULL || l2.Consumer__c == 'Insufficient information'|| l2.Consumer__c == 'Not reviewed'){
                    consumerDealsYellow.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;
                    o.Consumer_Loan_Hidden__c = FALSE;
                }else{
                    o.Consumer_Loan__c = FALSE;
					o.Consumer_Loan_Hidden__c = TRUE;
                }
                if(consumerDealsYellow.contains(o.Id)){
				     o.Consumer_Loan__c = FALSE;
                     o.Consumer_Loan_Hidden__c = TRUE;
                }else{
				    o.Consumer_Loan__c = TRUE;
                    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;
            }
        }
    }
}

Let me know if it helps !
 
Forrest MulduneForrest Muldune
I appreciate the help, but it did not work out.

In the Consumer__c picklist field in Loan__c custom object I have the following values below

Not reviewed
Consumer
Non-consumer
Insufficient information
Ambiguous

Example: If one Opportunity (Deal) is connect to 2 loans (loan A and Loan B) . If the the Consumer__c picklist = "Consumer"  in Loan A and the Consumer__c picklist = ​"Ambiguous" or "Insufficient information" or "Not reviewed" or "Null" in Loan B, then the Consumer_Loan__c = TRUE and the Consumer_Loan_Hidden__c = FALSE. the "Consumer" value in the Consumer__c picklist is always superior to other values. However if a user changes the Consumer__c picklist = "Ambiguous" or "Insufficient information" or "Not reviewed" or "Null" in Loan A, then the  Consumer_Loan__c = FALSE and Consumer_Loan_Hidden__c = TRUE. 

I hope my explanation helps. thank you for your time again.


 
Forrest MulduneForrest Muldune
Manoj, 

I found a way to make this trigger work with using additional formula fields in Opportunities, thank you. The next question I have is when I delete a Loan Record, the trigger is not activate. When a loan record is deleted, how can this trigger activate?