Skip to main content
Mayur Shinde preguntó en #Apex
Practice Use Case:

1.create a checkbox 'create contact' on Account.

2.create a trigger which handles events like account insert, update, delete.

3.Trigger will be responsible for creating contact with defined set of fields. It will be responsible to keep key information of contact updated from Account. Hence Account will still be treated as master for key information.  
5 respuestas
  1. 8 nov 2017, 00:56
    Hello Mayur,

    Did you mean, when user selects create Account checkbox and save the record, a trigger should and a related contact record should get created?

    Do you really want this for account update and delete? Because this will fire the trigger every time the account is updated and does not make a use case to create a contact on Account delete.

    but anyways, here it is :

    trigger InsertContactOnAccountUpsert on Account (after insert, after update) {

    if(Trigger.isInsert)

    {

    List<Contact> ct = new List <Contact>();

    for (Account acc: Trigger.New)

    {

    // Insert a contact on account insert with mapping

    // You can add the logic here to check if acount create contact is true or not

    ct.add (new Contact(

    FirstName = 'Account',

    LastName = 'Last Name',

    Email = 'Account@email.com',

    AccountId = acc.id,

    Fax=acc.Fax,

    MailingStreet=acc.BillingStreet,

    MailingCity=acc.BillingCity,

    MailingState=acc.BillingState,

    MailingPostalCode=acc.BillingPostalCode,

    MailingCountry=acc.BillingCountry,

    Phone=acc.Phone

    )

    );

    }

    if(!ct.isEmpty())

    insert ct;

    }

    }

    Hope this helps.

    Cheers.​ 
0/9000