• AlphaP
  • NEWBIE
  • 25 Points
  • Member since 2011

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 17
    Replies

I'm writng a test class for a lead event trigger and I;m stuck on how to account for one part of my test class.

 

The select querey is covered - which reads:

 

List<Event> checkEvent = [select id from event where WhoId in :leadidset];

 

Now I have a part of code that looks like this that is not covered:

 

    else if ((l.Call_Back_Requested_All__c <> null) && (CheckEvent.size() == 1) && (l.isconverted==false)) {
    for(Event MyEvent : checkEvent)
    {
    MyEvent.ActivityDateTime = l.Call_Back_Requested_All__c;
    MyEvent.Description = 'Call back customer ' + l.firstname + ' ' + l.lastname +
    ' with Lead Party ID ' + l.Company + '.';
    MyEvent.Subject = 'Call back customer ' + l.firstname + ' ' + l.lastname;
    MyEvent.DurationInMinutes = 15;
    //calEvent.WhatId = Trigger.new[0].Id;
    MyEvent.ReminderDateTime = l.Call_Back_Requested_All__c.addMinutes(-15);
    MyEvent.IsReminderSet = true;
    MyEvent.Whoid = l.Id;
    updateEvent.add(MyEvent);

 

 

When I'm creating my test for this -  I can't get this part covered.   How can I make sure I insert a lead with an id that matches the whoid?   I can't assign an id in a test class, right?   If I put in the lead first, it doesn't fit the flow and cover the class.  If I try to say something like 'whoid = l1.id,' that deson't work because I haven't inserted that lead yet.

 

Does that quiestion make sense?

 

Thanks!

 

  • March 10, 2012
  • Like
  • 0

I have 90% test class coverage for a trigger with the exception of this one line in my code:

 

 27    else if ((tmpDNCList.size()<1) && (myLead.Sub_Status_ALL__c == 'DNC Requested'))
 28   {
 29   Trigger.new[0].Status.addError('Please add DNC record to Salesforce prior to saving.');

 

 

When I create a record in the test class that matches this critieria - it causes a DML exception (as it should) and halts all of my other tests if I run them all at once and it drops my code coverage to almost 50%.   The class itself gets 100% coverage though.  

 

Is there something sepcial about wiritng a test class when using the error method?

  • February 10, 2012
  • Like
  • 0

EDITIED:  Cleaned up code, added commnets and added some clarity

 

I'm working with a referal process where we work a lead one day and then get another list to call when documents are ready.   The first part of the trigger works - the part where i'm assigning the value of the External Unique ID to the lead company.  

 

For the rest - I want to find leads that have been converted - so that when I get the second lead for 'Docs Ready' - I can look at who converted the lead and create a calendar event for that person that contains some of the information on that customer.

 

So I pull a list of converted leads by matching the External Unique ID - they will already be in the system since this is a follow-up lead.  

 

I looked at the system.debug logs and the ConvertedLeadList never seesm to populate when I'm inseting test files.   Is it my blocking?  The query looks fine.  Any ideas?

 

Then what I would like to happen - is take the ConvertedLeadList and loop through that and create a calendar event based on data from that list.  One item I am unsure how to do is assign the calevent onwer based on the last modified id of the converted lead from the ConvertedLeadList.

 

How do I write the for loop for that?  I looked at the dev guide and i'm just not 'getting' it.

trigger Referral on Lead (before insert, before update) {

    //create a list of leads 'Converted:eadList' that looks for a mtach in the tmpEUIDset.  These converted leads have agent owners and will need to have a calendar event created.           
    List<Lead> ConvertedLeadList = new List<Lead>();
    Set<String> tmpEUIDset = new Set<String>();


//referals are external these - referals and do not have Party IDs - map EUID to party ID (company) on insert and exit loop        
        for(Lead l : trigger.new){         
        if((l.Lead_Type_ALL__c == 'LegalZoom Referral') || (l.Lead_Type_ALL__c == 'LegalZoom Docs Ready')) {
        l.company = l.External_Unique_ID__c;
                                
                           } //exit loop
                    
    
    //add EUIDS to string from insterted leads to match against when creating the ConvertedLeadList
    for(Lead lead : Trigger.new){
        tmpEUIDset.add(lead.External_Unique_ID__c);
            if(Lead.External_Unique_ID__c != null)
            system.debug('Here is the current count of items in the tmpEUIDset string:' + tmpEUIDset.size());
           
                          } // exit loop                  
    
// pull a list of converted leads from the first referral call by querying leads aginst the tmpEUID list
ConvertedLeadList = [select id, Lead_Type_ALL__c, External_Unique_ID__c, Lastmodifiedbyid, Name from Lead where SystemModstamp=LAST_N_DAYS:90 AND name in :tmpEUIDSet AND Lead.Status='Contacted' AND Isconverted=true];
system.debug('Here is the current ConvertedLeadList size' + Convertedleadlist.size());

    
//look for an existing opportunity when there is a 'Docs Ready' lead type

            if(ConvertedLeadList.size()>0){
                
            for(Lead mylead : trigger.new) {
                 if ((trigger.new[0].External_Unique_ID__c == ConvertedLeadList[0].External_Unique_ID__c) && (trigger.new[0].Lead_Type_ALL__c == 'LegalZoom Docs Ready')){
                     System.debug('Matching Converted lead record found - creating event for' + trigger.new[0]);
                    //This will generate an event for the current user if one does not exist currently
                    List<Event> checkEvent = [select id from event where whoid  =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested_All__c];
                        if(checkEvent == null || checkEvent.size() == 0){
                            //create new event
                            Event calEvent = new Event();
                            //base date vand time on Call_Back_Requested_ALL__c field on inserted lead
                            calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested_All__c;
                            calEvent.Description = 'Documents are ready. Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with Lead Party ID ' + Trigger.new[0].Company + '.';
                            calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                            calEvent.DurationInMinutes = 15;
                            calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested_All__c.addMinutes(-15);
                            // the cal event needs to go to agent that converted the lead
                            calEvent.Ownerid = ConvertedLeadList[0].lastmodifiedbyid;
                            calEvent.IsReminderSet = true;
                            calEvent.Whoid  = Trigger.new[0].Id;
                            insert calEvent;
                         
                            
                        }
                    }
                }
             }
          }
    }

 

  • January 25, 2012
  • Like
  • 0

I am trying to make an event whenever a date/time filed is populated on an opportunity object.   I thouight i could use "whatid" to point to the opportunity, but I may have misread the dev docs.  I'm getting:

 

Error:Apex trigger OppEvent caused an unexpected exception, contact your administrator: OppEvent: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 006T0000008bvfaIAA: [WhoId]: Trigger.OppEvent: line 19, column 17
 
I dug around on the forum - and don't see what i might be doing wrong.   Any thoughts?

 

 

 

  /* This trigger creates an event when call back requested is selected on the opportunity
  
  trigger OppEvent on Opportunity (before update) {

     if (Trigger.new[0].Call_Back_Requested__c <> null){
            //This will generate an event for the current user if one does not exist currently
            List<Event> checkEvent = [select id from event where WhatId =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested__c];
            if(checkEvent == null || checkEvent.size() == 0){
                Event calEvent = new Event();
                calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested__c;
                calEvent.Description = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with ID ' + Trigger.new[0].Company + '.';
                calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                calEvent.DurationInMinutes = 15;
                //calEvent.WhatId = Trigger.new[0].Id;
                calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested__c.addMinutes(-15);
                calEvent.IsReminderSet = true;
                calEvent.WhoId = Trigger.new[0].Id;
                insert calEvent;
            }
        }   

 

  • December 08, 2011
  • Like
  • 0

Line 176 is bombing out because of the '}'    I'm missing why my blocking is off on that line.    Any ideas? 

 

*/
trigger StandOffPeriod2 on Lead (before Insert, before update) {

    List<Opportunity> opptList = new List<Opportunity>();
    List<Opportunity> tmpList = new List<Opportunity>();
    List<Lead> leadList = new List<Lead>();
    List<Lead> tmpleadList = new List<Lead>();
    Set<String> tmpSet = new Set<String>();
    Set<String> tmpPhoneSet = new Set<String>();
    Set<String> tmpPartyIdSet = new Set<String>();
    Map<Id, Lead> unConvertedLeadsToUpdate = new Map<Id,lead>();
    Date myDate = Date.Today().addDays(-90);
    for(Lead lead : Trigger.new){
        tmpSet.add(lead.Company_Phone__c);
        tmpPhoneSet.add(lead.Phone);
        tmpPartyIdSet.add(lead.Company);
        //tmpleadList.add(lead);

    }    
    
    System.debug('tmpPhoneset = ' + tmpPhoneset);
    System.debug('tmpPartyIdset = ' + tmpPartyIdSet);
    
    //pull a list of all opportunities that match the company_phone__c field
    //tmpList = [select id, Lead_Phone_Number_ALL__c, Lead_Status_All__c, Lead_Call_Date_ALL__c,Lead_Company_Phone__c from Opportunity where Lead_Company_Phone__c in :tmpSet];
    tmpList  = [select id, Lead_Phone_Number_ALL__c, Lead_Status_All__c, Lead_Call_Date_ALL__c,Account.Name  from Opportunity where Account.name in :tmpPartyIdSet OR Lead_Phone_Number_ALL__c in:tmpPhoneSet];
    //System.debug('tmpList2 information = ' + tmpList2.size());
    
    //pull a list of all existing leads that match the company_phone__c field
    //tmpLeadList = [select id, Call_Date_All__c, Company_Phone__c from Lead where Company_Phone__c in:tmpSet];
    tmpLeadList = [select id, Call_Date_All__c, Company_Phone__c,Party_ID_ALL__c,Company,Phone from Lead where isConverted=FALSE AND (Phone in:tmpPhoneSet OR Company in:tmpPartyIdSet)];
    
    //pull a list of matching Do Not Call records
    tmpDNCList = [select id, Name from Do_Not_Call__c where Name in:tmpPartyIdSet];
    
    //Build a map with all retrieved lead objects

    System.debug('The tmpList is currently ' + tmpList.size());      
    
        
        for(Lead myLead : Trigger.new){
            if(myLead.status == 'Open' && Trigger.new.size() > 1){
                //Check the lead Dates and set the status if necessary

                System.debug('Lead to look at...Here is the date -=> ' + myLead.Call_Date_All__c);
                System.debug('Comparing to myDate -=> ' + myDate);
                
                //Checking the opportunity
                Boolean opptCheck = false;
                if(tmpList.size()>0){
                
                    for(Opportunity o : tmpList){
                        if((o.Account.Name == myLead.Company) || (o.Lead_Phone_Number_ALL__c==myLead.Phone)){
                            if(o.Lead_Call_Date_All__c == null){
                                System.debug('NULL Date found on the Opportunity...setting status to Open');
                                myLead.Status = 'Open';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                                                    
                            }else if (o.Lead_Call_Date_All__c <= myDate){
                                System.debug('Earlier date found on the Opportunity...setting status to Open');
                                myLead.Status = 'Open';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                                
                            }else if(o.Lead_Call_Date_All__c > myDate){
                                System.debug('Later date found on the Opportunity...setting status to Pending');
                                myLead.Status = 'Pending';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                            }
                        }
                    }
                }
                if(opptCheck){
                    System.debug('######################################## Opportunity has set the status #####################');
                    continue;
                }
                
                 Boolean leadCheck = false;                
                if(tmpLeadList.size()>0 && myLead.id == null){

                    for(Lead l : tmpLeadList){
                       if((l.Company == myLead.Company) || (l.Phone == myLead.Phone)){
                           if(l.Call_Date_All__c == null){
                                System.debug('NULL Date found on the LEAD MATCH...setting status to Open');
                                myLead.Status = 'Open';
                                l.status='Open';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                                                    
                            }else if (l.Call_Date_All__c <= myDate){
                                System.debug('Earlier date found on the LEAD MATCH...setting status to Open');
                                myLead.Status = 'Open';
                                l.status='Open';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                                
                            }else if(l.Call_Date_All__c > myDate){
                                System.debug('Later date found on the LEAD MATCH...setting status to Pending');
                                myLead.Status = 'Pending';
                                l.status='Pending';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                            }
                       
                       }
                        
                    }
                }
                
                if(leadCheck){
                    System.debug('######################################## Existing Lead has set the status #####################');
                    continue;
                }
                
                if(myLead.Call_Date_All__c == null){
                    System.debug('NULL Date found...setting status to Open');
                    myLead.Status = 'Open';
                    //leadList.add(myLead);                    
                }else if (myLead.Call_Date_All__c <= myDate){
                    System.debug('Earlier date found...setting status to Open');
                    myLead.Status = 'Open';
                    //leadList.add(myLead);
                }else if(myLead.Call_Date_All__c > myDate){
                    System.debug('Later date found...setting status to Pending');
                    myLead.Status = 'Pending';
                    //leadList.add(myLead);
                }
            }
        }
        
                //Checking the Do Not Call Object
                Boolean DNCCheck = false;
                if(tmpDNCList.size()>0){
                
                    for(Do_Not_Call obj :  tmpDNCList) {
                        if((obj.Name == myLead.Company) {
                            System.debug(Matching Do Not Call Record Found - setting status to 'pending');
                                myLead.Status = 'Pending';
                                DNCCheck = true;
                                break;
                                                    

                            }
                        }
                    }
                
                if(DNCCheck){
                    System.debug('######################################## Do Not Call Object has set the status #####################');
                    continue;
                }
                    
        if(unConvertedLeadsToUpdate.size() > 0){
        List<Lead> updateME = new List<Lead>();
        updateME = unConvertedLeadsToUpdate.values();
        System.debug('unConvertedLeadsToUpdate SIZE = ' + unConvertedLeadsToUpdate.size());
        //    update updateMe;
        }
}

 

  • November 17, 2011
  • Like
  • 0

 

trigger CEWSowner on PSC_CEWS__c (before insert) {
    PSC_CEWS__c  [] CEWSList = trigger.new;
    List <PSC_CEWS__c> updatedListToInsert= new List <PSC_CEWS__c>();
       
    for(PSC_CEWS__c CEWS : CEWSList){
         List<User> lookupCEWSUser = [select ID from User where CAI__c =: CEWS.CAI__c Limit 1];
         if(lookupCEWSUser.size() > 0){
         CEWS.Ownerid = lookupCEWSuser[0].Id;
         updatedListToInsert.add(CEWS);
         }
     }
    if(updatedListToInsert.size() > 0) {
    insert updatedListToInsert;
    }
}

 So I have a custom object 'PSC_CEWS__c' that is upserted/inserted with a unique identifier for each user 'CAI__c.'     Before insert, i want to match the CAI__c to the correct User in Salesforce, and make that person the record owner.   So far, this actually does nothing.  I have something typed wrong, but have looked at it long enough and am thinking I'm missing something real basic here.

 

Any thoughts?

  • November 12, 2011
  • Like
  • 0

I'm writng a test class for a lead event trigger and I;m stuck on how to account for one part of my test class.

 

The select querey is covered - which reads:

 

List<Event> checkEvent = [select id from event where WhoId in :leadidset];

 

Now I have a part of code that looks like this that is not covered:

 

    else if ((l.Call_Back_Requested_All__c <> null) && (CheckEvent.size() == 1) && (l.isconverted==false)) {
    for(Event MyEvent : checkEvent)
    {
    MyEvent.ActivityDateTime = l.Call_Back_Requested_All__c;
    MyEvent.Description = 'Call back customer ' + l.firstname + ' ' + l.lastname +
    ' with Lead Party ID ' + l.Company + '.';
    MyEvent.Subject = 'Call back customer ' + l.firstname + ' ' + l.lastname;
    MyEvent.DurationInMinutes = 15;
    //calEvent.WhatId = Trigger.new[0].Id;
    MyEvent.ReminderDateTime = l.Call_Back_Requested_All__c.addMinutes(-15);
    MyEvent.IsReminderSet = true;
    MyEvent.Whoid = l.Id;
    updateEvent.add(MyEvent);

 

 

When I'm creating my test for this -  I can't get this part covered.   How can I make sure I insert a lead with an id that matches the whoid?   I can't assign an id in a test class, right?   If I put in the lead first, it doesn't fit the flow and cover the class.  If I try to say something like 'whoid = l1.id,' that deson't work because I haven't inserted that lead yet.

 

Does that quiestion make sense?

 

Thanks!

 

  • March 10, 2012
  • Like
  • 0

I have 90% test class coverage for a trigger with the exception of this one line in my code:

 

 27    else if ((tmpDNCList.size()<1) && (myLead.Sub_Status_ALL__c == 'DNC Requested'))
 28   {
 29   Trigger.new[0].Status.addError('Please add DNC record to Salesforce prior to saving.');

 

 

When I create a record in the test class that matches this critieria - it causes a DML exception (as it should) and halts all of my other tests if I run them all at once and it drops my code coverage to almost 50%.   The class itself gets 100% coverage though.  

 

Is there something sepcial about wiritng a test class when using the error method?

  • February 10, 2012
  • Like
  • 0

FATAL_ERROR|System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times).

 


trigger InsertFin  on Case (after insert) {
 list<Fin__c> falist = new list<Fin__c>();
 
  if ((Trigger.new.size() == 1)&&(trigger.new[0].MID__c !=null)){
      list<Contact> con = new list<Contact>([Select  c.EmpId__c,c.branch__c,c.branch__r.name,c.Id,c.name From Contact c where c.EmpId__c!=null and c.EmpId__c =: trigger.new[0].MID__c limit 1]);
      Fin__c f1 = new Fin__c();
        f1.Case__c = trigger.new[0].Id;
        if(con.size()>0){
        f1.Branch__c = con[0].branch__r.name;
        f1.name= con[0].name;
        }
        falist.add(f1);
      }
  if ((Trigger.new.size() == 1)&&(trigger.new[0].MID_2__c !=null)){
      list<Contact> con = new list<Contact>([Select  c.EmpId__c,c.branch__c,c.branch__r.name,c.Id,c.name From Contact c where c.EmpId__c!=null and c.EmpId__c =: trigger.new[0].MID_2__c limit 1]);
      Fin__c f2 = new Fin__c();
        f2.Case__c = trigger.new[0].Id;
        if(con.size()>0){
        f2.Branch__c = con[0].branch__r.name;
        f2.name= con[0].name;
        }
        falist.add(f2);
      }
      
  if ((Trigger.new.size() == 1)&&(trigger.new[0].MID_3__c !=null)){
      list<Contact> con = new list<Contact>([Select  c.EmpId__c,c.branch__c,c.branch__r.name,c.Id,c.name From Contact c where c.EmpId__c!=null and c.EmpId__c =: trigger.new[0].MID_3__c limit 1]);
      Fin__c f3 = new Fin__c();
        f3.Case__c = trigger.new[0].Id;
        if(con.size()>0){
        f3.Branch__c = con[0].branch__r.name;
        f3.name= con[0].name;
        }
        falist.add(f3);
      }
      
      if ((Trigger.new.size() == 1)&&(trigger.new[0].MID_4__c !=null)){
      list<Contact> con = new list<Contact>([Select  c.EmpId__c,c.branch__c,c.branch__r.name,c.Id,c.name From Contact c where c.EmpId__c!=null and c.EmpId__c =: trigger.new[0].MID_4__c limit 1]);
      Fin__c f4 = new Fin__c();
        f4.Case__c = trigger.new[0].Id;
        if(con.size()>0){
        f4.Branch__c = con[0].branch__r.name;
        f4.name= con[0].name;
        }
        falist.add(f4);
      }
      if ((Trigger.new.size() == 1)&&(trigger.new[0].MID_5__c !=null)){
      list<Contact> con = new list<Contact>([Select  c.EmpId__c,c.branch__c,c.branch__r.name,c.Id,c.name From Contact c where c.EmpId__c!=null and c.EmpId__c =: trigger.new[0].MID_5__c limit 1]);
      Fin__c f5 = new Fin__c();
        f5.Case__c = trigger.new[0].Id;
        if(con.size()>0){
        f5.Branch__c = con[0].branch__r.name;
        f5.name= con[0].name;
        }
        falist.add(f5);
      }
      insert falist;
      
      
}

How to fix this trigger.Any Idea?

 

 

Thanks


EDITIED:  Cleaned up code, added commnets and added some clarity

 

I'm working with a referal process where we work a lead one day and then get another list to call when documents are ready.   The first part of the trigger works - the part where i'm assigning the value of the External Unique ID to the lead company.  

 

For the rest - I want to find leads that have been converted - so that when I get the second lead for 'Docs Ready' - I can look at who converted the lead and create a calendar event for that person that contains some of the information on that customer.

 

So I pull a list of converted leads by matching the External Unique ID - they will already be in the system since this is a follow-up lead.  

 

I looked at the system.debug logs and the ConvertedLeadList never seesm to populate when I'm inseting test files.   Is it my blocking?  The query looks fine.  Any ideas?

 

Then what I would like to happen - is take the ConvertedLeadList and loop through that and create a calendar event based on data from that list.  One item I am unsure how to do is assign the calevent onwer based on the last modified id of the converted lead from the ConvertedLeadList.

 

How do I write the for loop for that?  I looked at the dev guide and i'm just not 'getting' it.

trigger Referral on Lead (before insert, before update) {

    //create a list of leads 'Converted:eadList' that looks for a mtach in the tmpEUIDset.  These converted leads have agent owners and will need to have a calendar event created.           
    List<Lead> ConvertedLeadList = new List<Lead>();
    Set<String> tmpEUIDset = new Set<String>();


//referals are external these - referals and do not have Party IDs - map EUID to party ID (company) on insert and exit loop        
        for(Lead l : trigger.new){         
        if((l.Lead_Type_ALL__c == 'LegalZoom Referral') || (l.Lead_Type_ALL__c == 'LegalZoom Docs Ready')) {
        l.company = l.External_Unique_ID__c;
                                
                           } //exit loop
                    
    
    //add EUIDS to string from insterted leads to match against when creating the ConvertedLeadList
    for(Lead lead : Trigger.new){
        tmpEUIDset.add(lead.External_Unique_ID__c);
            if(Lead.External_Unique_ID__c != null)
            system.debug('Here is the current count of items in the tmpEUIDset string:' + tmpEUIDset.size());
           
                          } // exit loop                  
    
// pull a list of converted leads from the first referral call by querying leads aginst the tmpEUID list
ConvertedLeadList = [select id, Lead_Type_ALL__c, External_Unique_ID__c, Lastmodifiedbyid, Name from Lead where SystemModstamp=LAST_N_DAYS:90 AND name in :tmpEUIDSet AND Lead.Status='Contacted' AND Isconverted=true];
system.debug('Here is the current ConvertedLeadList size' + Convertedleadlist.size());

    
//look for an existing opportunity when there is a 'Docs Ready' lead type

            if(ConvertedLeadList.size()>0){
                
            for(Lead mylead : trigger.new) {
                 if ((trigger.new[0].External_Unique_ID__c == ConvertedLeadList[0].External_Unique_ID__c) && (trigger.new[0].Lead_Type_ALL__c == 'LegalZoom Docs Ready')){
                     System.debug('Matching Converted lead record found - creating event for' + trigger.new[0]);
                    //This will generate an event for the current user if one does not exist currently
                    List<Event> checkEvent = [select id from event where whoid  =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested_All__c];
                        if(checkEvent == null || checkEvent.size() == 0){
                            //create new event
                            Event calEvent = new Event();
                            //base date vand time on Call_Back_Requested_ALL__c field on inserted lead
                            calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested_All__c;
                            calEvent.Description = 'Documents are ready. Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with Lead Party ID ' + Trigger.new[0].Company + '.';
                            calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                            calEvent.DurationInMinutes = 15;
                            calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested_All__c.addMinutes(-15);
                            // the cal event needs to go to agent that converted the lead
                            calEvent.Ownerid = ConvertedLeadList[0].lastmodifiedbyid;
                            calEvent.IsReminderSet = true;
                            calEvent.Whoid  = Trigger.new[0].Id;
                            insert calEvent;
                         
                            
                        }
                    }
                }
             }
          }
    }

 

  • January 25, 2012
  • Like
  • 0

I am trying to make an event whenever a date/time filed is populated on an opportunity object.   I thouight i could use "whatid" to point to the opportunity, but I may have misread the dev docs.  I'm getting:

 

Error:Apex trigger OppEvent caused an unexpected exception, contact your administrator: OppEvent: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 006T0000008bvfaIAA: [WhoId]: Trigger.OppEvent: line 19, column 17
 
I dug around on the forum - and don't see what i might be doing wrong.   Any thoughts?

 

 

 

  /* This trigger creates an event when call back requested is selected on the opportunity
  
  trigger OppEvent on Opportunity (before update) {

     if (Trigger.new[0].Call_Back_Requested__c <> null){
            //This will generate an event for the current user if one does not exist currently
            List<Event> checkEvent = [select id from event where WhatId =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested__c];
            if(checkEvent == null || checkEvent.size() == 0){
                Event calEvent = new Event();
                calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested__c;
                calEvent.Description = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with ID ' + Trigger.new[0].Company + '.';
                calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                calEvent.DurationInMinutes = 15;
                //calEvent.WhatId = Trigger.new[0].Id;
                calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested__c.addMinutes(-15);
                calEvent.IsReminderSet = true;
                calEvent.WhoId = Trigger.new[0].Id;
                insert calEvent;
            }
        }   

 

  • December 08, 2011
  • Like
  • 0

Line 176 is bombing out because of the '}'    I'm missing why my blocking is off on that line.    Any ideas? 

 

*/
trigger StandOffPeriod2 on Lead (before Insert, before update) {

    List<Opportunity> opptList = new List<Opportunity>();
    List<Opportunity> tmpList = new List<Opportunity>();
    List<Lead> leadList = new List<Lead>();
    List<Lead> tmpleadList = new List<Lead>();
    Set<String> tmpSet = new Set<String>();
    Set<String> tmpPhoneSet = new Set<String>();
    Set<String> tmpPartyIdSet = new Set<String>();
    Map<Id, Lead> unConvertedLeadsToUpdate = new Map<Id,lead>();
    Date myDate = Date.Today().addDays(-90);
    for(Lead lead : Trigger.new){
        tmpSet.add(lead.Company_Phone__c);
        tmpPhoneSet.add(lead.Phone);
        tmpPartyIdSet.add(lead.Company);
        //tmpleadList.add(lead);

    }    
    
    System.debug('tmpPhoneset = ' + tmpPhoneset);
    System.debug('tmpPartyIdset = ' + tmpPartyIdSet);
    
    //pull a list of all opportunities that match the company_phone__c field
    //tmpList = [select id, Lead_Phone_Number_ALL__c, Lead_Status_All__c, Lead_Call_Date_ALL__c,Lead_Company_Phone__c from Opportunity where Lead_Company_Phone__c in :tmpSet];
    tmpList  = [select id, Lead_Phone_Number_ALL__c, Lead_Status_All__c, Lead_Call_Date_ALL__c,Account.Name  from Opportunity where Account.name in :tmpPartyIdSet OR Lead_Phone_Number_ALL__c in:tmpPhoneSet];
    //System.debug('tmpList2 information = ' + tmpList2.size());
    
    //pull a list of all existing leads that match the company_phone__c field
    //tmpLeadList = [select id, Call_Date_All__c, Company_Phone__c from Lead where Company_Phone__c in:tmpSet];
    tmpLeadList = [select id, Call_Date_All__c, Company_Phone__c,Party_ID_ALL__c,Company,Phone from Lead where isConverted=FALSE AND (Phone in:tmpPhoneSet OR Company in:tmpPartyIdSet)];
    
    //pull a list of matching Do Not Call records
    tmpDNCList = [select id, Name from Do_Not_Call__c where Name in:tmpPartyIdSet];
    
    //Build a map with all retrieved lead objects

    System.debug('The tmpList is currently ' + tmpList.size());      
    
        
        for(Lead myLead : Trigger.new){
            if(myLead.status == 'Open' && Trigger.new.size() > 1){
                //Check the lead Dates and set the status if necessary

                System.debug('Lead to look at...Here is the date -=> ' + myLead.Call_Date_All__c);
                System.debug('Comparing to myDate -=> ' + myDate);
                
                //Checking the opportunity
                Boolean opptCheck = false;
                if(tmpList.size()>0){
                
                    for(Opportunity o : tmpList){
                        if((o.Account.Name == myLead.Company) || (o.Lead_Phone_Number_ALL__c==myLead.Phone)){
                            if(o.Lead_Call_Date_All__c == null){
                                System.debug('NULL Date found on the Opportunity...setting status to Open');
                                myLead.Status = 'Open';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                                                    
                            }else if (o.Lead_Call_Date_All__c <= myDate){
                                System.debug('Earlier date found on the Opportunity...setting status to Open');
                                myLead.Status = 'Open';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                                
                            }else if(o.Lead_Call_Date_All__c > myDate){
                                System.debug('Later date found on the Opportunity...setting status to Pending');
                                myLead.Status = 'Pending';
                                opptCheck = true;
                                System.debug('Here is the oppt id = ' + o.id);
                                break;
                            }
                        }
                    }
                }
                if(opptCheck){
                    System.debug('######################################## Opportunity has set the status #####################');
                    continue;
                }
                
                 Boolean leadCheck = false;                
                if(tmpLeadList.size()>0 && myLead.id == null){

                    for(Lead l : tmpLeadList){
                       if((l.Company == myLead.Company) || (l.Phone == myLead.Phone)){
                           if(l.Call_Date_All__c == null){
                                System.debug('NULL Date found on the LEAD MATCH...setting status to Open');
                                myLead.Status = 'Open';
                                l.status='Open';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                                                    
                            }else if (l.Call_Date_All__c <= myDate){
                                System.debug('Earlier date found on the LEAD MATCH...setting status to Open');
                                myLead.Status = 'Open';
                                l.status='Open';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                                
                            }else if(l.Call_Date_All__c > myDate){
                                System.debug('Later date found on the LEAD MATCH...setting status to Pending');
                                myLead.Status = 'Pending';
                                l.status='Pending';
                                Boolean addLead = true;
                                if(!unconvertedLeadsToUpdate.containsKey(l.Id)){                                
                                    unConvertedLeadsToUpdate.put(l.id, l);
                                }
                                leadCheck = true;
                                System.debug('Here is the LEAD id = ' + l.id);
                                //break;
                            }
                       
                       }
                        
                    }
                }
                
                if(leadCheck){
                    System.debug('######################################## Existing Lead has set the status #####################');
                    continue;
                }
                
                if(myLead.Call_Date_All__c == null){
                    System.debug('NULL Date found...setting status to Open');
                    myLead.Status = 'Open';
                    //leadList.add(myLead);                    
                }else if (myLead.Call_Date_All__c <= myDate){
                    System.debug('Earlier date found...setting status to Open');
                    myLead.Status = 'Open';
                    //leadList.add(myLead);
                }else if(myLead.Call_Date_All__c > myDate){
                    System.debug('Later date found...setting status to Pending');
                    myLead.Status = 'Pending';
                    //leadList.add(myLead);
                }
            }
        }
        
                //Checking the Do Not Call Object
                Boolean DNCCheck = false;
                if(tmpDNCList.size()>0){
                
                    for(Do_Not_Call obj :  tmpDNCList) {
                        if((obj.Name == myLead.Company) {
                            System.debug(Matching Do Not Call Record Found - setting status to 'pending');
                                myLead.Status = 'Pending';
                                DNCCheck = true;
                                break;
                                                    

                            }
                        }
                    }
                
                if(DNCCheck){
                    System.debug('######################################## Do Not Call Object has set the status #####################');
                    continue;
                }
                    
        if(unConvertedLeadsToUpdate.size() > 0){
        List<Lead> updateME = new List<Lead>();
        updateME = unConvertedLeadsToUpdate.values();
        System.debug('unConvertedLeadsToUpdate SIZE = ' + unConvertedLeadsToUpdate.size());
        //    update updateMe;
        }
}

 

  • November 17, 2011
  • Like
  • 0

 

trigger CEWSowner on PSC_CEWS__c (before insert) {
    PSC_CEWS__c  [] CEWSList = trigger.new;
    List <PSC_CEWS__c> updatedListToInsert= new List <PSC_CEWS__c>();
       
    for(PSC_CEWS__c CEWS : CEWSList){
         List<User> lookupCEWSUser = [select ID from User where CAI__c =: CEWS.CAI__c Limit 1];
         if(lookupCEWSUser.size() > 0){
         CEWS.Ownerid = lookupCEWSuser[0].Id;
         updatedListToInsert.add(CEWS);
         }
     }
    if(updatedListToInsert.size() > 0) {
    insert updatedListToInsert;
    }
}

 So I have a custom object 'PSC_CEWS__c' that is upserted/inserted with a unique identifier for each user 'CAI__c.'     Before insert, i want to match the CAI__c to the correct User in Salesforce, and make that person the record owner.   So far, this actually does nothing.  I have something typed wrong, but have looked at it long enough and am thinking I'm missing something real basic here.

 

Any thoughts?

  • November 12, 2011
  • Like
  • 0