+ Start a Discussion
azar khasimazar khasim 

Need to write a trigger for the below requirements, Please help me out

I have a Booking Link(Custom Object) and Account Object. There is a relation between both of them using Lookup. and i had a filed called Customer Number in Accounts. 

By using Sites i am storing as a record which contains Customer number as one of the field in that record in BookingLink Object.
Now By using this Customer Number i need to update the Account Name and Owner of that Customer Number(Which already had a record in Account object).


Please help me out from thiss.

""When ever a Record is created in BookingLink Object by using Customer Number it need to update the Account name and Owner of the record""
Deepali KulshresthaDeepali Kulshrestha
Hi azar,

Hope Below Code will help you:
1.Apex trigger:

trigger UpdateParentAccount on Booking_Link__c (after insert)
{
    if(Trigger.isAfter && Trigger.isInsert)
    {
        UpdateAccountByBookinLink.updateIfBookLinkHaveCustomer(Trigger.New);
    }
}

2.Apex class

public class UpdateAccountByBookinLink
{
    public static void updateIfBookLinkHaveCustomer(List<Booking_Link__c> allBookingLink)
    {
        try
        {
            List<Booking_Link__c> BookingLinkHavingCustomer=new List<Booking_Link__c>();
            Set<Id> customerId=new Set<Id>();
            Set<Id> accountId=new Set<Id>();
            for(Booking_Link__c link:allBookingLink)
            {
                if(link.Customer__c!=null)
                {
                    BookingLinkHavingCustomer.add(link);
                    customerId.add(link.Customer__c);
                    accountId.add(link.Account__c);
                }
            }
            
            if(BookingLinkHavingCustomer.size()>0)
            {
                Map<id,Customer__c> customerMap=new Map<id,Customer__c>();
                Map<id,Account> accountMap=new Map<id,Account>();
                List<Customer__c> allcustomer=new List<Customer__c>([select id,Name from Customer__c where Id IN:customerId]);
                for(Customer__c customer:allcustomer)
                {
                   customerMap.put(customer.id,customer);
                }
                
                List<Account> allaccount=new List<Account>([select id,Name from Account where Id IN:accountId]);
                for(Account acc:allaccount)
                {
                   accountMap.put(acc.id,acc);
                }
                
                
                List<Account> updateAccount=new List<Account>();
                
                for(Booking_Link__c link:BookingLinkHavingCustomer)
                {
                    Account getacc=accountMap.get(link.Account__c);
                    getacc.Name='';//put your value whatever you want ON ACCOUNTNAME
                    getacc.OwnerId='';//put your value whatever you want ON OWNERID
                    
                    updateAccount.add(getacc);
                }
                
                if(updateAccount.size()>0)
                {
                    update updateAccount;
                }
            }
        }
        catch(Exception e)
        {
            System.debug('Error in-->'+e.getLineNumber()+'and Error is-->'+e.getMessage());
        }
    }
}

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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com