+ Start a Discussion
Satyavani RavikindiSatyavani Ravikindi 

Trigger to add guest to campaign

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;
                }
                
            }
    }
Neethu uNeethu u
Hi

Please try the below code. I have made few changes in your code(Bold one).

trigger NewGuest on Event_Guests__c(after insert){
    for(Event_Guests__c myGuest : Trigger.New) {
    Id campaignId =null;
        List<Lead> exLead = [SELECT Id FROM Lead WHERE Email = :myGuest.Guest_Email__c];
        List<Contact> exContact = [SELECT Id FROM Contact WHERE Email = :myGuest.Guest_Email__c];
            if (exLead.size() == 0 && 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{     
                            if(exLead.size()>0){
                                campaignId=newLead.id;
                            }
                            else{
                                campaignId=exContact.Id;
                            }

                            Campaignmember newMember = new Campaignmember();
                            newMember.Contact = campaignId;
                            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;
                    }                  
                    
                }               
                            
            }
    }

Please mark it as best answer if it resolves your issue.

Regards,
Neethu 
Satyavani RavikindiSatyavani Ravikindi
Hi Neetu,

Thanks for your reply, I tried your code and I still get the error

Error: Compile Error: Missing '}' at 'else' at line 26 column 16

I could see all braces complete but still it gives me this error on saving

Regards,
Satya
Satyavani RavikindiSatyavani Ravikindi
Hi Neetu,

Its the if statement braces that is causing the error, now I get an error like this  Illegal assignment from List<dc__Event__c> to Campaign at line 23 column 29

Event is a lookup on Event Guest and EventCampaign is a lookup on Event, so I am doing a thrid level fetch for the id



trigger NewGuest on Event_Guests__c(after insert){
    for(Event_Guests__c myGuest : Trigger.New) {
    Id campaignId =null;
        List<Lead> exLead = [SELECT Id FROM Lead WHERE Email = :myGuest.Guest_Email__c];
        List<Contact> exContact = [SELECT Id FROM Contact WHERE Email = :myGuest.Guest_Email__c];
            if (exLead.size() == 0 && 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{     
                            if(exLead.size()>0){
                                campaignId=newLead.id;
                            }
                            else{
                                campaignId=exContact.Id;
                            }
                            Campaignmember newMember = new Campaignmember();
                            newMember.Contact = campaignId;
                            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;
                    } 
                                     
                    
                }               
                            
            
    }
Neethu uNeethu u
Hi

You are getting this error in below line as the SOQL output is a list and you are going to assign it to type Campaign.You can assign only the same type.
newMember.Campaign = [SELECT dc__Event_Campaign__c FROM dc__Event__c WHERE Id = :myGuest.Event__c];
The field Campaign in CampaignMember is a lookup to Campaign Object(which will have campign Id's) . Here you are not assigning campaignId.

So please try to assign a campaignId to newMember.Campaign. Just check your SOQL in developer console that only one campignId is returned which needs to be assigned to newMember.Campaign. 

Let me know if this helps.

Thanks,
Neethu