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
RbnRbn 

Help on trigger

Hi ..

 

I am getting the below error when trying to save the record

 

 

Error:  EventObjectSync: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Standard_Event__c duplicates value on record with id: a0Vg0000000URsh: [] Trigger.EventObjectSync: line 17, column 1

 

trigger::

trigger EventObjectSync on Event(After Insert,After Update) {
  list<Event_Object__c> eventlist2= new list<Event_Object__c>();
list<id> eventids= new list<id>();
map<id,Event_Object__c> custeventinserted = new map<id,Event_Object__c>();
 
          if (EventObjectSyncClass.check){
                        
            list<Event_Object__c> eventlist = new list<Event_Object__c>();
            list<UserAccountTeamMember> ues= new list<UserAccountTeamMember>();
            list<Event_Object__Share> evntObjShare2 = new list<Event_Object__Share>();
            
            for (integer a=0 ; a<Trigger.new.size() ; a++) {
              eventlist.add(EventObjectSyncClass.insertCustom(Trigger.new[a]));
              ues = [select id,UserId,TeamMemberRole,AccountAccessLevel from UserAccountTeamMember where OwnerId = :Trigger.new[a].OwnerId ];
            }
            EventObjectSyncClass.check=False;
            Insert eventlist;
            for(UserAccountTeamMember a :ues){
                Event_Object__Share evntObjShare = new Event_Object__Share();
                string str1='';
                string str2='';
                str1=eventlist[0].id;
                str2=a.UserId;
                evntObjShare.ParentId = eventlist[0].id;
                evntObjShare.UserOrGroupId = a.UserId;
                evntObjShare.AccessLevel = 'Edit';
                evntObjShare2.add(evntObjShare);
                evntObjShare = new Event_Object__Share();
            }      
            if(evntObjShare2.size()>0){
 
            try{
            //insert tskObjShare2;
            list<Database.SaveResult> sr = Database.insert(evntObjShare2,false);
                     }
            catch (DmlException de) {
            
    Integer numErrors = de.getNumDml();
    for(Integer i=0;i<numErrors;i++) {
 System.debug('getDmlFields=' + de.getDmlFields(i));
  System.debug('getDmlMessage=' + de.getDmlMessage(i));  
                        }
                    } 
                }            
            }
 
 if (Trigger.isUpdate){
          if (EventObjectSyncClass.check){
              
              eventids= new list<id>();
              custeventinserted = new map<id,Event_Object__c>();
              
              for (integer a=0 ; a<Trigger.new.size() ; a++) {
              if(Trigger.new[a].TaskTemplateId__c!=null){
                eventids.add(Trigger.new[a].TaskTemplateId__c);
              }            
              }
              if(eventids.size()>0){
                  eventlist2=[select id,Subject__c,Description__c,Start__c,End__c,Assigned_To__c,Type__c,Reason__c,IsAllDayEvent__c,Show_Time_As__c,Location__c,Priority__c,What__c,Who__c,Private__c,Email__c,Phone__c,Reminder__c from Event_Object__c where id in : eventids];
                  for(Event_Object__c ta:eventlist2){
                     custeventinserted.put(ta.id,ta);
                  }
                 eventids= new list<id>(); 
                 eventlist2= new list<Event_Object__c>();
               }
             for (integer a=0 ; a<Trigger.new.size() ; a++) {
               eventlist2.add(EventObjectSyncClass.Updatecustom(custeventinserted.get(Trigger.new[a].TaskTemplateId__c),Trigger.new[a]));
            }
            EventObjectSyncClass.check=False;
            update eventlist2; 
           }
        }
        }

 

