Skip to main content
Manish pol ha preguntado en #Apex
Hi All,

I need to write trigger on contact object.

My problem is whenever i change the account name from in existing contact record then all account name should be changed related to contact object record like case and task. In case record and task record account name should be changed whenever i change account name on contact record.

Thanks,
3 respuestas
  1. 3 mar 2018, 9:50
    Hi Roy

    try below code and let me know if it doesn't work

    trigger updateRelatedRecords on Contact (before update) {

    List<Case> caseList = new List<Case>();

    List<Task> taskList = new List<Task>();

    for(Contact con: trigger.new){

    Contact con1 = trigger.old[0];

    if(con1.AccountId != con.AccountId){

    Task newTask = [Select Id from Task where WhoId =: con.Id limit 1];

    newTask.WhatId = con.AccountId;

    Case newCase = [Select Id from Case where ContactId =: con.Id limit 1];

    newCase.AccountId = con.AccountId;

    taskList.add(newTask);

    caseList.add(newCase);

    }

    }

    update taskList;

    update caseList;

    }

    I tried it and it is working for me
0/9000