Activity Forums Salesforce® Discussions How to populate Child Field from a Parent field in Salesforce?

  • sushant

    Member
    January 27, 2017 at 2:14 pm

    Hi Vikas,

    Try this:

    trigger trigsetLastName on Contact (after insert,after update)
    {
    Map<id,account> parentacc = new Map<id,account>();
    Set<id> listids = new Set<id>();

    for( contact childobj :Trigger.new)
    {
    listids.add(childobj.accountid); // error in this line
    }

    parentacc= new map<id,account> ([select id,Name ,(select id,LastName from contacts) from account where id in: listids]);

    for ( contact c :trigger.new)
    {
    account myacc = parentacc.get(c.accountid);
    myacc.Name=c.LastName;
    }
    Update parentacc.values();
    }

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos