+ Start a Discussion
Salesforce BlitzSalesforce Blitz 

Updating existing record crreating new account in salesforce.Help needed

I have created a Event Staging object and a trigger which creates Events/Event Relations after insert of an Event Staging record.
There is a field meetingUID on EventStaging.It is not set set to unique in SF.However if a duplicate field with same meetingUID value is inserted into object, it should fetch and update the original Event Staging object record.
How to achieve this? I am trying in before insert trigger the below code. However a new record gets created every time which leads to record duplication.
Any help appreciated.

 
Public class EventnEventRelation 
{
    Set<string> userset=new set<string>();
    Set<string> EventcreatedBy=new set<string>();
    Set<string> EventMeetingUID=new set<string>();
    List<user> userlist=new List<user>();
    map<string,id> map1=new map<string,id>();
    map<string,string> map2=new map<string,string>();
    map<string,Sobject> map3=new map<string,Sobject>();
    List<Event_Staging__c> userlist1 =new List<Event_Staging__c>();
    List<event> eventlist=new list<event>();
    List<eventrelation> eventrelationlist=new list<eventrelation>();
    List<eventrelation> maineventrelationlist=new list<eventrelation>();
    List<Event_Staging__c > oldeventrelationlist=new list<Event_Staging__c >();
    List<Event_Staging__c > Evstagelist = new List<Event_Staging__c >();
    Set< String > MeetingUidSet = new Set< String >();
    public sObject sobj{get;set;}



    Public EventnEventRelation()
    {
    //Set<string> userset=new set<string>();
    //fetch list of all active Users
    userlist=[Select id,Name,Email from User where IsActive = true];
    //Create a map of emailId and ID of users
    //map<string,id> map1=new map<string,id>();
    for(user us:userlist)
    {
        map1.put(us.email,us.id);
    }

    userlist1=[Select Event_Created_By__c,Meeting_UID__c,Event_Attendees__c,Event_CC__c FROM Event_Staging__c];
    //Create map of meetingUID and eventattendeed of eventstaging
    //map<string,string> map2=new map<string,string>();
    for( Event_Staging__c  Evs1:userlist1)
    {
        map2.put(Evs1.Meeting_UID__c,Evs1.Event_Attendees__c+';'+Evs1.Event_CC__c);
        EventcreatedBy.add(Evs1.Event_Created_By__c);
        EventMeetingUID.add(Evs1.Meeting_UID__c);
        //map3.put(Evs1.Meeting_UID__c,Evs1.id);
    }        

    for(User Uu:userlist)
    {
    userset.add(uu.Email);
    }
    //userset.addall(userlist);

    }

    Public void EventmethodBefore(List<Event_Staging__c> ESList)
    {

        for( Event_Staging__c ec : ESList ) MeetingUidSet.add(ec.Meeting_UID__c);
        for( Event_Staging__c  Evss:[Select Id,Meeting_UID__c FROM Event_Staging__c WHERE Meeting_UID__c=:MeetingUidSet])
        map3.put(Evss.Meeting_UID__c,Evss);

        for( Event_Staging__c ec : ESList )
        {
            sobj = map3.get(ec.Meeting_UID__c);
            If(sobj !=NULL)
            {
                sobj.put('Name', ec.Name);
                sobj.put('Event_Attendees__c', ec.Event_Attendees__c);
                sobj.put('Event_Attendees__c', ec.Event_Attendees__c);
                sobj.put('Event_CC__c', ec.Event_CC__c);
                sobj.put('Event_Start_Date__c', ec.Event_Start_Date__c);
                sobj.put('Event_End_Date__c', ec.Event_End_Date__c);
                sobj.put('Event_Duration_in_Minutes__c', ec.Event_Duration_in_Minutes__c);
                sobj.put('Event_Location__c', ec.Event_Location__c);
                sobj.put('Meeting_UID__c', ec.Meeting_UID__c);
                sobj.put('Event_Description__c', ec.Event_Description__c);


            }
        }
    }   



      Public void Eventmethod(List<Event_Staging__c> ESList)
    //Public void Eventmethod(List<Event_Staging__c> ESList)
    {  
    For(Event_Staging__c ES: ESList)
        {

            If(userset.contains(ES.Event_Created_By__c) )
            {
                Event ev = new Event();
                Id Id1=map1.get(ES.Event_Created_By__c);
                ev.ownerId=Id1;
                system.debug('Event created By'+id1);
                ev.Subject=ES.Name;
                ev.Location=ES.Event_Location__c;
                ev.StartDateTime=ES.Event_Start_Date__c;
                ev.EndDateTime=ES.Event_End_Date__c;
                ev.Description=ES.Meeting_UID__c;
                ev.DurationInMinutes=ES.Event_Duration_in_Minutes__c.intvalue();
                eventlist.add(ev);



            }
            else
            {
                Event ev1 = new Event();
                ev1.ownerId=UserInfo.getUserId();
                ev1.Subject=ES.Name;
                //ev1.setEventWhoIds();
                ev1.Location=ES.Event_Location__c;
                ev1.StartDateTime=ES.Event_Start_Date__c;
                ev1.EndDateTime=ES.Event_End_Date__c;
                ev1.Description=ES.Meeting_UID__c;
                ev1.DurationInMinutes=ES.Event_Duration_in_Minutes__c.intvalue();
                eventlist.add(ev1);

            }

        //}
        }

        if(eventlist.size()>0 && eventlist!=NULL)
        {
            system.debug('Eventlist Size------'+eventlist.size());
            insert eventlist;
            system.debug('Eventlist Size------'+eventlist[0]);
            system.debug('Eventlist Size------'+eventlist[0].subject);
        }
        set<string> set3=new set<string>();
        List<Event_Staging__c> UpdateEventstaging = new list<Event_Staging__c>();
        for(Event ECB : eventlist)
        {
            set3.add(ECB.description);
        }

        for(Event_Staging__c  EVSCB :[select id,name,Meeting_UID__c FROM Event_Staging__c] )
        {
            If(set3.contains(EVSCB.Meeting_UID__c))
            {
                EVSCB.IsEventCreated__c=TRUE;
                UpdateEventstaging.add(EVSCB);
            }
        }
        If(UpdateEventstaging.size()>0 && UpdateEventstaging!=NULL)
        {
            update UpdateEventstaging;
        }



        for(event ev:eventlist)
        {
                //List<String> ReciepientMail= EVS.Event_Attendees__c.split(',');
                string str=map2.get(ev.Description);
                system.debug('Map1 size---------'+map1.size());
                system.debug('Map2 size---------'+map2.size());
                system.debug('string --------'+str);

                List<String> ReciepientMail= str.split(';');
                for(integer i=0;i<ReciepientMail.size();i++)
                {
                    If(userset.contains(ReciepientMail[i]))
                    {
                    eventrelation evr=new eventrelation();
                    evr.eventid=ev.Id;
                    evr.relationid=map1.get(ReciepientMail[i]);
                    evr.status='New';
                    evr.respondeddate=datetime.now();
                    eventrelationlist.add(evr);
                    //maineventrelationlist.addall(eventrelationlist);
                    //eventrelationlist.clear();
                    }
                }
                maineventrelationlist.addall(eventrelationlist);
                eventrelationlist.clear();




        }

       if(maineventrelationlist.size()>0 && maineventrelationlist!=NULL)
        {
             insert maineventrelationlist;
        }
        //delete [select id from Event_Staging__c where id in :Trigger.new];

    }

    Public void DeleteOldEventStagings()
    {
        List<Event_Staging__c > DelList=new List<Event_Staging__c>();
        DelList=[Select Id,name from Event_Staging__c where IsEventCreated__c=FALSE AND CreatedDate <= LAST_N_DAYS:01 ];
        Delete DelList;
    }

}



 
SandhyaSandhya (Salesforce Developers) 
Hi,
Please refer salesforce helps document which has the sample code.This can help you to cross check your code.

http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
 
Hope this helps you!

Please mark it as BestAnswer if this helps you so that it will make available for other as a proper solution.

Thanks and Regards
Sandhya