• andyKal10
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 12
    Replies

I have written a trigger that calls a class, and a test class for them. When I run the test in the ide 'apex test runner' everything seems to be fine. I get 100% coverage in my class and trigger, and no warnings or errors. When I try to deploy the code to my production org I get a null pointer exception on the insert line that is highlighted below.

 

The trigger and class are posted below too.

 

Thanks!

 

@isTest
private class LeadHandlerTest {

static testMethod void testLeadHandler() {

List<Lead> testLeads = new List<Lead>();

Contact contact = new Contact(NPD_Client_Satisfaction_Survey__c = 'Yes',
LastName = 'Test',
JobFunction__c =Admin',
Email = 'test@npd.com',
ContactType__c = 'NPDCustomer');
insert contact;

Lead lead = new Lead(Phone='1234567890',
LeadSource='Advertising',
LastName='TestJohnson',
FirstName='Test',
LeadBU__c='Automotive',
Company='Test Company',
CurrencyIsoCode='USD',
Lead_Type__c='In-Person Meeting',
Status='New Leads - Not yet working',
Rating='Z - Not Contacted - Unknown Quality',
Email='test@npd.com',
MarketingCampaign__c = 'Web-Marketing-Opt In Web Page-200807'
);
testLeads.add(lead);

Lead lead2 = new Lead(Phone='1234567890',
LeadSource='Advertising',
LastName='TestJohnson2',
FirstName='Test2',
LeadBU__c='Beauty',
Company='Test Company',
CurrencyIsoCode='CAD',
Lead_Type__c='In-Person Meeting',
Status='New Leads - Not yet working',
Rating='Z - Not Contacted - Unknown Quality',
Email='test@npd.com'
);
testLeads.add(lead2);

insert testLeads;
}
}

Trigger:

 

trigger leadAfterInsertUpdate on Lead (after insert, after update) {

if(UserInfo.getUserId() <> '00500000006z3qKAAQ'){

Map<String,String> newLeadsMap = new Map<String,String>();

for(Lead newLead : Trigger.new) {
if((null != newLead.Email) && (newLead.MarketingCampaign__c == 'Web-Marketing-Opt In Web Page-200807') && (Trigger.isInsert || newLead.Email != Trigger.oldMap.get(newLead.Id).Email)) {
if(newLeadsMap.containsKey(newLead.Email)) {
newLead.Email.addError('Another new lead is already using this email address.');
}else {
newLeadsMap.put(newLead.Email, newLead.ID);
}
}
}

leadHandler.leadDeDuper(newLeadsMap);
}

}

 Class

 

 

