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
MOHD SAYEED 10MOHD SAYEED 10 

Please help me on opportunity

I want to check that on opportunity if amount is greater than 10% of expected revenue then checked the coupon 1 and if 20% then checked coupon 2 and if 30% then checked coupon 3.

Coupon 1 and 2 and 3 these fields are discount coupon and this object are child object of opportunity 

How to write code for this scenario
PriyaPriya (Salesforce Developers) 
Hey Mohammed,

No need to write code for this scenario. You can do this with using Workflow/Process builder or using flow. 

Flow builder is the recommended one. 

Check the below example to update the field based on another field value :- 

https://www.gradient.works/blog/salesforce-flow-update-records

Kindly mark it as the best answer if it helps.

Thank you
Shubham Jain 338Shubham Jain 338
Hi Sayeed,

Please find the working code

trigger OpportunityTrigger on Opportunity (after update) {
    
    Map<Id,String> oppIdToCheckBoxMap = new Map<Id,String>();
    for (Opportunity opp : Trigger.new) {
        if (opp.Amount != Trigger.oldMap.get(opp.Id).Amount || opp.ExpectedRevenue != Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
            if (opp.Amount > 0.3 * opp.ExpectedRevenue && Trigger.oldMap <= 0.3 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '3');
            } else if (opp.Amount > 0.2 * opp.ExpectedRevenue && Trigger.oldMap <= 0.2 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '2');
            } else if (opp.Amount > 0.1 * opp.ExpectedRevenue && Trigger.oldMap <= 0.1 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '1');
            }
        }
    }
    
    if (oppIdToCheckBoxMap.size() > 0) {
        List<Discount_Coupon__c> discountCouponList = [SELECT Id, Coupon_Code1__c, Coupon_Code2__c, Coupon_Code3__c FROM Discount_Coupon__c WHERE Opportunity__c IN :oppIdToCheckBoxMap.keySet()];
        if (discountCouponList.size() > 0) {
            for (Discount_Coupon__c disCoupon : discountCouponList) {
                if (oppIdToCheckBoxMap.containsKey(disCoupon.Opportunity__c)) {
                    if (oppIdToCheckBoxMap.get(disCoupon.Opportunity__c) == '3') {
                        disCoupon.Coupon_Code3__c = true;
                        disCoupon.Coupon_Code2__c = false;
                        disCoupon.Coupon_Code1__c = false;
                    } else if (oppIdToCheckBoxMap.get(disCoupon.Opportunity__c) == '2') {
                        disCoupon.Coupon_Code2__c = true;
                        disCoupon.Coupon_Code3__c = false;
                        disCoupon.Coupon_Code1__c = false;
                    } else {
                        disCoupon.Coupon_Code1__c = true;
                        disCoupon.Coupon_Code3__c = false;
                        disCoupon.Coupon_Code2__c = false;
                    }
                }
            }
            update discountCouponList;
        }
    }
}

Please mark this as the best answer if it helps.

Thanks
Shubham Jain
MOHD SAYEED 10MOHD SAYEED 10
Showing error
comparison arguments must be compatible types:Map, Decimal
Shubham Jain 338Shubham Jain 338
Hi Sayeed,

Apologies as during copy-paste some code got removed somehow because of that it is giving compile-time error.

Please use this code and let me know if any issues

trigger OpportunityTrigger on Opportunity (after update) {
    
    Map<Id,String> oppIdToCheckBoxMap = new Map<Id,String>();
    for (Opportunity opp : Trigger.new) {
        if (opp.Amount != Trigger.oldMap.get(opp.Id).Amount || opp.ExpectedRevenue != Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
            if (opp.Amount > 0.3 * opp.ExpectedRevenue && Trigger.oldMap.get(opp.Id).Amount <= 0.3 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '3');
            } else if (opp.Amount > 0.2 * opp.ExpectedRevenue && Trigger.oldMap.get(opp.Id).Amount <= 0.2 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '2');
            } else if (opp.Amount > 0.1 * opp.ExpectedRevenue && Trigger.oldMap.get(opp.Id).Amount <= 0.1 * Trigger.oldMap.get(opp.Id).ExpectedRevenue) {
                oppIdToCheckBoxMap.put(opp.Id, '1');
            }
        }
    }
    
    if (oppIdToCheckBoxMap.size() > 0) {
        List<Discount_Coupon__c> discountCouponList = [SELECT Id, Coupon_Code1__c, Coupon_Code2__c, Coupon_Code3__c FROM Discount_Coupon__c WHERE Opportunity__c IN :oppIdToCheckBoxMap.keySet()];
        if (discountCouponList.size() > 0) {
            for (Discount_Coupon__c disCoupon : discountCouponList) {
                if (oppIdToCheckBoxMap.containsKey(disCoupon.Opportunity__c)) {
                    if (oppIdToCheckBoxMap.get(disCoupon.Opportunity__c) == '3') {
                        disCoupon.Coupon_Code3__c = true;
                        disCoupon.Coupon_Code2__c = false;
                        disCoupon.Coupon_Code1__c = false;
                    } else if (oppIdToCheckBoxMap.get(disCoupon.Opportunity__c) == '2') {
                        disCoupon.Coupon_Code2__c = true;
                        disCoupon.Coupon_Code3__c = false;
                        disCoupon.Coupon_Code1__c = false;
                    } else {
                        disCoupon.Coupon_Code1__c = true;
                        disCoupon.Coupon_Code3__c = false;
                        disCoupon.Coupon_Code2__c = false;
                    }
                }
            }
            update discountCouponList;
        }
    }
}


Please mark this as the best answer if it helps.

Thanks
Shubham Jain