Public class EventObjectSyncClass{
    
    Public static boolean check=True;

    
     Public static Event_Object__c Updatecustom(Event_Object__c CustEvent,Event StdEvent){
                    if(StdEvent.owner__c != null){
                    CustEvent.ownerid=StdEvent.owner__c;
                    }    
                    if(StdEvent.Subject != null){
                    CustEvent.Subject__c=StdEvent.Subject;
                    }
                    
                    if(StdEvent.Type != null){
                    CustEvent.Type__c=StdEvent.Type;
                    }
                    
                   /* if(StdEvent.Description != null){
                    CustEvent.Description=StdEvent.Description;
                    CustEvent.Description=CustEvent.Description+' This was created on be half of other employee ';
                    }else {
                    string str=' This was created on be half of other employee ';
                    str=str+StdEvent.Owner.Name;
                    CustEvent.Description=str;
                    }*/
                    if(StdEvent.Description != null){
                    CustEvent.Description__c=StdEvent.Description;
                    }
                    if(StdEvent.Act_Type__c != null){
                    CustEvent.Reason__c=StdEvent.Act_Type__c;
                    }
              
                    if(StdEvent.IsAllDayEvent != null){
                    CustEvent.IsAllDayEvent__c=StdEvent.IsAllDayEvent;
                    }
					if(StdEvent.EndDateTime != null){
                    CustEvent.Start__c=StdEvent.EndDateTime;
                    }
                    if(StdEvent.EndDateTime != null){
                    CustEvent.End__c=StdEvent.EndDateTime;
                    }
                    
                    if(StdEvent.WhatId != null){
                    CustEvent.What__c=StdEvent.WhatId;
                    }
                    
                    if(StdEvent.WhoId != null){
                    CustEvent.Who__c=StdEvent.WhoId;
                    }
                    
                  if(StdEvent.IsPrivate != null){
                    CustEvent.Private__c=StdEvent.IsPrivate;
                    }
                    
                    if(StdEvent.Priority__c != null){
                    CustEvent.Priority__c=StdEvent.Priority__c;
                    }
                         if(StdEvent.Location != null){
                    CustEvent.Location__c=StdEvent.Location;
                    }
                   
                        if(StdEvent.ShowAs != null){
                    CustEvent.Show_Time_As__c=StdEvent.ShowAs;
                    }
  
     return CustEvent;
    }
    
    Public static Event_Object__c insertCustom(Event StdEvent ){
    Event_Object__c CustEvent = new Event_Object__c();
    if(StdEvent.OwnerId != null){
                    CustEvent.Assigned_To__c=StdEvent.OwnerId;
                    }
                    
                    if(StdEvent.Subject != null){
                    CustEvent.Subject__c=StdEvent.Subject;
                    }
                    
                    if(StdEvent.Type != null){
                    CustEvent.Type__c=StdEvent.Type;
                    }
                     if(StdEvent.StartDateTime != null){
                    CustEvent.Start__c=StdEvent.StartDateTime;
                    }
                        if(StdEvent.IsAllDayEvent != null){
                    CustEvent.IsAllDayEvent__c=StdEvent.IsAllDayEvent;
                    }
                
                    
                    if(StdEvent.EndDateTime != null){
                    CustEvent.End__c=StdEvent.EndDateTime;
                    }
                    if(StdEvent.Act_Type__c != null){
                    CustEvent.Reason__c=StdEvent.Act_Type__c;
                    }
                              
                    if(StdEvent.WhatId != null){
                    CustEvent.What__c=StdEvent.WhatId;
                    }
                    
                    if(StdEvent.WhoId != null){
                    CustEvent.Who__c=StdEvent.WhoId;
                    }
                        if(StdEvent.IsPrivate != null){
                    CustEvent.Private__c=StdEvent.IsPrivate;
                    }
                   if(StdEvent.Priority__c != null){
                    CustEvent.Priority__c=StdEvent.Priority__c;
                    }
                    if(StdEvent.Location != null){
                    CustEvent.Location__c=StdEvent.Location;
                    }
                    if(StdEvent.ShowAs != null){
                    CustEvent.Show_Time_As__c=StdEvent.ShowAs;
                    }
                    if(StdEvent.Description != null){
                    CustEvent.Description__c=StdEvent.Description;
                    }
         CustEvent.Standard_Event__c = StdEvent.id;
         CustEvent.name='Custom Event';
    return CustEvent;
    }
     
     

 

 
Class :::
 

 

Atul111Atul111

Rabi,

 

It might be possible that u have create any unique field and when you are trying to submit it is showing error.

 

This very tipical to solve this problem like this. If you need help we can discuss this issue


rabi wrote:

Hi ..

 

I am getting the below error when trying to save the record

 

 

Error:  EventObjectSync: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Standard_Event__c duplicates value on record with id: a0Vg0000000URsh: [] Trigger.EventObjectSync: line 17, column 1

 

trigger::

trigger EventObjectSync on Event(After Insert,After Update) {
  list<Event_Object__c> eventlist2= new list<Event_Object__c>();
list<id> eventids= new list<id>();
map<id,Event_Object__c> custeventinserted = new map<id,Event_Object__c>();
 
          if (EventObjectSyncClass.check){
                        
            list<Event_Object__c> eventlist = new list<Event_Object__c>();
            list<UserAccountTeamMember> ues= new list<UserAccountTeamMember>();
            list<Event_Object__Share> evntObjShare2 = new list<Event_Object__Share>();
            
            for (integer a=0 ; a<Trigger.new.size() ; a++) {
              eventlist.add(EventObjectSyncClass.insertCustom(Trigger.new[a]));
              ues = [select id,UserId,TeamMemberRole,AccountAccessLevel from UserAccountTeamMember where OwnerId = :Trigger.new[a].OwnerId ];
            }
            EventObjectSyncClass.check=False;
            Insert eventlist;
            for(UserAccountTeamMember a :ues){
                Event_Object__Share evntObjShare = new Event_Object__Share();
                string str1='';
                string str2='';
                str1=eventlist[0].id;
                str2=a.UserId;
                evntObjShare.ParentId = eventlist[0].id;
                evntObjShare.UserOrGroupId = a.UserId;
                evntObjShare.AccessLevel = 'Edit';
                evntObjShare2.add(evntObjShare);
                evntObjShare = new Event_Object__Share();
            }      
            if(evntObjShare2.size()>0){
 
            try{
            //insert tskObjShare2;
            list<Database.SaveResult> sr = Database.insert(evntObjShare2,false);
                     }
            catch (DmlException de) {
            
    Integer numErrors = de.getNumDml();
    for(Integer i=0;i<numErrors;i++) {
 System.debug('getDmlFields=' + de.getDmlFields(i));
  System.debug('getDmlMessage=' + de.getDmlMessage(i));  
                        }
                    } 
                }            
            }
 
 if (Trigger.isUpdate){
          if (EventObjectSyncClass.check){
              
              eventids= new list<id>();
              custeventinserted = new map<id,Event_Object__c>();
              
              for (integer a=0 ; a<Trigger.new.size() ; a++) {
              if(Trigger.new[a].TaskTemplateId__c!=null){
                eventids.add(Trigger.new[a].TaskTemplateId__c);
              }            
              }
              if(eventids.size()>0){
                  eventlist2=[select id,Subject__c,Description__c,Start__c,End__c,Assigned_To__c,Type__c,Reason__c,IsAllDayEvent__c,Show_Time_As__c,Location__c,Priority__c,What__c,Who__c,Private__c,Email__c,Phone__c,Reminder__c from Event_Object__c where id in : eventids];
                  for(Event_Object__c ta:eventlist2){
                     custeventinserted.put(ta.id,ta);
                  }
                 eventids= new list<id>(); 
                 eventlist2= new list<Event_Object__c>();
               }
             for (integer a=0 ; a<Trigger.new.size() ; a++) {
               eventlist2.add(EventObjectSyncClass.Updatecustom(custeventinserted.get(Trigger.new[a].TaskTemplateId__c),Trigger.new[a]));
            }
            EventObjectSyncClass.check=False;
            update eventlist2; 
           }
        }
        }

 

Public class EventObjectSyncClass{
    
    Public static boolean check=True;

    
     Public static Event_Object__c Updatecustom(Event_Object__c CustEvent,Event StdEvent){
                    if(StdEvent.owner__c != null){
                    CustEvent.ownerid=StdEvent.owner__c;
                    }    
                    if(StdEvent.Subject != null){
                    CustEvent.Subject__c=StdEvent.Subject;
                    }
                    
                    if(StdEvent.Type != null){
                    CustEvent.Type__c=StdEvent.Type;
                    }
                    
                   /* if(StdEvent.Description != null){
                    CustEvent.Description=StdEvent.Description;
                    CustEvent.Description=CustEvent.Description+' This was created on be half of other employee ';
                    }else {
                    string str=' This was created on be half of other employee ';
                    str=str+StdEvent.Owner.Name;
                    CustEvent.Description=str;
                    }*/
                    if(StdEvent.Description != null){
                    CustEvent.Description__c=StdEvent.Description;
                    }
                    if(StdEvent.Act_Type__c != null){
                    CustEvent.Reason__c=StdEvent.Act_Type__c;
                    }
              
                    if(StdEvent.IsAllDayEvent != null){
                    CustEvent.IsAllDayEvent__c=StdEvent.IsAllDayEvent;
                    }
					if(StdEvent.EndDateTime != null){
                    CustEvent.Start__c=StdEvent.EndDateTime;
                    }
                    if(StdEvent.EndDateTime != null){
                    CustEvent.End__c=StdEvent.EndDateTime;
                    }
                    
                    if(StdEvent.WhatId != null){
                    CustEvent.What__c=StdEvent.WhatId;
                    }
                    
                    if(StdEvent.WhoId != null){
                    CustEvent.Who__c=StdEvent.WhoId;
                    }
                    
                  if(StdEvent.IsPrivate != null){
                    CustEvent.Private__c=StdEvent.IsPrivate;
                    }
                    
                    if(StdEvent.Priority__c != null){
                    CustEvent.Priority__c=StdEvent.Priority__c;
                    }
                         if(StdEvent.Location != null){
                    CustEvent.Location__c=StdEvent.Location;
                    }
                   
                        if(StdEvent.ShowAs != null){
                    CustEvent.Show_Time_As__c=StdEvent.ShowAs;
                    }
  
     return CustEvent;
    }
    
    Public static Event_Object__c insertCustom(Event StdEvent ){
    Event_Object__c CustEvent = new Event_Object__c();
    if(StdEvent.OwnerId != null){
                    CustEvent.Assigned_To__c=StdEvent.OwnerId;
                    }
                    
                    if(StdEvent.Subject != null){
                    CustEvent.Subject__c=StdEvent.Subject;
                    }
                    
                    if(StdEvent.Type != null){
                    CustEvent.Type__c=StdEvent.Type;
                    }
                     if(StdEvent.StartDateTime != null){
                    CustEvent.Start__c=StdEvent.StartDateTime;
                    }
                        if(StdEvent.IsAllDayEvent != null){
                    CustEvent.IsAllDayEvent__c=StdEvent.IsAllDayEvent;
                    }
                
                    
                    if(StdEvent.EndDateTime != null){
                    CustEvent.End__c=StdEvent.EndDateTime;
                    }
                    if(StdEvent.Act_Type__c != null){
                    CustEvent.Reason__c=StdEvent.Act_Type__c;
                    }
                              
                    if(StdEvent.WhatId != null){
                    CustEvent.What__c=StdEvent.WhatId;
                    }
                    
                    if(StdEvent.WhoId != null){
                    CustEvent.Who__c=StdEvent.WhoId;
                    }
                        if(StdEvent.IsPrivate != null){
                    CustEvent.Private__c=StdEvent.IsPrivate;
                    }
                   if(StdEvent.Priority__c != null){
                    CustEvent.Priority__c=StdEvent.Priority__c;
                    }
                    if(StdEvent.Location != null){
                    CustEvent.Location__c=StdEvent.Location;
                    }
                    if(StdEvent.ShowAs != null){
                    CustEvent.Show_Time_As__c=StdEvent.ShowAs;
                    }
                    if(StdEvent.Description != null){
                    CustEvent.Description__c=StdEvent.Description;
                    }
         CustEvent.Standard_Event__c = StdEvent.id;
         CustEvent.name='Custom Event';
    return CustEvent;
    }
     
     

 

 
Class :::
 

 


 

RbnRbn
Thanks for your quick reply...

Yes i have a unique field::Standard_Event__c

request you to please help me on this
RbnRbn
Hi Atul...i got rid of the error...but right now i am facing the below error...

EventObjectSync: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Class.EventObjectSyncClass.Updatecustom: line 76, column 1 Trigger.EventObjectSync: line 76, column 1
Atul111Atul111
This is only the problem. you should enter unique value in this field
Atul111Atul111
This is coming because memory has not been allocated to any variable. I need debug this code any then i can help you.
Please confirm if still you are facing this problem.
RbnRbn
yeah atul i am still facing this problem...

EventObjectSync: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Class.EventObjectSyncClass.Updatecustom: line 76, column 1 Trigger.EventObjectSync: line 77, column 1

please help me on this...i am struck since yesterday
Atul111Atul111
I need the access of you sandbox any then i can help you.

Or we can share the screen to resolve the issue
RbnRbn
i am extremely sorry atul..

We can not access this sandbox out of the organization.

If any input required from my side...please let me know...
Atul111Atul111
Can we have a live meeting so that i can resolve this issue?