public with sharing class leadHandler {
	
    Public Static Void leadDeDuper(Map<String,String> newLeads) {
        Set<String> leadsToDelete = new Set<String>();
        Map<id,Lead> newLeadMap = new Map <ID, Lead>([Select Id, FirstName, LastName, Phone, PrimaryAreaOfInterest__c, Blog__c, Email, HasOptedOutOfEmail,Press_Releases__c,Insights_Newsletter__c,Product_Announcements__c,Marketing_Opt_Out__c from Lead Where ID IN: newLeads.values()]);
        List<Lead> leadsToDeleteList = new List<Lead>();
        List<Lead> existingLeads = new List<Lead>();
        List<Contact> contactsToDelete = new List<Contact>();
        List<Contact> existingContacts = new List<Contact>();
        
        //finds existing Leads where their email equals the incoming Lead
        for(Lead leadB : [select Id, FirstName, LastName,PrimaryAreaofInterest__c,Phone, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) {
            //adds the incoming lead to a set that will be deleted further down
            leadsToDelete.add(newLeads.get(leadB.email));   
                    
            //updates fields in existing leads
            leadB.HasOptedOutOfEmail = false;
            leadB.Press_Releases__c = newLeadMap.get(newLeads.get(leadB.email)).Press_Releases__c;
            leadB.Insights_Newsletter__c = newLeadMap.get(newLeads.get(leadB.email)).Insights_Newsletter__c;
            leadB.Product_Announcements__c = newLeadMap.get(newLeads.get(leadB.email)).Product_Announcements__c;
            leadB.Blog__c =newLeadMap.get(newLeads.get(leadB.email)).Blog__c;
            leadB.Marketing_Opt_Out__c = false;
            leadB.FirstName = newLeadMap.get(newLeads.get(leadB.email)).FirstName;
            leadB.LastName = newLeadMap.get(newLeads.get(leadB.email)).LastName;
            leadB.PrimaryAreaOfInterest__c = newLeadMap.get(newLeads.get(leadB.email)).PrimaryAreaOfInterest__c;
            leadB.Phone = newLeadMap.get(newLeads.get(leadB.email)).Phone;
           
            existingLeads.add(leadB);
        }
        
        if(existingLeads.size()>0) {
            update existingLeads;
        }
        //finds existing contacts where their email equals the incoming leads
        for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) {
            
            //adds the lead to a set to be deleted below
            leadsToDelete.add(newLeads.get(contactA.Email));
            
            //updates the existing contact          
            contactA.HasOptedOutOfEmail = false;
            contactA.Insights_Newsletter__c = newLeadMap.get(newLeads.get(contactA.email)).Insights_Newsletter__c;
            contactA.Press_Releases__c = newLeadMap.get(newLeads.get(contactA.email)).Press_Releases__c;
            contactA.Product_Announcements__c = newLeadMap.get(newLeads.get(contactA.email)).Product_Announcements__c;
            contactA.Blog__c = newLeadMap.get(newLeads.get(contactA.email)).Blog__c;
            contactA.FirstName = newLeadMap.get(newLeads.get(contactA.email)).FirstName;
            contactA.LastName = newLeadMap.get(newLeads.get(contactA.email)).LastName;
            contactA.Phone = newLeadMap.get(newLeads.get(contactA.email)).Phone;
            
            existingContacts.add(contactA);
        }
       
        if(existingContacts.size()>0) {
            update existingContacts;
        }
        
        if(leadsToDelete.size()>0) {
        	for(Lead dLead : [Select ID from Lead where ID IN:leadsToDelete]) {
        		leadsToDeleteList.add(dLead);
        	}
            delete leadsToDeleteList;
        }
        
    }

}

 

 

 

 

 

