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
Poorna Developer 13Poorna Developer 13 

Account lookup field update with custom field value

Hi Everyone,
  I have Cus_Object__c and Account(Cust_Status__c in Account). In my Cus_Object__c have a field called 'Cus_Status__c' this field values are inserted by apex code.
Like:
Cus_Object__c cus  = new Cus_Object__c();
cus.Cus_Status__c = 'Success';

Whenever the Cus_Status__c field value is not null, I need to update Cus_Status__c field value in my Account object Cust_Status__c field.

Is possible? Any Suggestions?
Thanks in advance.
Best Answer chosen by Poorna Developer 13
Suraj Tripathi 47Suraj Tripathi 47

You can take references from the below code.

trigger updatecontact1 on  Contact (after insert){
    List<contact> lstConUpdate = new List<Contact>();
    set<Id> sAccId = new set<Id>();
    for(Contact con: trigger.new){
        sAccId.add(con.AccountId);
    }
    List<Account> lstAccount = [select id, Address_1__c, (select id,Address_1__c from contacts) from account where id IN: sAccId];
    for(Account acc: lstAccount){
        for(Contact con: acc.contacts){
            con.Address_1__c = acc.Address_1__c;
            lstConUpdate.add(con);
        }
    }
    if(lstConUpdate.size() > 0){
        update lstConUpdate;
    }   
}
Thank You

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Poorna,

Seems possible with after insert/update trigger. Have you tried that?
Aparna JAparna J
you can use below logic in trigger or Apex Class to update Account:

Account objAcc = new Account(Id = cus.Account__c);
objAcc.Cust_Status__c = cus.Cus_Status__c;

update objAcc;
Suraj Tripathi 47Suraj Tripathi 47

You can take references from the below code.

trigger updatecontact1 on  Contact (after insert){
    List<contact> lstConUpdate = new List<Contact>();
    set<Id> sAccId = new set<Id>();
    for(Contact con: trigger.new){
        sAccId.add(con.AccountId);
    }
    List<Account> lstAccount = [select id, Address_1__c, (select id,Address_1__c from contacts) from account where id IN: sAccId];
    for(Account acc: lstAccount){
        for(Contact con: acc.contacts){
            con.Address_1__c = acc.Address_1__c;
            lstConUpdate.add(con);
        }
    }
    if(lstConUpdate.size() > 0){
        update lstConUpdate;
    }   
}
Thank You
This was selected as the best answer