• sriram admin 3
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 1
    Replies
Validation Rule is : when Location field is 'Hyd' then contact fields should not be blank.   The above validation should be bypassed by using the custom set
User-added imageUser-added imageUser-added image
The validation rule should work when we select the check box in the (default checked) custom setting field. It should bypass when the default is unchecked. But it is bypassing for both checked and uncheked checkboxe in custom setting custom field. 

 

TRIGGER ON OPPORTUNITY 

trigger UpdateOnBothAccountANDContactRecords on Opportunity (After update) {

    if(trigger.isAfter && trigger.isUpdate){
        Opp_Handler_Class.AfterUpdate(trigger.new,trigger.oldMap);
    }
}

public class Opp_Handler_Class {

    public static void AfterUpdate(list<Opportunity> listOpp, map<id,Opportunity> IdOfOpportunity){
        set<Id> SetOfAccountIds = new Set<Id>();
        Map<Id,Opportunity> OpportunityWithAccIds = new Map<Id,Opportunity>();
        for(Opportunity opp : listOpp){
            if(opp.TestPhone__c != null && opp.TestPhone__c != IdOfOpportunity.get(opp.id).TestPhone__c){
                SetOfAccountIds.add(opp.AccountId);
                OpportunityWithAccIds.put(opp.AccountId,opp);
                system.debug('Updated Opp Record');
            }
        }
        
        list<Account> listOfExistingAcc = [SELECT Id,Name,TestPhone__c,(select id,TestPhone__c from Contacts) FROM Account
                                           where id IN :SetOfAccountIds];
        list<Account> listOfAccountToUpdate = new list<Account>();
        list<Contact> listOfContacts = new List<Contact>();
        for(Account ac : listOfExistingAcc){
            Account a = new Account();
            a.id = ac.id;
            a.TestPhone__c = OpportunityWithAccIds.get(ac.id).TestPhone__c;
            system.debug('Updated Account');
            for(Contact c: ac.contacts){
                Contact con = new contact();
                con.id = c.id;
                con.TestPhone__c = OpportunityWithAccIds.get(ac.id).TestPhone__c;
                listOfContacts.add(con);
                system.debug('Update Contact ');
            }
            listOfAccountToUpdate.add(a);
        }
        
        if(!listOfAccountToUpdate.isEmpty()){
            update listOfAccountToUpdate;
        }
        if(!listOfContacts.isEmpty()){
            update listOfContacts;
        }
        
    }
}


Can Some one help me. How to write Test class for above trigger?

It is not working. Can someone help me where I did wrong.
trigger>
trigger UpdateOpportunityField on Account (After Update) {

    if(trigger.isAfter && trigger.isUpdate){
    OppHandlerClass.AfterUpdate(trigger.new,trigger.oldMap);
    }
}

Handler Class >>>>

public class OppHandlerClass {

    public static void AfterUpdate(list<Account> acc, map<id,Account> oldmap){
        set<Id> setOfAccountIds = new set<Id>();
        for(Account ac : acc){
            if(ac.is_Out_of_businnes__c == true && ac.is_Out_of_businnes__c != oldmap.get(ac.id).is_Out_of_businnes__c){
                setOfAccountIds.add(ac.id);
                system.debug('Account Ids '+ac.Id);
            }
        }
         list<Opportunity> listOfOpp = [SELECT Id,StageName,AccountId from Opportunity where AccountId IN :setOfAccountIds];
        
        list<opportunity> listOfOppToUpdate = new list<Opportunity>();
        
        for(Opportunity op : listOfOpp){
            opportunity opp = new Opportunity();
            opp.id = op.id;
            opp.StageName = 'Closed Lost';
            listOfOppToUpdate.add(op);
            system.debug('Update Opp Records' + opp.StageName);
            system.debug('Update Opp Records' + opp.Id);
            system.debug('Update Opp Records' + opp.CloseDate);
        }
        
        if(!listOfOppToUpdate.isEmpty()){
            update listOfOppToUpdate;
        }
    }
}