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
HTANIRSHTANIRS 

Trigger to update after inserting in the same object

Hi,
I have a the requirement: I need to create a Custom Object with a relationship to Contact. I need to create 5 checkbox in Contacts and same in custom object. Now when I enter the values in the checkboxes in contact I need to insert a new record in custom object with the checkbox values. After inserting the record in cusgom object I need to make the contact checkbox to unselsect if 2 checkbox is selected. How can I do this. I am done with insert and update trigger. Below is my Trigger for reference. Please give me your suggestions.

trigger projInsert on Contact (after insert, after update) {
    System.debug('---- Inside Contact :----');
    
    if(trigger.isInsert) {
        List<Project__c> projList = new List<Project__c>();
        for(Contact con : Trigger.new) {
            System.debug('---- Inside For Loop : ----');    
            if (con.LastName != '' && con.LastName != null) {
                System.debug('---- Inside IF Loop : ----');    
                Project__c proj = new Project__c();
                proj.Contact__c = con.id;
                proj.Feature_1__c = con.Feature_1__c;
                proj.Feature_2__c = con.Feature_2__c;
                if(con.Product_A__c == true) {
                    proj.Products__c = 'Product A';
                }
                else if (con.Product_B__c == true) {
                    proj.Products__c = 'Product B';
                }
                else {
                    proj.Products__c = '';
                }
                projList.add(proj);
            }
            System.debug('---- Proj List ----');
            System.debug('---- Proj List After If : ----'+projList);   
        }
        
        if(projList.size() > 0) {
            insert projList;
        }
        System.debug('---- Proj List : ----');
        System.debug('---- Proj List Size : ----' +projList.size());
    }
       
    if(trigger.isUpdate) {
        Map <Id, Contact> mapContact = new Map <Id, Contact>();
        List<Project__c> listProject = new List<Project__c>();
        
        for(Contact cont : trigger.new)
            mapContact.put(cont.Id, cont);
        
        listProject = [SELECT id, Contact__c, Feature_1__c, Feature_2__c, Products__c
                         FROM Project__c 
                         WHERE Contact__c IN : mapContact.keySet()];
        
        if (listProject.size() > 0) {
            for (Project__c pro : listProject) {
                pro.Feature_1__c = mapContact.get(pro.Contact__c).Feature_1__c;
                pro.Feature_2__c = mapContact.get(pro.Contact__c).Feature_2__c;
            }
            update listProject;
        }
    }        
}

Thanks
Best Answer chosen by HTANIRS
Akshay_DhimanAkshay_Dhiman
Hi HTANIRS,

Try this Below Trigger, For your update functionality -- 
 
