• Satyavani Ravikindi
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I am new to development and can someone help to write trigger for the senario:

whenever a new guest is created (custom object Event Guest) trigger should find if there is an existing lead or contact, if any exists it should add lead/contact to campaign otherwise create a new lead and add to campaign. Campaign is find this way Event Guest->Event->Campaign.

trigger NewGuest on Event_Guests__c(after insert){
    for(Event_Guests__c myGuest : Trigger.New) {
        List<Lead> exLead = [SELECT Id FROM Lead WHERE Email = :myGuest.Guest_Email__c];
            if (exLead.size() == 0)
                {
                    List<Contact> exContact = [SELECT Id FROM Contact WHERE Email = :myGuest.Guest_Email__c];
                    if(exContact.size() == 0)
                        {
                            Lead newLead = new Lead();
                            newLead.FirstName = myGuest.Name;
                            newLead.LastName = myGuest.Last_Name__c;
                            newLead.Phone = myGuest.Guest_Phone__c;
                            newLead.Email = myGuest.Guest_Email__c;
                            newLead.LeadSource = 'Workshop Guest';
                            insert newLead;
                    
                            Campaignmember newMember = new Campaignmember();
                            newMember.LeadId = newLead.id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }
                    else
                        {
                            Campaignmember newMember = new Campaignmember();
                            newMember.Contact = exContact.Id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }               
                    
                }               
            if (exLead.size() != 0)
                {
                    Campaignmember newMember = new Campaignmember();
                    newMember.Lead = exLead.Id;
                    newMember.dc__Registered_First_Name__c = myGuest.Name;
                    newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                    newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                    newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                    newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                    insert newMember;
                }
                
            }
    }
I am new to development and can someone help to write trigger for the senario:

whenever a new guest is created (custom object Event Guest) trigger should find if there is an existing lead or contact, if any exists it should add lead/contact to campaign otherwise create a new lead and add to campaign. Campaign is find this way Event Guest->Event->Campaign.

trigger NewGuest on Event_Guests__c(after insert){
    for(Event_Guests__c myGuest : Trigger.New) {
        List<Lead> exLead = [SELECT Id FROM Lead WHERE Email = :myGuest.Guest_Email__c];
            if (exLead.size() == 0)
                {
                    List<Contact> exContact = [SELECT Id FROM Contact WHERE Email = :myGuest.Guest_Email__c];
                    if(exContact.size() == 0)
                        {
                            Lead newLead = new Lead();
                            newLead.FirstName = myGuest.Name;
                            newLead.LastName = myGuest.Last_Name__c;
                            newLead.Phone = myGuest.Guest_Phone__c;
                            newLead.Email = myGuest.Guest_Email__c;
                            newLead.LeadSource = 'Workshop Guest';
                            insert newLead;
                    
                            Campaignmember newMember = new Campaignmember();
                            newMember.LeadId = newLead.id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }
                    else
                        {
                            Campaignmember newMember = new Campaignmember();
                            newMember.Contact = exContact.Id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }               
                    
                }               
            if (exLead.size() != 0)
                {
                    Campaignmember newMember = new Campaignmember();
                    newMember.Lead = exLead.Id;
                    newMember.dc__Registered_First_Name__c = myGuest.Name;
                    newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                    newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                    newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                    newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                    insert newMember;
                }
                
            }
    }
I am new to development and can someone help to write trigger for the senario:

whenever a new guest is created (custom object Event Guest) trigger should find if there is an existing lead or contact, if any exists it should add lead/contact to campaign otherwise create a new lead and add to campaign. Campaign is find this way Event Guest->Event->Campaign.

trigger NewGuest on Event_Guests__c(after insert){
    for(Event_Guests__c myGuest : Trigger.New) {
        List<Lead> exLead = [SELECT Id FROM Lead WHERE Email = :myGuest.Guest_Email__c];
            if (exLead.size() == 0)
                {
                    List<Contact> exContact = [SELECT Id FROM Contact WHERE Email = :myGuest.Guest_Email__c];
                    if(exContact.size() == 0)
                        {
                            Lead newLead = new Lead();
                            newLead.FirstName = myGuest.Name;
                            newLead.LastName = myGuest.Last_Name__c;
                            newLead.Phone = myGuest.Guest_Phone__c;
                            newLead.Email = myGuest.Guest_Email__c;
                            newLead.LeadSource = 'Workshop Guest';
                            insert newLead;
                    
                            Campaignmember newMember = new Campaignmember();
                            newMember.LeadId = newLead.id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }
                    else
                        {
                            Campaignmember newMember = new Campaignmember();
                            newMember.Contact = exContact.Id;
                            newMember.dc__Registered_First_Name__c = myGuest.Name;
                            newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                            newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                            newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                            newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                            insert newMember;
                        }               
                    
                }               
            if (exLead.size() != 0)
                {
                    Campaignmember newMember = new Campaignmember();
                    newMember.Lead = exLead.Id;
                    newMember.dc__Registered_First_Name__c = myGuest.Name;
                    newMember.dc__Registered_Last_Name__c = myGuest.Last_Name__c;
                    newMember.dc__Registered_Email__c = myGuest.Guest_Email__c;
                    newMember.dc__Registered_Phone__c = myGuest.Guest_Phone__c;
                    newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
                    insert newMember;
                }
                
            }
    }