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
azar khasimazar khasim 

Need Help in Writing a trigger 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 field 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"". 
Best Answer chosen by azar khasim
Rohit KumawatRohit Kumawat
Hi Azar,

if we want to update account name and owner on Billing Link (Custom Object)
 
trigger AccountNameUpdate on Booking_Link__c (before insert, before update) {
 
    
    List<Account> accountList = new List<Account>();
    Decimal CustomNumber;
    for(Booking_Link__c bl : Trigger.new)
    {
        CustomNumber = bl.Customer_Number__c;
    } 
    
    accountList = [Select Id, OwnerId, Customer_Number__c from Account where Customer_Number__c =:CustomNumber ];
    
    for(Booking_Link__c bl : Trigger.new)
    {
        for(Account a : accountList)
        {
            bl.Account__c = a.Id;
            bl.OwnerId = a.OwnerId;
        }
    }   
}



I hope above code will be helpful.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Azar,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger AccountUpdate on Booking_Link__c (after insert) {
    
    Map<ID, Account> parent = new Map<ID, Account>();
    List<Id> listIds = new List<Id>();
    
    for (Booking_Link__c childObj : Trigger.new) {
        listIds.add(childObj.Account__c);  // API name of parent field in child object
    }
    
    parent = new Map<Id, Account>([SELECT Id, Name, Customer_Number__c, (SELECT Id, Customer_Number__c FROM Booking_Links__r) 
                                   FROM Account 
                                   WHERE ID IN :listIds]); // see Child Relationship Name
    
    for (Booking_Link__c bk: Trigger.new){
        if(bk.Customer_Number__c!=null){
                Account acc = parent.get(bk.Account__c);
                acc.Name = bk.Name;
                acc.OwnerId = bk.OwnerId;
        }
    }
    
    UPDATE parent.values();
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Rohit KumawatRohit Kumawat
Hi Azar,

if we want to update account name and owner on Billing Link (Custom Object)
 
trigger AccountNameUpdate on Booking_Link__c (before insert, before update) {
 
    
    List<Account> accountList = new List<Account>();
    Decimal CustomNumber;
    for(Booking_Link__c bl : Trigger.new)
    {
        CustomNumber = bl.Customer_Number__c;
    } 
    
    accountList = [Select Id, OwnerId, Customer_Number__c from Account where Customer_Number__c =:CustomNumber ];
    
    for(Booking_Link__c bl : Trigger.new)
    {
        for(Account a : accountList)
        {
            bl.Account__c = a.Id;
            bl.OwnerId = a.OwnerId;
        }
    }   
}



I hope above code will be helpful.
This was selected as the best answer
azar khasimazar khasim
Yaaa Khan Anas,

But i am saving my record from Sites.
This trigger has stopped the record in order to get saved in BookingLink Custom Object

Please have a look on my Trigger code which i have done some changes.

Trigger Account Update on Booking_Link__c Object.



trigger AccountUpdate on Booking_Link__c (after insert) {
   
    Map<ID, Account> parent = new Map<ID, Account>();
    List<Id> listIds = new List<Id>();
    
    for (Booking_Link__c blink : Trigger.new) {
        listIds.add(blink.Account__c);  // API name of parent field in child object
    }
    //Parent= new Map<.......
    parent = new Map<Id, Account>([SELECT Id, Name, Customer_Number__pc, (SELECT Id, Customer_Number__c FROM Booking_Link__r) 
                                   FROM Account 
                                   WHERE ID IN :listIds]); // see Child Relationship Name
    
    for (Booking_Link__c bk: Trigger.new){
        if(bk.Customer_Number__c!=null){
                Account acc = parent.get(bk.Account__c);
                acc.Name = bk.Name;
                acc.OwnerId = bk.OwnerId;
        }
    }
    
    UPDATE parent.values();


Please help me out 
This trigger is not allowing the record to get saved into Booking Link Custom Object through Sites i am saving this record.

Thanks and Regards,
Azar Khasim.
azar khasimazar khasim
Hello Khan Anas,

I hope u got my issue,

Can u please help me from this..,


We have ''Person Contact'' Object means( Account+Contact )Object, Where this 
Customer Number has been there like
API Name : Customer_Number__pc.

We Have BookingLink API as                                                                     : Booking_Link__c
In Booking Link we have the same Customer Number field as              : Customer_Number__c

Now,
""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"". 
 
I am Using Sites to create the record in BookingLink.
Already we have a record in PersonAccount( Account+Contact )Object  now 
the new record in Booking Link need to check Customer Number in Person Account and Update the Account and Owner  Names in Booking Link Record which we created.

Below is my Trigger:

Trigger Account Update on Booking Link Custom Object.


************************************************************************************************
trigger AccountUpdate on Booking_Link__c (after insert) {
   
    Map<ID, Account> parent = new Map<ID, Account>();
    List<Id> listIds = new List<Id>();
    
    for (Booking_Link__c blink : Trigger.new) {
        listIds.add(blink.Account__c);  // API name of parent field in child object
    }
   
    parent = new Map<Id, Account>([SELECT Id, Name, Customer_Number__pc, (SELECT Id, Customer_Number__c FROM Booking_Link__r) 
                                   FROM Account 
                                   WHERE ID IN :listIds]); // see Child Relationship Name
    
    for (Booking_Link__c bk: Trigger.new){
        if(bk.Customer_Number__c!=null){
                Account acc = parent.get(bk.Account__c);
                acc.Name = bk.Name;
                acc.OwnerId = bk.OwnerId;
        }
    }
    
    UPDATE parent.values();

******************************************************************************************************
These need to get fixed for me 
Please help me out 
This trigger is not allowing the record to get saved into Booking Link Custom Object through Sites which i am using it.
The trigger is stopping to get saved in object.


Please help me out from this issue
.

Thanks and Regards,
Azar Khasim.
azar khasimazar khasim
Hello Guys,

I have a error in below trigger code.
*********************************************************
trigger AccountNameUpdate on Booking_Link__c (before insert, before update) {
    List<Account> accountList = new List<Account>();
    Decimal CustomNumber;
    for(Booking_Link__c bl : Trigger.new)
    {       
6th       CustomNumber = bl.Customer_Number__c;
    }
8th    accountList = [Select Id, OwnerId, Customer_Number__pc from Account where Customer_Number__pc =:CustomNumber ];
    for(Booking_Link__c bl : Trigger.new)
    {
        for(Account a : accountList)
        {
           if(bl.Customer_Number__c == a.Customer_Number__pc)
            {
                bl.Account__c = a.Id;
                bl.OwnerId = a.OwnerId;
            }
        }
    }  
}
*****************************************************************************
Line 6:  CustomNumber = bl.Customer_Number__c;
Line 8: accountList = [Select Id, OwnerId, Customer_Number__pc from Account where Customer_Number__c =:CustomNumber ];

In Line 8 i have changed the bold one from Customer_Number__c to Customer_Number__pc.
why because we have the Customer Number field API as above because we are using PersonAccount.

Please help me from these errors.

Errors:

Line 6: Illegal assignment from String to Decimal
Line 8: 
Customer_Number__pc from Account where Customer_Number__c =:CustomNumber
                                       ^
ERROR at Row:1:Column:60
No such column 'Customer_Number__c' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.


Guys, Please help me out from this.

Thanks and Regards,
Azar Khasim.