trigger CustomObjectCheckBoxTri on Contact (after insert , before insert , before update) {
    List<Custom_Object__c> customObj = new List<Custom_Object__c>();
    List<Contact> conList = new List<Contact>();
    
    
    if(trigger.isInsert && trigger.isAfter)
    {
        for(Contact con : trigger.New)
        {
            System.debug('Inside Loop');
            Integer count = 0;
            if(CheckboxHoldingValue.FirstCheckbox == true || CheckboxHoldingValue.SecondCheckbox == true || CheckboxHoldingValue.ThirdCheckbox == true 
               || CheckboxHoldingValue.FourthCheckbox == true || CheckboxHoldingValue.FifthCheckbox == true)
            {
                Custom_Object__c cob = new Custom_Object__c();
                cob.Contact__c = con.Id;
                cob.First_Checkbox__c = CheckboxHoldingValue.FirstCheckbox;
                cob.Second_Checkox__c = CheckboxHoldingValue.SecondCheckbox;
                cob.Third_Checkbox__c = CheckboxHoldingValue.ThirdCheckbox;
                cob.Fourth_Checkbox__c = CheckboxHoldingValue.FourthCheckbox;
                cob.Fifth_checkbox__c = CheckboxHoldingValue.FifthCheckbox;
                customObj.add(cob);
            }
        }
        if(customObj.size()>0)
        {
            System.debug('Inside customObj');
            insert customObj;
        }
    }
    if(trigger.isInsert && trigger.isBefore)
    {
        for(Contact con : trigger.New)
        {
            Integer count = 0;
            System.debug('Inside 2nd Loop');
            if(con.First_Checkbox__c == true)
            {
                CheckboxHoldingValue.FirstCheckbox = con.First_Checkbox__c;
                count++;
            }
            if(con.Second_Checkox__c == true)
            {
                CheckboxHoldingValue.SecondCheckbox = con.Second_Checkox__c;
                count++;
            }
            if(con.Third_Checkbox__c == true)
            {
                CheckboxHoldingValue.ThirdCheckbox = con.Third_Checkbox__c;
                count++;
            }
            if(con.Fourth_Checkbox__c == true)
            {
                CheckboxHoldingValue.FourthCheckbox = con.Fourth_Checkbox__c;
                count++;
            }
            if(con.Fifth_checkbox__c == true)
            {
                CheckboxHoldingValue.FifthCheckbox = con.Fifth_checkbox__c;
                count++;
            }
            if(count == 2)
            {
                con.First_Checkbox__c = false;
                con.Second_Checkox__c = false;
                con.Third_Checkbox__c = false;
                con.Fourth_Checkbox__c = false;
                con.Fifth_checkbox__c = false;
            }
        }
    }
    if(trigger.isUpdate && trigger.isBefore)
    {
        System.debug('Inside Update');
        List<Custom_Object__c> customObjList = new List<Custom_Object__c>();
        Set<Id> customObjIdSet = new Set<Id>();
        Map<Id,List<Custom_Object__c>> customObjVsContactMap = new  Map<Id,List<Custom_Object__c>>();
        for(Contact con : trigger.New)
        {
            customObjIdSet.add(con.Id);
        }
        customObjList = [Select Id,Name , First_Checkbox__c , Second_Checkox__c , Third_Checkbox__c ,Fourth_Checkbox__c , Fifth_checkbox__c , Contact__c
                         from Custom_Object__c where Contact__c IN : customObjIdSet];
        System.debug('customObjList--->>>' + customObjList);
        if(customObjList.size()>0)
        {
            for(Custom_Object__c custObjLoop : customObjList)
            {
                if(!customObjVsContactMap.containsKey(custObjLoop.Contact__c))
                {
                    customObjVsContactMap.put(custObjLoop.Contact__c ,new List<Custom_Object__c>());
                }
                customObjVsContactMap.get(custObjLoop.Contact__c).add(custObjLoop);
            }
            System.debug('customObjVsContactMap--->>>' + customObjVsContactMap);
            if(!customObjVsContactMap.isEmpty())
            {
                for(Contact con : trigger.New)
                {
                    for(Custom_Object__c custObjLoop : customObjVsContactMap.get(con.Id))
                    {
                        if(con.First_Checkbox__c == true)
                        {
                            custObjLoop.First_Checkbox__c = con.First_Checkbox__c;
                            con.First_Checkbox__c = false;
                            customObj.add(custObjLoop);
                        }
                    }  
                } 
            }
            if(customObj.size()>0)
            {
                update customObj;
            }
        }
    }
}



Hope it will help you 
Thanks 
Akshay

All Answers

Akshay_DhimanAkshay_Dhiman
Hi HTANIRS,

Try this below Trigger -- 
Just make modification as per your need, if you are feeling any difficulties -- 
Object and Fields Names are not same as per your given condition, so make changes According to your Object and Field Names -- 
 
--------------------Class For Holding Values Of CheckBoxes --------------------------------------
public class CheckboxHoldingValue {
    public static Boolean  FirstCheckbox = false;
    public static Boolean  SecondCheckbox = false;
    public static Boolean  ThirdCheckbox = false;
    public static Boolean  FourthCheckbox = false;
    public static Boolean  FifthCheckbox = false;

}

------------------------Trigger For Doing Task According To your need ---------------------------

trigger CustomObjectCheckBoxTri on Contact (after insert , before insert) {
    List<Custom_Object__c> customObj = new List<Custom_Object__c>();
    List<Contact> conList = new List<Contact>();
    
    
    if(trigger.isInsert && trigger.isAfter)
    {
        for(Contact con : trigger.New)
        {
            System.debug('Inside Loop');
            Integer count = 0;
            if(CheckboxHoldingValue.FirstCheckbox == true || CheckboxHoldingValue.SecondCheckbox == true || CheckboxHoldingValue.ThirdCheckbox == true 
               || CheckboxHoldingValue.FourthCheckbox == true || CheckboxHoldingValue.FifthCheckbox == true)
            {
                Custom_Object__c cob = new Custom_Object__c();
                cob.Contact__c = con.Id;
                cob.First_Checkbox__c = CheckboxHoldingValue.FirstCheckbox;
                cob.Second_Checkox__c = CheckboxHoldingValue.SecondCheckbox;
                cob.Third_Checkbox__c = CheckboxHoldingValue.ThirdCheckbox;
                cob.Fourth_Checkbox__c = CheckboxHoldingValue.FourthCheckbox;
                cob.Fifth_checkbox__c = CheckboxHoldingValue.FifthCheckbox;
                customObj.add(cob);
            }
        }
        if(customObj.size()>0)
        {
            System.debug('Inside customObj');
            insert customObj;
        }
    }
    if(trigger.isInsert && trigger.isBefore)
    {
        for(Contact con : trigger.New)
        {
            Integer count = 0;
            System.debug('Inside 2nd Loop');
            if(con.First_Checkbox__c == true)
            {
                CheckboxHoldingValue.FirstCheckbox = con.First_Checkbox__c;
                count++;
            }
            if(con.Second_Checkox__c == true)
            {
                CheckboxHoldingValue.SecondCheckbox = con.Second_Checkox__c;
                count++;
            }
            if(con.Third_Checkbox__c == true)
            {
                CheckboxHoldingValue.ThirdCheckbox = con.Third_Checkbox__c;
                count++;
            }
            if(con.Fourth_Checkbox__c == true)
            {
                CheckboxHoldingValue.FourthCheckbox = con.Fourth_Checkbox__c;
                count++;
            }
            if(con.Fifth_checkbox__c == true)
            {
                CheckboxHoldingValue.FifthCheckbox = con.Fifth_checkbox__c;
                count++;
            }
            if(count == 2)
            {
                con.First_Checkbox__c = false;
                con.Second_Checkox__c = false;
                con.Third_Checkbox__c = false;
                con.Fourth_Checkbox__c = false;
                con.Fifth_checkbox__c = false;
            }
        }
    }
}


If found this answer helpful , Do mark as best answer so that it helps other also
Thanks 
Akshay
HTANIRSHTANIRS
Hi Akshay,

Thanks for you reply. This is working and I need small change. How can I use the same logic in update. My requirement is when click on checkbox 1 in edit. Need to uncheck it after it updated in custom object.

Below Code I modified for Update but it is not working. Can you please suggest how can it done.

if(trigger.isUpdate) {
        Map <Id, Contact> mapContact = new Map <Id, Contact>();
        List<Project__c> listProject = new List<Project__c>();
        Integer count = 0;
        
        for(Contact con : trigger.new) {
            mapContact.put(con.Id, con);
            if(con.Product_A__c == true) {
                CheckboxHoldingValue.FirstCheckbox = con.Product_A__c;
                count++;
            }            
            if(con.Feature_1__c == true) {
                CheckboxHoldingValue.SecondCheckbox = con.Feature_1__c;
                count++;
            }
            if(con.Feature_2__c == true) {
                CheckboxHoldingValue.ThirdCheckbox = con.Feature_2__c;
                count++;
            }            
            if(count >= 1 ) {
                con.Product_A__c = false;
                con.Feature_1__c = false;
                con.Feature_2__c = false;
            }
        }
        listProject = [SELECT id, Contact__c, Feature_1__c, Feature_2__c, Products__c
                         FROM Project__c 
                         WHERE Contact__c IN : mapContact.keySet()];
                
        if (listProject.size() > 0) {
            for (Project__c pro : listProject) {
                pro.Feature_1__c = mapContact.get(pro.Contact__c).Feature_1__c;
                pro.Feature_2__c = mapContact.get(pro.Contact__c).Feature_2__c;
            }
            update listProject;
        }
    }

Thanks
Akshay_DhimanAkshay_Dhiman
Hi HTANIRS,

Try this Below Trigger, For your update functionality -- 
 
trigger CustomObjectCheckBoxTri on Contact (after insert , before insert , before update) {
    List<Custom_Object__c> customObj = new List<Custom_Object__c>();
    List<Contact> conList = new List<Contact>();
    
    
    if(trigger.isInsert && trigger.isAfter)
    {
        for(Contact con : trigger.New)
        {
            System.debug('Inside Loop');
            Integer count = 0;
            if(CheckboxHoldingValue.FirstCheckbox == true || CheckboxHoldingValue.SecondCheckbox == true || CheckboxHoldingValue.ThirdCheckbox == true 
               || CheckboxHoldingValue.FourthCheckbox == true || CheckboxHoldingValue.FifthCheckbox == true)
            {
                Custom_Object__c cob = new Custom_Object__c();
                cob.Contact__c = con.Id;
                cob.First_Checkbox__c = CheckboxHoldingValue.FirstCheckbox;
                cob.Second_Checkox__c = CheckboxHoldingValue.SecondCheckbox;
                cob.Third_Checkbox__c = CheckboxHoldingValue.ThirdCheckbox;
                cob.Fourth_Checkbox__c = CheckboxHoldingValue.FourthCheckbox;
                cob.Fifth_checkbox__c = CheckboxHoldingValue.FifthCheckbox;
                customObj.add(cob);
            }
        }
        if(customObj.size()>0)
        {
            System.debug('Inside customObj');
            insert customObj;
        }
    }
    if(trigger.isInsert && trigger.isBefore)
    {
        for(Contact con : trigger.New)
        {
            Integer count = 0;
            System.debug('Inside 2nd Loop');
            if(con.First_Checkbox__c == true)
            {
                CheckboxHoldingValue.FirstCheckbox = con.First_Checkbox__c;
                count++;
            }
            if(con.Second_Checkox__c == true)
            {
                CheckboxHoldingValue.SecondCheckbox = con.Second_Checkox__c;
                count++;
            }
            if(con.Third_Checkbox__c == true)
            {
                CheckboxHoldingValue.ThirdCheckbox = con.Third_Checkbox__c;
                count++;
            }
            if(con.Fourth_Checkbox__c == true)
            {
                CheckboxHoldingValue.FourthCheckbox = con.Fourth_Checkbox__c;
                count++;
            }
            if(con.Fifth_checkbox__c == true)
            {
                CheckboxHoldingValue.FifthCheckbox = con.Fifth_checkbox__c;
                count++;
            }
            if(count == 2)
            {
                con.First_Checkbox__c = false;
                con.Second_Checkox__c = false;
                con.Third_Checkbox__c = false;
                con.Fourth_Checkbox__c = false;
                con.Fifth_checkbox__c = false;
            }
        }
    }
    if(trigger.isUpdate && trigger.isBefore)
    {
        System.debug('Inside Update');
        List<Custom_Object__c> customObjList = new List<Custom_Object__c>();
        Set<Id> customObjIdSet = new Set<Id>();
        Map<Id,List<Custom_Object__c>> customObjVsContactMap = new  Map<Id,List<Custom_Object__c>>();
        for(Contact con : trigger.New)
        {
            customObjIdSet.add(con.Id);
        }
        customObjList = [Select Id,Name , First_Checkbox__c , Second_Checkox__c , Third_Checkbox__c ,Fourth_Checkbox__c , Fifth_checkbox__c , Contact__c
                         from Custom_Object__c where Contact__c IN : customObjIdSet];
        System.debug('customObjList--->>>' + customObjList);
        if(customObjList.size()>0)
        {
            for(Custom_Object__c custObjLoop : customObjList)
            {
                if(!customObjVsContactMap.containsKey(custObjLoop.Contact__c))
                {
                    customObjVsContactMap.put(custObjLoop.Contact__c ,new List<Custom_Object__c>());
                }
                customObjVsContactMap.get(custObjLoop.Contact__c).add(custObjLoop);
            }
            System.debug('customObjVsContactMap--->>>' + customObjVsContactMap);
            if(!customObjVsContactMap.isEmpty())
            {
                for(Contact con : trigger.New)
                {
                    for(Custom_Object__c custObjLoop : customObjVsContactMap.get(con.Id))
                    {
                        if(con.First_Checkbox__c == true)
                        {
                            custObjLoop.First_Checkbox__c = con.First_Checkbox__c;
                            con.First_Checkbox__c = false;
                            customObj.add(custObjLoop);
                        }
                    }  
                } 
            }
            if(customObj.size()>0)
            {
                update customObj;
            }
        }
    }
}



Hope it will help you 
Thanks 
Akshay
This was selected as the best answer
HTANIRSHTANIRS
Hi Akshay,

Thanks for your Help. My issue got solved and it is working.

Thanks.
HTANIRSHTANIRS
Hi Akshay,

How can I write Test Class for this Trigger. Please help in assisting.

Thanks.