I have made a trigger that intercepts leads that are created by our web-to-lead form. The trigger ensures that there are no duplicate leads based on email, (which is probably an unnecessary safety step because I don't there is a way for two leads to be submitted by one web-form). It then packages those unique leads into a map and sends it over to the class I have pasted below. The goal of the class is to find existing leads and contacts and update them with the data supplied by the incoming lead, and then delete the incoming lead.

 

For each of the lines in Bold I get the 'Initial Term of Field Expression must be concrete Sobject' error. I think this is because the platform is assuming that 

newLeads.get(leadB.email)...

could return more than one result. So I am thinking that I have to prove that 'newLeads.get(leadB.email)' will only return one result, but I am not sure how to do this.

 

public with sharing class leadHandler {

Public Static Void leadDeDuper(Map<String,String> newLeads) {
Set<String> leadsToDelete = new Set<String>();
List<Lead> leadsToDeleteList = new List<Lead>();
List<Lead> existingLeads = new List<Lead>();
List<Contact> contactsToDelete = new List<Contact>();
List<Contact> existingContacts = new List<Contact>();

//finds existing Leads where their email equals the incoming Lead
for(Lead leadB : [select Id, Name, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) {
//adds the incoming lead to a set that will be deleted further down
leadsToDelete.add(newLeads.get(leadB.email));

//updates fields in existing leads
leadB.HasOptedOutOfEmail = false;
leadB.Press_Releases__c = newLeads.get(leadB.email).Press_Releases__c;
leadB.Insights_Newsletter__c = newLeads.get(leadB.email).Insights_Newsletter__c;
leadB.Product_Announcements__c = newLeads.get(leadB.email).Product_Announcements__c;
leadB.Blog__c =newLeads.get(leadB.email).Blog__c;
leadB.Marketing_Opt_Out__c = false;

existingLeads.add(leadB);
}

if(existingLeads.size()>0) {
update existingLeads;
}
//finds existing contacts where their email equals the incoming leads
for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) {

//adds the lead to a set to be deleted below
leadsToDelete.add(newLeads.get(contactA.Email));

//updates the existing contact
contactA.HasOptedOutOfEmail = false;
contactA.Insights_Newsletter__c = newLeads.get(contactA.email).Insights_Newsletter__c;
contactA.Press_Releases__c = newLeads.get(contactA.email).Press_Releases__c;
contactA.Product_Announcements__c = newLeads.get(contactA.email).Product_Announcements__c;
contactA.Blog__c = newLeads.get(contactA.email).Blog__c;


existingContacts.add(contactA);
}

if(existingContacts.size()>0) {
update existingContacts;
}

if(leadsToDelete.size()>0) {
for(Lead dLead : [Select ID from Lead where ID IN:leadsToDelete]) {
leadsToDeleteList.add(dLead);
}
delete leadsToDeleteList;
}

}

}

 

 

The code below is my attempt at a lead dupe preventer. Rather than just prevent the insert of the lead I want to query the existing leads and contacts for records that have the same email and then update fields in those existing records. The class is receiving leads from an after update/insert trigger. The idea being to insert the lead then find if there are already any existing leads or contacts...update those records and then delete the lead that came from the trigger.

 

I tried to do this with one SOSL a little while ago, but have decided to go with 2 SOQL statements. Anyway, I am getting a strange save error when I try to save the code below: 'Save error: Set of SOBJECT:Lead not allowed'.  My first attempt at this was to make the variable "leadsToDelete" a List, but this attempt results in a duplicate id error in the delete statement in cases where the same lead is added to the list more than once, which is why I changed the varible to a Set but now I cant figure this error out.

 

Does anybody know of any reason for why I wouldn't be able to make a Set of Leads?

 

Thanks for any help.

 

 

 


public with sharing class leadHandler { Public Static Void leadDeDuper(Map<String,Lead> newLeads) { Set<Lead> leadsToDelete = new Set<Lead>(); List<Lead> existingLeads = new List<Lead>(); List<Contact> contactsToDelete = new List<Contact>(); List<Contact> existingContacts = new List<Contact>(); //finds existing Leads where their email equals the incoming Lead for(Lead leadB : [select Id, Name, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) { //adds the incoming lead to a set that will be deleted further down leadsToDelete.add(newLeads.get(leadB.email)); //updates fields in existing leads leadB.HasOptedOutOfEmail = false; leadB.Press_Releases__c = true; leadB.Insights_Newsletter__c = true; leadB.Product_Announcements__c = true; leadB.Blog__c =true; leadB.Marketing_Opt_Out__c = false; if(leadB.Insights_Newsletter__c == false || leadB.Press_Releases__c == false || leadB.HasOptedOutOfEmail == true){ existingLeads.add(leadB); } } if(existingLeads.size()>0) { update existingLeads; } //finds existing contacts where their email equals the incoming leads for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) { //adds the lead to a set to be deleted below leadsToDelete.add(newLeads.get(contactA.Email)); //updates the existing contact contactA.HasOptedOutOfEmail = false; contactA.Insights_Newsletter__c = True; contactA.Press_Releases__c = True; contactA.Product_Announcements__c = true; contactA.Blog__c = true; if(contactA.Insights_Newsletter__c == false || contactA.Press_Releases__c == false || contactA.HasOptedOutOfEmail == true) { existingContacts.add(contactA); } } if(existingContacts.size()>0) { update existingContacts; } if(leadsToDelete.size()>0) { delete leadsToDelete; } } }

 

 

 

After two years of writing Apex I thought I finally found a use for sosl, but it seams that it's not possible to search using a variable as I have become used to in SOQL. Can anybody tell me if the following is possible, or do I have to put the sosl query inside of a for loop so that I can get the string which makes up the keySet().

 

 

List<List<sObject>> leadsYContacts = [FIND :newLeadsMap.keySet() IN Email Fields Returning Lead,Contact];

 

The variable newLeadsMap is a Map<String,Lead>. The string being the Lead's email. I am trying to make a trigger that finds existing contacts and leads that have the same email as incoming leads.

 

I just wrote this, and it seems to work, although I haven't tested very rigorously yet.

I know there has to be a better way to do this, though...probably with maps, but I just don't see how to do it. Any suggestions would be appreciated.

 

  • oppties comes from a trigger(trying to code for bulk)
  • lineItemsWOSchedules was generated with a soql query that pulls all line items where the OpportunityId is IN :oppties and HasSchedule field is False.

It seems really inefficient to iterate through oppties and then iterate through lineItems to find the ones related to the oppty.

 

 

 

for(Opportunity oppty : oppties) { for(OpportunityLineItem ol : lineItemsWOSchedules) { if(ol.OpportunityId == oppty.Id) { if(null != olWSmallestDate.ServiceDate) { if(ol.ServiceDate < olWSmallestDate.ServiceDate) { olWSmallestDate = ol; } }else {olWSmallestDate = ol;} } } if(olWSmallestDate.ServiceDate.month() < System.today().month()) { oppty.adderror('The following line item has earnings scheduled before this month. Please edit the service date to reflect the earning schedule correctly. '+olWSmallestDate.PriceBookEntry.Name); } }

 

 

 

I am gettinga null pointer exception on this line. I ran system.asserts to make sure that both of them were returning values, and both of them are. So, does anybody know why I would be getting null pointer exception when I compare them?

 

 

if(olWSmallestDate.ServiceDate.month() < System.today().month()) {...

 

 

 

I need to validate that the contact chosen on for an Opportunity Contact Role belongs to the same Account that related Oppty belongs to. It seems that validation rules and triggers are not possible on Contact Roles. Is there anybody that can get me started?

 

Thanks.

I need to know if it is possible to use html in the header and footer of a letterhead template? The salesforce helpdesk confirmed that it was possible, but I have not managed to get it to work. Has anybody out there done this, or know which file types can be used in a letterhead template's header and footer?

 

thanks,

Andy

I have been given the task of creating a list of our Queues and the members that belong to them. After playing around in the schema browser it seems that this meta-data has enough exposure to be able to generate this list with a soql query. Is there anybody out there who has done this before, and could point me in the right direction.

 

Thanks,

Andy

I've an SOQL which queries an object and its associated Account & Contact values like this:

 

CustomObject__c co = [Select ID, Account__c, Contact__c, OwnerID From  CustomObject__c Where ID =: idvar];

 

Now, this works fine in my Development org. I can read the record in above query. But in user org I could not do so. Just so you know, I am "not" using "with sharing" keyword in my Apex Class. So automatic sharing does not apply to the SOQL results, right?

 

The only difference I can note in the 2 orgs is: User has Record Types created. DO anyone know if RecordType can effect user read permissions?

 

I've verified "Sharing Settings" too, the values are identical in both orgs, and also user is using same profile settings and Same Group as I'm using in my Dev org.

 

 

Please help, this is really bugging me out, I don't know what is making the record invisible in soql ... :(

  • September 23, 2010
  • Like
  • 0

I have written a trigger that calls a class, and a test class for them. When I run the test in the ide 'apex test runner' everything seems to be fine. I get 100% coverage in my class and trigger, and no warnings or errors. When I try to deploy the code to my production org I get a null pointer exception on the insert line that is highlighted below.

 

The trigger and class are posted below too.

 

Thanks!

 

@isTest
private class LeadHandlerTest {

static testMethod void testLeadHandler() {

List<Lead> testLeads = new List<Lead>();

Contact contact = new Contact(NPD_Client_Satisfaction_Survey__c = 'Yes',
LastName = 'Test',
JobFunction__c =Admin',
Email = 'test@npd.com',
ContactType__c = 'NPDCustomer');
insert contact;

Lead lead = new Lead(Phone='1234567890',
LeadSource='Advertising',
LastName='TestJohnson',
FirstName='Test',
LeadBU__c='Automotive',
Company='Test Company',
CurrencyIsoCode='USD',
Lead_Type__c='In-Person Meeting',
Status='New Leads - Not yet working',
Rating='Z - Not Contacted - Unknown Quality',
Email='test@npd.com',
MarketingCampaign__c = 'Web-Marketing-Opt In Web Page-200807'
);
testLeads.add(lead);

Lead lead2 = new Lead(Phone='1234567890',
LeadSource='Advertising',
LastName='TestJohnson2',
FirstName='Test2',
LeadBU__c='Beauty',
Company='Test Company',
CurrencyIsoCode='CAD',
Lead_Type__c='In-Person Meeting',
Status='New Leads - Not yet working',
Rating='Z - Not Contacted - Unknown Quality',
Email='test@npd.com'
);
testLeads.add(lead2);

insert testLeads;
}
}

Trigger:

 

trigger leadAfterInsertUpdate on Lead (after insert, after update) {

if(UserInfo.getUserId() <> '00500000006z3qKAAQ'){

Map<String,String> newLeadsMap = new Map<String,String>();

for(Lead newLead : Trigger.new) {
if((null != newLead.Email) && (newLead.MarketingCampaign__c == 'Web-Marketing-Opt In Web Page-200807') && (Trigger.isInsert || newLead.Email != Trigger.oldMap.get(newLead.Id).Email)) {
if(newLeadsMap.containsKey(newLead.Email)) {
newLead.Email.addError('Another new lead is already using this email address.');
}else {
newLeadsMap.put(newLead.Email, newLead.ID);
}
}
}

leadHandler.leadDeDuper(newLeadsMap);
}

}

 Class

 

 

public with sharing class leadHandler {
	
    Public Static Void leadDeDuper(Map<String,String> newLeads) {
        Set<String> leadsToDelete = new Set<String>();
        Map<id,Lead> newLeadMap = new Map <ID, Lead>([Select Id, FirstName, LastName, Phone, PrimaryAreaOfInterest__c, Blog__c, Email, HasOptedOutOfEmail,Press_Releases__c,Insights_Newsletter__c,Product_Announcements__c,Marketing_Opt_Out__c from Lead Where ID IN: newLeads.values()]);
        List<Lead> leadsToDeleteList = new List<Lead>();
        List<Lead> existingLeads = new List<Lead>();
        List<Contact> contactsToDelete = new List<Contact>();
        List<Contact> existingContacts = new List<Contact>();
        
        //finds existing Leads where their email equals the incoming Lead
        for(Lead leadB : [select Id, FirstName, LastName,PrimaryAreaofInterest__c,Phone, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) {
            //adds the incoming lead to a set that will be deleted further down
            leadsToDelete.add(newLeads.get(leadB.email));   
                    
            //updates fields in existing leads
            leadB.HasOptedOutOfEmail = false;
            leadB.Press_Releases__c = newLeadMap.get(newLeads.get(leadB.email)).Press_Releases__c;
            leadB.Insights_Newsletter__c = newLeadMap.get(newLeads.get(leadB.email)).Insights_Newsletter__c;
            leadB.Product_Announcements__c = newLeadMap.get(newLeads.get(leadB.email)).Product_Announcements__c;
            leadB.Blog__c =newLeadMap.get(newLeads.get(leadB.email)).Blog__c;
            leadB.Marketing_Opt_Out__c = false;
            leadB.FirstName = newLeadMap.get(newLeads.get(leadB.email)).FirstName;
            leadB.LastName = newLeadMap.get(newLeads.get(leadB.email)).LastName;
            leadB.PrimaryAreaOfInterest__c = newLeadMap.get(newLeads.get(leadB.email)).PrimaryAreaOfInterest__c;
            leadB.Phone = newLeadMap.get(newLeads.get(leadB.email)).Phone;
           
            existingLeads.add(leadB);
        }
        
        if(existingLeads.size()>0) {
            update existingLeads;
        }
        //finds existing contacts where their email equals the incoming leads
        for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) {
            
            //adds the lead to a set to be deleted below
            leadsToDelete.add(newLeads.get(contactA.Email));
            
            //updates the existing contact          
            contactA.HasOptedOutOfEmail = false;
            contactA.Insights_Newsletter__c = newLeadMap.get(newLeads.get(contactA.email)).Insights_Newsletter__c;
            contactA.Press_Releases__c = newLeadMap.get(newLeads.get(contactA.email)).Press_Releases__c;
            contactA.Product_Announcements__c = newLeadMap.get(newLeads.get(contactA.email)).Product_Announcements__c;
            contactA.Blog__c = newLeadMap.get(newLeads.get(contactA.email)).Blog__c;
            contactA.FirstName = newLeadMap.get(newLeads.get(contactA.email)).FirstName;
            contactA.LastName = newLeadMap.get(newLeads.get(contactA.email)).LastName;
            contactA.Phone = newLeadMap.get(newLeads.get(contactA.email)).Phone;
            
            existingContacts.add(contactA);
        }
       
        if(existingContacts.size()>0) {
            update existingContacts;
        }
        
        if(leadsToDelete.size()>0) {
        	for(Lead dLead : [Select ID from Lead where ID IN:leadsToDelete]) {
        		leadsToDeleteList.add(dLead);
        	}
            delete leadsToDeleteList;
        }
        
    }

}

 

 

 

 

 

I have made a trigger that intercepts leads that are created by our web-to-lead form. The trigger ensures that there are no duplicate leads based on email, (which is probably an unnecessary safety step because I don't there is a way for two leads to be submitted by one web-form). It then packages those unique leads into a map and sends it over to the class I have pasted below. The goal of the class is to find existing leads and contacts and update them with the data supplied by the incoming lead, and then delete the incoming lead.

 

For each of the lines in Bold I get the 'Initial Term of Field Expression must be concrete Sobject' error. I think this is because the platform is assuming that 

newLeads.get(leadB.email)...

could return more than one result. So I am thinking that I have to prove that 'newLeads.get(leadB.email)' will only return one result, but I am not sure how to do this.

 

public with sharing class leadHandler {

Public Static Void leadDeDuper(Map<String,String> newLeads) {
Set<String> leadsToDelete = new Set<String>();
List<Lead> leadsToDeleteList = new List<Lead>();
List<Lead> existingLeads = new List<Lead>();
List<Contact> contactsToDelete = new List<Contact>();
List<Contact> existingContacts = new List<Contact>();

//finds existing Leads where their email equals the incoming Lead
for(Lead leadB : [select Id, Name, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) {
//adds the incoming lead to a set that will be deleted further down
leadsToDelete.add(newLeads.get(leadB.email));

//updates fields in existing leads
leadB.HasOptedOutOfEmail = false;
leadB.Press_Releases__c = newLeads.get(leadB.email).Press_Releases__c;
leadB.Insights_Newsletter__c = newLeads.get(leadB.email).Insights_Newsletter__c;
leadB.Product_Announcements__c = newLeads.get(leadB.email).Product_Announcements__c;
leadB.Blog__c =newLeads.get(leadB.email).Blog__c;
leadB.Marketing_Opt_Out__c = false;

existingLeads.add(leadB);
}

if(existingLeads.size()>0) {
update existingLeads;
}
//finds existing contacts where their email equals the incoming leads
for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) {

//adds the lead to a set to be deleted below
leadsToDelete.add(newLeads.get(contactA.Email));

//updates the existing contact
contactA.HasOptedOutOfEmail = false;
contactA.Insights_Newsletter__c = newLeads.get(contactA.email).Insights_Newsletter__c;
contactA.Press_Releases__c = newLeads.get(contactA.email).Press_Releases__c;
contactA.Product_Announcements__c = newLeads.get(contactA.email).Product_Announcements__c;
contactA.Blog__c = newLeads.get(contactA.email).Blog__c;


existingContacts.add(contactA);
}

if(existingContacts.size()>0) {
update existingContacts;
}

if(leadsToDelete.size()>0) {
for(Lead dLead : [Select ID from Lead where ID IN:leadsToDelete]) {
leadsToDeleteList.add(dLead);
}
delete leadsToDeleteList;
}

}

}

 

 

The code below is my attempt at a lead dupe preventer. Rather than just prevent the insert of the lead I want to query the existing leads and contacts for records that have the same email and then update fields in those existing records. The class is receiving leads from an after update/insert trigger. The idea being to insert the lead then find if there are already any existing leads or contacts...update those records and then delete the lead that came from the trigger.

 

I tried to do this with one SOSL a little while ago, but have decided to go with 2 SOQL statements. Anyway, I am getting a strange save error when I try to save the code below: 'Save error: Set of SOBJECT:Lead not allowed'.  My first attempt at this was to make the variable "leadsToDelete" a List, but this attempt results in a duplicate id error in the delete statement in cases where the same lead is added to the list more than once, which is why I changed the varible to a Set but now I cant figure this error out.

 

Does anybody know of any reason for why I wouldn't be able to make a Set of Leads?

 

Thanks for any help.

 

 

 


public with sharing class leadHandler { Public Static Void leadDeDuper(Map<String,Lead> newLeads) { Set<Lead> leadsToDelete = new Set<Lead>(); List<Lead> existingLeads = new List<Lead>(); List<Contact> contactsToDelete = new List<Contact>(); List<Contact> existingContacts = new List<Contact>(); //finds existing Leads where their email equals the incoming Lead for(Lead leadB : [select Id, Name, Blog__c,Email,HasOptedOutofEmail,Product_Announcements__c, MarketingCampaign__c, Insights_Newsletter__c,Press_Releases__c from Lead where Email IN :newLeads.KeySet()]) { //adds the incoming lead to a set that will be deleted further down leadsToDelete.add(newLeads.get(leadB.email)); //updates fields in existing leads leadB.HasOptedOutOfEmail = false; leadB.Press_Releases__c = true; leadB.Insights_Newsletter__c = true; leadB.Product_Announcements__c = true; leadB.Blog__c =true; leadB.Marketing_Opt_Out__c = false; if(leadB.Insights_Newsletter__c == false || leadB.Press_Releases__c == false || leadB.HasOptedOutOfEmail == true){ existingLeads.add(leadB); } } if(existingLeads.size()>0) { update existingLeads; } //finds existing contacts where their email equals the incoming leads for(Contact contactA : [select ID, Name, Email, HasOptedOutofEmail, Insights_Newsletter__c, Press_Releases__c from Contact where Email IN :newLeads.keySet()]) { //adds the lead to a set to be deleted below leadsToDelete.add(newLeads.get(contactA.Email)); //updates the existing contact contactA.HasOptedOutOfEmail = false; contactA.Insights_Newsletter__c = True; contactA.Press_Releases__c = True; contactA.Product_Announcements__c = true; contactA.Blog__c = true; if(contactA.Insights_Newsletter__c == false || contactA.Press_Releases__c == false || contactA.HasOptedOutOfEmail == true) { existingContacts.add(contactA); } } if(existingContacts.size()>0) { update existingContacts; } if(leadsToDelete.size()>0) { delete leadsToDelete; } } }

 

 

 

After two years of writing Apex I thought I finally found a use for sosl, but it seams that it's not possible to search using a variable as I have become used to in SOQL. Can anybody tell me if the following is possible, or do I have to put the sosl query inside of a for loop so that I can get the string which makes up the keySet().

 

 

List<List<sObject>> leadsYContacts = [FIND :newLeadsMap.keySet() IN Email Fields Returning Lead,Contact];

 

The variable newLeadsMap is a Map<String,Lead>. The string being the Lead's email. I am trying to make a trigger that finds existing contacts and leads that have the same email as incoming leads.

 

I just wrote this, and it seems to work, although I haven't tested very rigorously yet.

I know there has to be a better way to do this, though...probably with maps, but I just don't see how to do it. Any suggestions would be appreciated.

 

  • oppties comes from a trigger(trying to code for bulk)
  • lineItemsWOSchedules was generated with a soql query that pulls all line items where the OpportunityId is IN :oppties and HasSchedule field is False.

It seems really inefficient to iterate through oppties and then iterate through lineItems to find the ones related to the oppty.

 

 

 

for(Opportunity oppty : oppties) { for(OpportunityLineItem ol : lineItemsWOSchedules) { if(ol.OpportunityId == oppty.Id) { if(null != olWSmallestDate.ServiceDate) { if(ol.ServiceDate < olWSmallestDate.ServiceDate) { olWSmallestDate = ol; } }else {olWSmallestDate = ol;} } } if(olWSmallestDate.ServiceDate.month() < System.today().month()) { oppty.adderror('The following line item has earnings scheduled before this month. Please edit the service date to reflect the earning schedule correctly. '+olWSmallestDate.PriceBookEntry.Name); } }

 

 

 

I am gettinga null pointer exception on this line. I ran system.asserts to make sure that both of them were returning values, and both of them are. So, does anybody know why I would be getting null pointer exception when I compare them?

 

 

if(olWSmallestDate.ServiceDate.month() < System.today().month()) {...

 

 

 

I have been given the task of creating a list of our Queues and the members that belong to them. After playing around in the schema browser it seems that this meta-data has enough exposure to be able to generate this list with a soql query. Is there anybody out there who has done this before, and could point me in the right direction.

 

Thanks,

Andy