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 to update the Owner and account name in a record while saving in Custom Object.

Hi All,

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 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"". 

Thanks and Regards,
Azar Khasim.
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

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
Hello Rohit,

Thanks for the code, But its not working from my side.

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();

******************************************************************************************************

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.


 
Rohit KumawatRohit Kumawat
Hi Azar

See you have to set value of account name and onwer while creating of the record in Billing Link object.

So first thing is you have to use before event trigger. 
Secong thing while creating record of billing link , How can u get account id withount hving value of account field in billing object.
please check my code again beacause its working in my org perfectly.

I hope you understand the issue.
azar khasimazar khasim
Hello Rohit,

I have a relation between BookingLink and Account.
I have a account field in BookingLink Object.
It was working manually when i copy paste the Account name in Booking link account filed it was updating the owner name.


But i want that process to get automate by using Trigger.

whenever record is created in BookingLink, then it need to check for CustomerNumber in Account object,
then it need to update the Account name and Owner in Booking Link Object.


Hope you got my requirement in detail.

So please help me to resolve the issue which i have!!!!

Thanks and Regards,
Azar Khasim.
 
Rohit KumawatRohit Kumawat
Hi Azar

See the following 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)
    {
        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)
        {
           if(bl.Customer_Number__c == a.Customer_Number__c)
            {
                bl.Account__c = a.Id;
            	bl.OwnerId = a.OwnerId;
            }
        }
    }   
}

i have an account record which having customer number . 
See in bellow image account name is Sara and Customer Number is 2
User-added image

Now i will go to billing Link object and will create a record
See in bellow image: I have not filled account field value

User-added image

Now i will click on save
and the result you see in next bellow image:

Account Name is updated as Sara and owner is me bcz i was owner of sara also
and customer number is 2
User-added image

I hope this is only your requirments.
azar khasimazar khasim
Hi Rohit,

Yes, what you have shown is helpful and its working, Thank you,

But, I am Using this code i am facing some errors can u please help me in this.
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.


Thanks and Regards,
Azar Khasim.
Rohit KumawatRohit Kumawat
Hi Azar,

Line 8: accountList = [Select Id, OwnerId, Customer_Number__pc from Account where Customer_Number__c =:CustomNumber ];

In Line number 8 : in the query after where clause use same API name as Customer_Number__pc,  So qrery will be like this:
[Select Id, OwnerId, Customer_Number__pc from Account where Customer_Number__pc =:CustomNumber ];


Line 6:  CustomNumber = bl.Customer_Number__c;

So in line number 6 may be  data type of your Custome Number field in Billing link is Text u have teken and you are assigning to Decimal CustomNumber


So so do one thing either you change data type of Customer_Number__c  or you can change data type in code of CustomNumber variable


your error will go.


I hope this information will be helpful.

 
azar khasimazar khasim
Hi Rohit,

Yes, I solved it at that time only, and thanks for the steps, i have done the same for it before.

But, as u have shown me using the front end part like giving customer number directly in booking link object and autopopulating the names.

But i am using Sites to save a record in Booking Link Object, so while using this Trigger as Active, I am unable to save the record into Booking Link from the Sites.
The Trigger itself not allowing to save the record.
When i Inactive the trigger.
The record is getting saved.


Please help me from this issue.

Thanks and Regards,
Azar Khasim.