-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
2Questions
-
4Replies
UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record
Please someone help on how to resolve this error. What's the best practice on handling it? Appreciate response. Thanks
------------
Apex script unhandled trigger exception by user/organization: 005U0000000NmC0/00DU0000000KA1N
trac_Attachment: execution of AfterInsert
caused by: System.DmlException: Update failed. First exception on row 0 with id 00QU0000005c15jMAA; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []
Trigger.trac_Attachment: line 112, column 1
trigger trac_Attachment on Attachment (before delete, after insert, after update) {
String BASE_URL = Url.getSalesforceBaseUrl().toExternalForm() + '/servlet/servlet.FileDownload?file=';
String OWNER1 = 'Owner1_Sign.png';
String OWNER2 = 'Owner2_Sign.png';
String PG1 = 'Owner1_PG_Sign.png';
String PG2 = 'Owner2_PG_Sign.png';
Map<Id,Lead> leadMap = new Map<Id,Lead>();
Set<Id> touchedLeadIds = new Set<Id>();
for (Attachment lp : trigger.isdelete ? trigger.old : trigger.new) {
touchedLeadIds.add(lp.parentId);
}
leadMap = new Map<Id,Lead>([SELECT IsConverted, DirtyFlag__c, Conga_Owner1_PG_Sign_URL__c, Conga_Owner2_PG_Sign_URL__c,
Conga_Owner1_Sign_URL__c, Conga_Owner2_Sign_URL__c
FROM Lead
WHERE Id IN :touchedLeadIds]);
for(Lead i : leadMap.values()){
if(i.IsConverted == false && i.DirtyFlag__c == true) i.DirtyFlag__c = false;
else if(i.IsConverted == false) i.DirtyFlag__c = true;
}
// find relevant opp ids
Set<Id> oppIds = new Set<Id>();
Set<Id> leadIds = new Set<Id>();
for(Attachment a : trigger.isdelete ? trigger.old : trigger.new) {
if (a.name == OWNER1 || a.name == OWNER2 || a.name == PG1 || a.name == PG2) {
if (((String)a.parentId).startsWith('006')) {
oppIds.add(a.parentId);
} else if (((String)a.parentId).startsWith('00Q')) {
leadIds.add(a.parentId);
}
}
}
if ((oppIds.size() + leadIds.size()) > 0) {
// get opps and leads
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Conga_Owner1_PG_Sign_URL__c, Conga_Owner2_PG_Sign_URL__c,
Conga_Owner1_Sign_URL__c, Conga_Owner2_Sign_URL__c
FROM Opportunity
WHERE Id IN :oppIds]);
Boolean isLead;
for(Attachment a : trigger.isdelete ? trigger.old : trigger.new) {
if (((String)a.parentId).startsWith('00Q') || ((String)a.parentId).startsWith('006')) {
isLead = ((String)a.parentId).startsWith('00Q');
system.debug('isLead is: ' + isLead + ', a.name is: ' + a.name);
} // end parentid if
} // end for
update oppMap.values();
}
update leadMap.values();
}
-
- willj
- May 19, 2012
- Like
- 0
Too many SOL queries even with no queries in loop - please help
Getting too many soql queries 101 even though no queries in loop. how do I overcome this? very frustrating.
code is long so divided into two posts.
trigger trac_Lead on Lead (before insert, before update, after insert, after update) {
/*****************
* Before Insert *
*****************/
if(trigger.isInsert && trigger.isBefore) {
// Perform SIC mappings
for(Lead l : trigger.new) {
if (l.sic_lookup__c != null) {
l.Industry = l.sic_description__c;
}
}
// Query campaigns and rates
List<Campaign> campaignList = new List<Campaign>();
for(Campaign i : DBHelper.getCampaignListQuery()){
if(i.IsActive==true){
campaignList.add(i);
}
}
List<User> userList = DBHelper.getUserDetailQuery();
for (Lead l : trigger.new) {
for(User u : userList){
if(u.Id == UserInfo.getUserId()){
if(u.Account_ID__c != null) {
for(Account a : DBHelper.getAccountQuery()){ //check if accountid on user detail is valid
if(a.Id == u.Account_ID__c){
l.Payment_Advisor__c = u.Account_ID__c;
}
}
}
}
}
// Inject default rates to app from campaign
if (l.Campaign__c != null && l.Product_Group__c != null && l.Processor__c != null) {
Campaign matchingCampaign;
for(Campaign c : campaignList){
if(c.Id==l.Campaign__c) {
matchingCampaign = c;
break;
}
}
Id selectedRate;
if(l.Processor__c == 'FD') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c;
}
else if(l.Processor__c == 'Chase') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c;
}
else if(l.Processor__c == 'PsiGate') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c;
}
for(Rate__c r : DBHelper.getRateListQuery()){
if(r.Id==selectedRate) {
l.Account_Setup_Fee__c = r.Account_Setup_Fee__c;
l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c;
l.Annual_Fee__c = r.Annual_Fee__c;
l.Chargeback__c = r.Chargeback_del__c;
l.Cross_Border_Fee__c = r.Cross_Border_Fee__c;
l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c;
l.Discover_Rate__c = r.Discover_Rate__c;
l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c;
l.Interac_Fee__c = r.Interac_Fee__c;
l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c;
l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c;
l.MasterCard_Rate__c = r.MasterCard_Rate__c;
l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c;
l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c;
l.Monthly_Minimums__c = r.Monthly_Minimums__c;
l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c;
l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c;
l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c;
l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c;
l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c;
l.Visa_Rate__c = r.VISA_Rate__c;
l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c;
l.Voice_Authorization__c = r.Voice_Authorization__c;
l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c;
break;
}
}
}
}
}
/****************
* After Insert *
****************/
if(trigger.isInsert && trigger.isAfter) {
// add campaign mappings
List<CampaignMember> newCMs = new List<CampaignMember>();
List<Web_Profile__c> newWPs = new List<Web_Profile__c>();
for (Lead l : trigger.new) {
if(UserInfo.getUserId() != '005U0000000NmC0'){
newWPs.add(new Web_Profile__c(Applicant__c = l.id, Email__c = l.Email));
}
newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c));
}
insert newCMs;
try {
insert newWPs;
} catch (DmlException de) {
for (Lead l : trigger.new) {
l.addError('Duplicate Email with an existing lead.');
}
}
}
/*****************
* Before Update *
*****************/
if(trigger.isUpdate && trigger.isBefore) {
Set<Id> leadIds = new Set<Id>();
for (Lead lp : trigger.new) {
leadIds.add(lp.id);
}
List<Lead_Product__c> leadProductQuery = null;
leadProductQuery = [SELECT lead__c, product__r.isActive,
Card_Types__c,
Doing_Business_As_DBA__c,
Quantity__c,
Term__c,
Product__c,
product__r.purchase_price__c,
product__r.rental_price__c,
product__r.lease_price__c,
Batch_Processing__c,
Customer_Profile_Management__c,
Recurring_Billing__c,
Shopping_Cart_Option__c,
Virtual_Terminal__c,
Auto_Close_Hour__c,
Auto_Close_Minute__c,
Auto_Close__c,
Cash_Back_Enabled__c,
Invoice_Number__c,
QSR_Flag__c,
Server_Clerk_ID__c,
Tip_Option__c,
Connection_Type__c,
DNS1__c,
DNS2__c,
Gateway_Address__c,
IP_Address__c,
Request_Type__c,
Service_Option__c,
Special_Instructions__c,
Subnet_Mask__c,
Add_to_Existing_NetConnect__c,
Application_Type__c,
Existing_NetConnect_ID__c,
Software_Name_Version__c,
TID_s__c,
VAR_Comm_Method__c,
VAR_Name__c,
Rental_Cost__c,
Rental_Price__c,
Lease_Price__c,
Purchase_Price__c,
Buy_Rate__c
FROM Lead_Product__c WHERE lead__c IN :leadIds];
Id LEAD_RT_UNCLAIMED;
Id LEAD_RT_MANUAL;
for(RecordType rt : DBHelper.getRecordTypes()) {
if(rt.SobjectType=='Lead' && rt.name=='Unclaimed Lead'){
LEAD_RT_UNCLAIMED = rt.id;
}
if(rt.SobjectType=='Lead' && rt.name=='Manually Entered'){
LEAD_RT_MANUAL = rt.id;
}
}
List<CampaignMember> newCMs = new List<CampaignMember>();
Set<String> delCMKeys = new Set<String>();
List<Lead_Product__c> lps = new List<Lead_Product__c>();
for(Lead_Product__c i : leadProductQuery){
if(leadIds.contains((Id)i.lead__c)){
lps.add(i);
}
}
// check that lead has products
Set<Id> leadsWithProducts = new Set<Id>();
for(Lead_Product__c i : lps) {
if(!leadsWithProducts.contains((Id)i.lead__c)){
leadsWithProducts.add((Id)i.lead__c);
}
}
// check for lead products that have inactive products
Set<Id> leadsWithInactiveProducts = new Set<Id>();
for(Lead_Product__c i : lps) {
if(!leadsWithInactiveProducts.contains((Id)i.lead__c) && i.product__r.isActive == false){
leadsWithInactiveProducts.add((Id)i.lead__c);
}
}
// Query campaigns and rates
List<Campaign> campaignList = new List<Campaign>();
for(Campaign i : DBHelper.getCampaignListQuery()){
if(i.IsActive==true){
campaignList.add(i);
}
}
for (Lead l : trigger.new) {
// assign record type based on whether owner is queue or a user
if(((String)l.ownerid).startsWith('00G')) { // queue
l.recordtypeid = LEAD_RT_UNCLAIMED;
} else if (l.recordtypeid == LEAD_RT_UNCLAIMED){ // user, needs RT update
l.recordtypeid = LEAD_RT_MANUAL;
}
// lead is about to be converted
if (l.isConverted && !trigger.oldMap.get(l.id).isConverted) {
if(!leadsWithProducts.contains(l.id)) {
l.addError(system.label.noLeadProducts);
} else if(leadsWithInactiveProducts.contains(l.id)) {
l.addError(system.label.leadProductWithInactiveProduct);
}
l.status_date_review_and_sign__c = Datetime.now();
}
if (l.sic_lookup__c != null) {
l.Industry = l.sic_description__c;
}
// Find leads where the campaign has been changed
if(l.campaign__c != trigger.oldMap.get(l.id).campaign__c) {
newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c));
if (trigger.oldmap.get(l.id).campaign__c != null) {
delCMKeys.add(((String)trigger.oldmap.get(l.id).campaign__c).substring(0,15) + ((String)l.id).substring(0,15));
}
}
// Update rates if campaign, processor, or product group changed
Lead oldLead = Trigger.oldMap.get(l.ID);
if (l.Campaign__c != oldLead.Campaign__c || l.Product_Group__c != oldLead.Product_Group__c || l.Processor__c != oldLead.Processor__c) {
Campaign matchingCampaign;
for(Campaign c : campaignList){
if(c.Id==l.Campaign__c) {
matchingCampaign = c;
break;
}
}
Id selectedRate;
if(l.Processor__c == 'FD') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c;
}
else if(l.Processor__c == 'Chase') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c;
}
else if(l.Processor__c == 'PsiGate') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c;
}
for(Rate__c r : DBHelper.getRateListQuery()){
if(r.Id==selectedRate) {
l.Account_Setup_Fee__c = r.Account_Setup_Fee__c;
l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c;
l.Annual_Fee__c = r.Annual_Fee__c;
l.Chargeback__c = r.Chargeback_del__c;
l.Cross_Border_Fee__c = r.Cross_Border_Fee__c;
l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c;
l.Discover_Rate__c = r.Discover_Rate__c;
l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c;
l.Interac_Fee__c = r.Interac_Fee__c;
l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c;
l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c;
l.MasterCard_Rate__c = r.MasterCard_Rate__c;
l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c;
l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c;
l.Monthly_Minimums__c = r.Monthly_Minimums__c;
l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c;
l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c;
l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c;
l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c;
l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c;
l.Visa_Rate__c = r.VISA_Rate__c;
l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c;
l.Voice_Authorization__c = r.Voice_Authorization__c;
l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c;
break;
}
}
}
}
// add new campaign members
try {
insert newCMs;
} catch (DmlException de) {
// existing member...
}
// delete old campaign members
List<CampaignMember> oldCMs = [SELECT id FROM CampaignMember WHERE key__c IN :delCMKeys];
if (oldCMs.size() > 0) {
delete oldCMs;
}
}
-
- willj
- May 18, 2012
- Like
- 0
Batchable instance is too big
Hi! I'm looking for some info about "Batchable instance is too big" exception and I couldn't find nothing... Anyone have a clue about why this exception is triggered?
Thanks a lot!
- Juan Spagnoli
- October 31, 2012
- Like
- 0
Too many SOL queries even with no queries in loop - please help
Getting too many soql queries 101 even though no queries in loop. how do I overcome this? very frustrating.
code is long so divided into two posts.
trigger trac_Lead on Lead (before insert, before update, after insert, after update) {
/*****************
* Before Insert *
*****************/
if(trigger.isInsert && trigger.isBefore) {
// Perform SIC mappings
for(Lead l : trigger.new) {
if (l.sic_lookup__c != null) {
l.Industry = l.sic_description__c;
}
}
// Query campaigns and rates
List<Campaign> campaignList = new List<Campaign>();
for(Campaign i : DBHelper.getCampaignListQuery()){
if(i.IsActive==true){
campaignList.add(i);
}
}
List<User> userList = DBHelper.getUserDetailQuery();
for (Lead l : trigger.new) {
for(User u : userList){
if(u.Id == UserInfo.getUserId()){
if(u.Account_ID__c != null) {
for(Account a : DBHelper.getAccountQuery()){ //check if accountid on user detail is valid
if(a.Id == u.Account_ID__c){
l.Payment_Advisor__c = u.Account_ID__c;
}
}
}
}
}
// Inject default rates to app from campaign
if (l.Campaign__c != null && l.Product_Group__c != null && l.Processor__c != null) {
Campaign matchingCampaign;
for(Campaign c : campaignList){
if(c.Id==l.Campaign__c) {
matchingCampaign = c;
break;
}
}
Id selectedRate;
if(l.Processor__c == 'FD') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c;
}
else if(l.Processor__c == 'Chase') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c;
}
else if(l.Processor__c == 'PsiGate') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c;
}
for(Rate__c r : DBHelper.getRateListQuery()){
if(r.Id==selectedRate) {
l.Account_Setup_Fee__c = r.Account_Setup_Fee__c;
l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c;
l.Annual_Fee__c = r.Annual_Fee__c;
l.Chargeback__c = r.Chargeback_del__c;
l.Cross_Border_Fee__c = r.Cross_Border_Fee__c;
l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c;
l.Discover_Rate__c = r.Discover_Rate__c;
l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c;
l.Interac_Fee__c = r.Interac_Fee__c;
l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c;
l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c;
l.MasterCard_Rate__c = r.MasterCard_Rate__c;
l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c;
l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c;
l.Monthly_Minimums__c = r.Monthly_Minimums__c;
l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c;
l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c;
l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c;
l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c;
l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c;
l.Visa_Rate__c = r.VISA_Rate__c;
l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c;
l.Voice_Authorization__c = r.Voice_Authorization__c;
l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c;
break;
}
}
}
}
}
/****************
* After Insert *
****************/
if(trigger.isInsert && trigger.isAfter) {
// add campaign mappings
List<CampaignMember> newCMs = new List<CampaignMember>();
List<Web_Profile__c> newWPs = new List<Web_Profile__c>();
for (Lead l : trigger.new) {
if(UserInfo.getUserId() != '005U0000000NmC0'){
newWPs.add(new Web_Profile__c(Applicant__c = l.id, Email__c = l.Email));
}
newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c));
}
insert newCMs;
try {
insert newWPs;
} catch (DmlException de) {
for (Lead l : trigger.new) {
l.addError('Duplicate Email with an existing lead.');
}
}
}
/*****************
* Before Update *
*****************/
if(trigger.isUpdate && trigger.isBefore) {
Set<Id> leadIds = new Set<Id>();
for (Lead lp : trigger.new) {
leadIds.add(lp.id);
}
List<Lead_Product__c> leadProductQuery = null;
leadProductQuery = [SELECT lead__c, product__r.isActive,
Card_Types__c,
Doing_Business_As_DBA__c,
Quantity__c,
Term__c,
Product__c,
product__r.purchase_price__c,
product__r.rental_price__c,
product__r.lease_price__c,
Batch_Processing__c,
Customer_Profile_Management__c,
Recurring_Billing__c,
Shopping_Cart_Option__c,
Virtual_Terminal__c,
Auto_Close_Hour__c,
Auto_Close_Minute__c,
Auto_Close__c,
Cash_Back_Enabled__c,
Invoice_Number__c,
QSR_Flag__c,
Server_Clerk_ID__c,
Tip_Option__c,
Connection_Type__c,
DNS1__c,
DNS2__c,
Gateway_Address__c,
IP_Address__c,
Request_Type__c,
Service_Option__c,
Special_Instructions__c,
Subnet_Mask__c,
Add_to_Existing_NetConnect__c,
Application_Type__c,
Existing_NetConnect_ID__c,
Software_Name_Version__c,
TID_s__c,
VAR_Comm_Method__c,
VAR_Name__c,
Rental_Cost__c,
Rental_Price__c,
Lease_Price__c,
Purchase_Price__c,
Buy_Rate__c
FROM Lead_Product__c WHERE lead__c IN :leadIds];
Id LEAD_RT_UNCLAIMED;
Id LEAD_RT_MANUAL;
for(RecordType rt : DBHelper.getRecordTypes()) {
if(rt.SobjectType=='Lead' && rt.name=='Unclaimed Lead'){
LEAD_RT_UNCLAIMED = rt.id;
}
if(rt.SobjectType=='Lead' && rt.name=='Manually Entered'){
LEAD_RT_MANUAL = rt.id;
}
}
List<CampaignMember> newCMs = new List<CampaignMember>();
Set<String> delCMKeys = new Set<String>();
List<Lead_Product__c> lps = new List<Lead_Product__c>();
for(Lead_Product__c i : leadProductQuery){
if(leadIds.contains((Id)i.lead__c)){
lps.add(i);
}
}
// check that lead has products
Set<Id> leadsWithProducts = new Set<Id>();
for(Lead_Product__c i : lps) {
if(!leadsWithProducts.contains((Id)i.lead__c)){
leadsWithProducts.add((Id)i.lead__c);
}
}
// check for lead products that have inactive products
Set<Id> leadsWithInactiveProducts = new Set<Id>();
for(Lead_Product__c i : lps) {
if(!leadsWithInactiveProducts.contains((Id)i.lead__c) && i.product__r.isActive == false){
leadsWithInactiveProducts.add((Id)i.lead__c);
}
}
// Query campaigns and rates
List<Campaign> campaignList = new List<Campaign>();
for(Campaign i : DBHelper.getCampaignListQuery()){
if(i.IsActive==true){
campaignList.add(i);
}
}
for (Lead l : trigger.new) {
// assign record type based on whether owner is queue or a user
if(((String)l.ownerid).startsWith('00G')) { // queue
l.recordtypeid = LEAD_RT_UNCLAIMED;
} else if (l.recordtypeid == LEAD_RT_UNCLAIMED){ // user, needs RT update
l.recordtypeid = LEAD_RT_MANUAL;
}
// lead is about to be converted
if (l.isConverted && !trigger.oldMap.get(l.id).isConverted) {
if(!leadsWithProducts.contains(l.id)) {
l.addError(system.label.noLeadProducts);
} else if(leadsWithInactiveProducts.contains(l.id)) {
l.addError(system.label.leadProductWithInactiveProduct);
}
l.status_date_review_and_sign__c = Datetime.now();
}
if (l.sic_lookup__c != null) {
l.Industry = l.sic_description__c;
}
// Find leads where the campaign has been changed
if(l.campaign__c != trigger.oldMap.get(l.id).campaign__c) {
newCMs.add(new CampaignMember(leadid = l.id, campaignid = l.campaign__c));
if (trigger.oldmap.get(l.id).campaign__c != null) {
delCMKeys.add(((String)trigger.oldmap.get(l.id).campaign__c).substring(0,15) + ((String)l.id).substring(0,15));
}
}
// Update rates if campaign, processor, or product group changed
Lead oldLead = Trigger.oldMap.get(l.ID);
if (l.Campaign__c != oldLead.Campaign__c || l.Product_Group__c != oldLead.Product_Group__c || l.Processor__c != oldLead.Processor__c) {
Campaign matchingCampaign;
for(Campaign c : campaignList){
if(c.Id==l.Campaign__c) {
matchingCampaign = c;
break;
}
}
Id selectedRate;
if(l.Processor__c == 'FD') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.FD_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.FD_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.FD_Online_Rate__c;
}
else if(l.Processor__c == 'Chase') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.Online_Rate__c;
}
else if(l.Processor__c == 'PsiGate') {
if(l.Product_Group__c == 'Mobile') selectedRate = matchingCampaign.PsiGate_Mobile_Rate__c;
else if(l.Product_Group__c == 'In-Store') selectedRate = matchingCampaign.PsiGate_Instore_Rate__c;
else if(l.Product_Group__c == 'Online') selectedRate = matchingCampaign.PsiGate_Online_Rate__c;
}
for(Rate__c r : DBHelper.getRateListQuery()){
if(r.Id==selectedRate) {
l.Account_Setup_Fee__c = r.Account_Setup_Fee__c;
l.Amex_JCB_Fee_del__c = r.Amex_JCB_Fee_del__c;
l.Annual_Fee__c = r.Annual_Fee__c;
l.Chargeback__c = r.Chargeback_del__c;
l.Cross_Border_Fee__c = r.Cross_Border_Fee__c;
l.Deconversion_Cancellation_Fee_per_loc__c = r.Deconversion_Cancellation_Fee_per_loc__c;
l.Discover_Rate__c = r.Discover_Rate__c;
l.Discover_Rate_Amount__c = r.Discover_Transaction_Fee__c;
l.Interac_Fee__c = r.Interac_Fee__c;
l.MC_VISA_Authorization__c = r.MC_VISA_Authorization__c;
l.MasterCard_Cross_Border_Transaction_Fee__c = r.MasterCard_Cross_Border_Transaction_Fee__c;
l.MasterCard_Rate__c = r.MasterCard_Rate__c;
l.MasterCard_Rate_Amount__c = r.MasterCard_Transaction_Fee__c;
l.Monthly_Account_Fee__c = r.Monthly_Account_Fee__c;
l.Monthly_Minimums__c = r.Monthly_Minimums__c;
l.Non_Qualifying_Surcharge_Fee__c = r.Non_Qualifying_Surcharge_Fee__c;
l.Other_Monthly_Fees__c = r.Other_Monthly_Fees__c;
l.VISA_Debit_Rate__c = r.VISA_Debit_Rate__c;
l.VISA_Debit_Rate_Amount__c = r.VISA_Debit_Transaction_Fee__c;
l.VISA_MasterCard_Card_Brand_Fee__c = r.VISA_MasterCard_Card_Brand_Fee__c;
l.Visa_Rate__c = r.VISA_Rate__c;
l.VISA_Rate_Amount__c = r.VISA_Transaction_Fee__c;
l.Voice_Authorization__c = r.Voice_Authorization__c;
l.Wireless_Terminal_Setup_Fee__c = r.Wireless_Terminal_Setup_Fee__c;
break;
}
}
}
}
// add new campaign members
try {
insert newCMs;
} catch (DmlException de) {
// existing member...
}
// delete old campaign members
List<CampaignMember> oldCMs = [SELECT id FROM CampaignMember WHERE key__c IN :delCMKeys];
if (oldCMs.size() > 0) {
delete oldCMs;
}
}

- willj
- May 18, 2012
- Like
- 0