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
NPnpNPnp 

update trigger

Hi,

 

I have 2 objects, one custom object CustomEvent__c and another salesforce's event object.

I want the objects are same when CustomEvent__c is updated.

 

The trigger can be fired. But the data is not updated.

please help me to write a code for update.

 

the code is below.

 

 

trigger UpdtEvent on CustomEvent__c (before Update) {

    for (CustomEvent__c ce: Trigger.New){
        Event e = [select id from Event where CustomEventID__c =: ce.id];
            e.OWNERID = ce.OWNERID;
            e.SUBJECT = ce.NAME;
            e.ToDoType__c = ce.ToDoType__c;
            e.SubTypeCD__c = ce.SubTypeCD__c;
            e.ParticipationNum__c = ce.ParticipationNum__c;
            e.Description = ce.Note__c;
            e.WHOID = ce.ContactName2__c;
            e.WHATID = ce.AccountName2__c;
            e.CalledStatus__c = ce.CalledStatus__c;
            e.STARTDATETIME = ce.CALLSTARTTM__C;
            e.ENDDATETIME = ce.CALLENDTM__C;   
  }
}

Alok_NagarroAlok_Nagarro

Hi,

 

There are 2 things need to be make sure, 1st is remove the SOQL query from within the loop ( it can hit gov limit).

2nd thing is that you have to update event object using dml statement, it's not gonna update itself. So here also make sure you aren't gonna put dml statement within the loop, just collect event record in a list and then after loop update that list.

jd123jd123

Hi

 

 


trigger UpdtEvent on CustomEvent__c (before Update)
{
List<CustomEvent__c > customeventsWithEvents=[select id,Name, Ownerid, ToDoType__c,SubTypeCD__c,ParticipationNum__c,Note__c,ContactName2__c,
AccountName2__c,CalledStatus__c,CALLSTARTTM__C,
CALLENDTM__C
(select id,Name,OWNERID ,SUBJECT ,ToDoType__c ,SubTypeCD__c,ParticipationNum__c,Description,
WHOID,WHATID ,CalledStatus__c ,STARTDATETIME ,ENDDATETIME from Events) from CustomEvent__c where Id IN :Trigger.newMap.keySet()];

List<Event> eventsListUpdate= new List<Event>{};

for(CustomEvent__c ce: customeventsWithEvents)
{
// Use the child relationships dot syntax to access the related Contacts
for(Event e: ce.Events)
{
e.OWNERID = ce.OWNERID;
e.SUBJECT = ce.NAME;
e.ToDoType__c = ce.ToDoType__c;
e.SubTypeCD__c = ce.SubTypeCD__c;
e.ParticipationNum__c = ce.ParticipationNum__c;
e.Description = ce.Note__c;
e.WHOID = ce.ContactName2__c;
e.WHATID = ce.AccountName2__c;
e.CalledStatus__c = ce.CalledStatus__c;
e.STARTDATETIME = ce.CALLSTARTTM__C;
e.ENDDATETIME = ce.CALLENDTM__C;

eventsListUpdate.add(e);
}
}

update eventsListUpdate;
}

 

If it is resolve your question please mark as accept as a solution if not please let me know?