0

we have 2 objects Account and Dsignation

parent object:Account

Child object: Designation

Relationship :Lookup(Field name "Account__C" on Designation object)

If an Account has Designation with Title CEO(Field name="Title__c") then the Account field "Area__c" cannot be blank

//this is my  trigger code

trigger demo on Account (after update) {

    if(trigger.isAfter && trigger.isUpdate ){
        system.debug('hello buddy');
        demohandler.validationdemo(trigger.old);
    }
}

this is my handler class

public class demohandler {

    public static void validationdemo(List<Account> acc){
        try{
            Set<Id> idSet = new Set<Id>();
            for(Account acc1: acc){
                idSet.add(acc1.id);
                system.debug('@@@@');
            }
            List<Account> accList = [Select id, Area__c,(select id ,Title__c from Designations__r where Title__c='CEO') from Account where Id IN: idSet];
            for(Account act : accList){
                if(act.Designations__r.size()>0 && (act.Area__c == '' || act.Area__c == null)){
                    act.addError('Area should not be blank');
                    system.debug('$$$$');
                }
            }
        }catch (Exception e){
            system.debug('An exception occurred: ' + e.getStackTraceString());
        } 
        
    }
}

I have to write a trigger on another object also to fulfil my requirement but I am unable to write it.

1
  • i have to write trigger on another object also to full fill my requirement but i am unable to write can anyone help me .plz read the statement above carefully
    – singh_25
    Commented Dec 2, 2021 at 6:57

2 Answers 2

1

To robustly handle this, and assuming it doesn't matter that the Account has an Area without a CEO Designation, you need at least the following:

  1. Account automation on record update:
    1. Area being set blank needs CEO Designation check
  2. Designation automation on record create or undelete:
    1. Title being CEO needs Account Area non-blank check
  3. Designation automation on record update:
    1. Title being changed to CEO needs Account Area non-blank check
    2. Account being changed to a non-null value and where the Title is CEO needs new Account Area non-blank check

You can choose to use Apex triggers for this, or you can use before save auto-launch flows. If you are familiar with Apex then triggers are likely easier to write. In this case you will need triggers on Account (before update) and Designation (before create, undelete and update).

If you do not know how to address the various scenarios outlined above, you should consider doing some Apex trailheads to improve your knowledge and ability. Alternatively, write appropriately bulkified flows though I suspect you would have to deal with some bulkifications for yourself and this could be complex.

Note that there are some potential complications around having multiple Designations for the same Account that share the same Title, but I am ignoring that.

Note that SFSE is not a free coding service so please don't expect anyone to provide the trigger code for you. We are here to help resolve specific, targeted problems with existing code, or to provide guidance on how to achieve requirements where the asker has already outlined what they have tried and where they are stuck. Read more in How to Ask.

0

While you may have tried to abstract your problem, there may be an opportunity to improve the overall database design.

  1. Should the designation required to be on a different object or can it still be on the same object (BTW, I assume that your account is Personal Account, hence the need for designation). Designation is such a small attribute, it need not be on a separate object, unless you are carrying a lot more information, which you find it to be normalized in a separate object. If you keep it in the same object, you may leverage validation rules to keep it simple
  2. You may have chosen to go with lookup Vs Master-Detail, to overcome the limits wrt Master-Details per object (if you have the luxury of using M:D relationship, you may use some technique to bring up this info in the form of rollup summary... but we will save it for another day).
  3. Consider Flows, instead of Trigger- this is certainly do-able using Flows.

Lastly, In this case, you need to check from both objects for sanity (technically, 1) when the AREA value changes to blank 2) when the title is updated to CEO

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .