+ Start a Discussion
Kristiana GrangerKristiana Granger 

I have 2 Object, No relationship between obj - one field is common - My requirement is once i change fiedl value in 1 objject it should be refelct in other obj in 1 of field. how to write trigger for that

Trigger -
Best Answer chosen by Kristiana Granger
Ajay K DubediAjay K Dubedi
Hi Priti,

You can use the below code:

<------ Apex class--->>>>>
public class UpdateFields {
    public static void updateCustom1fields(List<Custom2__c> listOfCustom2){
        try{
        if(listOfCustom2 != NULL){
            System.debug('subhasis');
            List<Custom1__c> existingList = new List<Custom1__c>();
            existingList = [SELECT Id, Id__c, CTC__c FROM Custom1__c LIMIT 50000];
            Map<Id, List<Custom1__c>> idVsCustom1 = new Map<Id, List<Custom1__c>>();
            if(existingList.size() > 0){
                System.debug('subhasis');
                for(Custom1__c object1 : existingList){
                    for(Custom2__c object2 : listOfCustom2){
                        if(object1.Id__c == object2.Id__c){
                            object1.CTC__c = object2.Bonous__c + object2.Expense__c;
                        }
                    }
                }
                Update existingList;
            }
            
        }
        }catch(Exception exp){
            System.debug('Exception Cause'+exp.getCause()+'Line Number'+exp.getLineNumber());
        }
    }
}

<<<---- Trigger------>>>>
 
trigger UpdateFields on Custom2__c (after Update, after insert) {
    
    if(Trigger.isAfter )
        if(Trigger.isInsert || Trigger.isUpdate){
        UpdateFields.updateCustom1fields(Trigger.new);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Priti,

You can use the below code:

<------ Apex class--->>>>>
public class UpdateFields {
    public static void updateCustom1fields(List<Custom2__c> listOfCustom2){
        try{
        if(listOfCustom2 != NULL){
            System.debug('subhasis');
            List<Custom1__c> existingList = new List<Custom1__c>();
            existingList = [SELECT Id, Id__c, CTC__c FROM Custom1__c LIMIT 50000];
            Map<Id, List<Custom1__c>> idVsCustom1 = new Map<Id, List<Custom1__c>>();
            if(existingList.size() > 0){
                System.debug('subhasis');
                for(Custom1__c object1 : existingList){
                    for(Custom2__c object2 : listOfCustom2){
                        if(object1.Id__c == object2.Id__c){
                            object1.CTC__c = object2.Bonous__c + object2.Expense__c;
                        }
                    }
                }
                Update existingList;
            }
            
        }
        }catch(Exception exp){
            System.debug('Exception Cause'+exp.getCause()+'Line Number'+exp.getLineNumber());
        }
    }
}

<<<---- Trigger------>>>>
 
trigger UpdateFields on Custom2__c (after Update, after insert) {
    
    if(Trigger.isAfter )
        if(Trigger.isInsert || Trigger.isUpdate){
        UpdateFields.updateCustom1fields(Trigger.new);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer
Kristiana GrangerKristiana Granger
Thank you Ajay. 
This is working absolutely fine. 

 
Kristiana GrangerKristiana Granger

Can you Help me with one more trigger.

Here I'm tring to count Child record and update total count in parent. [lookup relationship],

This trigger is working fine with insert but throw error while delete .
when I add opty it reflect count , but when i delete opty it wont update total on Account page - below is code snippet.
---------------------------------------------------------------------------------------------------------------------------------------
trigger Opty_ChildCount on Opportunity (after insert, after delete) 
{
    set<id> setid = new set<id>();
    for(Opportunity op : Trigger.new)
    {
        setid.add(op.AccountId);
    }
    List<Account> accList = new List<Account>();
    List<Account> acc = [select id, Opty_ChildCount__c,(select id from opportunities) from Account where id IN : setid];
    if(Trigger.isInsert  && Trigger.isAfter)
    {
        for(Opportunity opty : Trigger.new)
        {
            for(Account a:acc)
            {
                Account a1 = new Account();
                a1.id = a.id
                a1.Opty_ChildCount__c = a.Opportunities.size();
                accList.add(a1);
            }
        }
    }
    if(Trigger.isDelete  && Trigger.isAfter)
    {
        for(Account a:acc)                           
        {
        Account a1 = new Account();
        a1.id = a.id;
        a1.Opty_ChildCount__c = a.Opportunities.size();
        accList.add(a1);
    }
    }
      update accList;
Ajay K DubediAjay K Dubedi
Hi Priti,

You can use the below code:

<------ Apex class--->>>>>
public class UpdateAccountField {
    public static void updateAccount(List<Opportunity> opportunityList){
        if(opportunityList != NULL){
            Set<Id> accIds = new Set<Id>();
            for(Opportunity oppObject : opportunityList){
                accIds.add(oppObject.AccountId);
            }
            List<Account> accList = new List<Account>();
            accList = [SELECT Id, Opty_ChildCount__c,(SELECT Id,AccountId FROM opportunities) FROM Account WHERE Id IN : accIds LIMIT 50000];
            if(accList.size() > 0){
                for(Account accObject : accList){
                    accObject.Opty_ChildCount__c = accObject.opportunities.size();
                }
                Update accList;
            }
        }
    }
    public static void deleteOpportunity(List<Opportunity> opportunityDeleteList){
        if(opportunityDeleteList != NULL){
            Set<Id> accIds = new Set<Id>();
            for(Opportunity oppObject : opportunityDeleteList){
                accIds.add(oppObject.AccountId);
            }
            List<Account> accList = new List<Account>();
            accList = [SELECT Id, Opty_ChildCount__c,(SELECT Id,AccountId FROM opportunities) FROM Account WHERE Id IN : accIds LIMIT 50000];
            if(accList.size() > 0){
                for(Account accObject : accList){
                    accObject.Opty_ChildCount__c = accObject.opportunities.size();
                }
                Update accList;
            }
            System.debug('Subhasis');
        }
    }

}

<<<---- Trigger------>>>>
trigger HandlerTriggerOpportunity on Opportunity (after Insert, after delete) {
    if(trigger.isAfter && trigger.isInsert){
        UpdateAccountField.updateAccount(trigger.new);
    }
    if(trigger.isAfter && trigger.isDelete){
        UpdateAccountField.deleteOpportunity(trigger.old);
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Kristiana GrangerKristiana Granger
Yes, its working fine .
and way more optimized code.
Thank you.
Kristiana GrangerKristiana Granger
@Ajay K Dubedi
For above trigger can you please help me with update trigger code.
when user edit opportunity and change (related to) account name, at that time count get updated on AC page.