0

If the user is "external user" passing the email. If emailid is match with existing contact no need to create a new contact.

conList =[select id, name, email 
          from contact 
          where 
              Account.name ='Technical Account' and Email != null AND
              (name = 'ATURB' or Name = 'Lead Central' or Name = 'Baldor' or Email=:extUserEmailId) 
          limit 50000];
if(externalSystem.equalsIgnorecase('External User') && extUserEmailId!=NULL && ct.email.equalsIgnorecase(extUserEmailId) && conList.size() > 0){
            //if(externalSystem.equalsIgnorecase('External User')){
            technicalcontact.put( 'External User', ct.id );
            System.debug('External User');
          }else{
              con = new Contact();
              con.LastName='Technical Contact';
              con.Email=extUserEmailId;
              con.accountId = accquery.id;
              System.debug('Contact Newly created');
          }
       }

If emailid is new creaing the contact. -- this part is working but previous part is not working

@AuraEnabled
public static void sendLeadToExternalSystem(String externalSystem, List<Id> leadIds,String extUserEmailId){       
   System.debug( 'externalSystem :' + externalSystem );
   System.debug( 'leadIds :' + leadIds );
   System.debug( 'leadIds.size() :' + leadIds.size() );
   System.debug( 'extUserEmailId :' + extUserEmailId);

   List<Lead> leads = [
       SELECT Id, FirstName, LastName, Name, Email, Phone, Company, City, State, PostalCode, Country, RefererUrl__c, Description__c
       FROM Lead
       WHERE ID in : leadIds Limit 50000
   ];
   Map<String,Id> technicalcontact;
   List<Contact> conList;
   conList =[select id, name, email from contact where Account.name ='Technical Account' and Email != null AND (name = 'ATURB' or Name = 'Lead Central' or Name = 'Baldor' or Email=:extUserEmailId) limit 50000];
   account accquery = new Account();
   accQuery = [Select id from account where name = 'Technical Account' limit 1];
   System.debug('conList : ' + conList); 
   technicalcontact = new Map<String,Id>();
   Contact con;
   //technicalcontact= new Map<String,Id>();
   for(contact ct : conlist){
      if(ct.name.equalsIgnorecase('Lead Central')) {
          technicalcontact.put( 'Lead Central', ct.id );
          System.debug('******************' + technicalcontact);
      }
      if(ct.name.equalsIgnorecase('ATURB')) {
          technicalcontact.put( 'ATURB', ct.id );
      }
      if(ct.name.equalsIgnorecase('Baldor')) {
          technicalcontact.put( 'Baldor', ct.id );
      }
      System.debug('66666666666666666666666');

      if(externalSystem.equalsIgnorecase('External User') && extUserEmailId!=NULL && ct.email.equalsIgnorecase(extUserEmailId) && conList.size() > 0){
        //if(externalSystem.equalsIgnorecase('External User')){
        technicalcontact.put( 'External User', ct.id );
        System.debug('External User');
      }else{
          con = new Contact();
          con.LastName='Technical Contact';
          con.Email=extUserEmailId;
          con.accountId = accquery.id;
          System.debug('Contact Newly created');
      }
   }

   if(con!=NULL){
       insert con;
       System.debug( 'con:' + con);
       technicalcontact.put( 'External User', con.id );
   }
5
  • Can you clarify in what way the "previous part is not working"? For what inputs is it not giving the expected outputs? Aug 20, 2017 at 19:58
  • Picklist value - External user & Email - test@gmail.com - If the email Id is new we need to create a new contact.
    – preeth
    Aug 21, 2017 at 3:30
  • If email id is existing contact we need to send the email to existing contact
    – preeth
    Aug 21, 2017 at 3:31
  • This part is not working - if(externalSystem.equalsIgnorecase('External User') && extUserEmailId!=NULL && ct.email.equalsIgnorecase(extUserEmailId) && conList.size() > 0){ //if(externalSystem.equalsIgnorecase('External User')){ technicalcontact.put( 'External User', ct.id ); System.debug('External User'); }
    – preeth
    Aug 21, 2017 at 3:32
  • It looks like your Contact Query may be returning more than one contact record in result. And as of this, Your for loop may be executing If and Else both part and resulting new contact record. You have to correct filters in your query or may adjust If else conditions to avoid duplicate contacts. Aug 21, 2017 at 6:11

1 Answer 1

0

One of two things is happening:

  1. Your if condition is never true. I.e. externalSystem is not 'External User' or there is no Contact with a matching email.
  2. You aren't doing anything meaningful with the technicalcontact Map in the method.

Try adding some temporary assertions that you know should be true at each stage.

Simplified version:

Map<String,Id> technicalcontact = new Map<String,Id>();

List<Contact> conList = [select id, name, email 
                         from contact 
                         where Account.name ='Technical Account' and Email != null AND
                         (name = 'ATURB' or Name = 'Lead Central' or Name = 'Baldor' or Email=:extUserEmailId) limit 50000];

// blah blah blah

for(contact ct : conlist){
  // blah blah blah

  System.assertEquals('External User', externalSystem);
  System.assertEquals(extUserEmailId, ct.email);

  if(externalSystem.equalsIgnorecase('External User') && extUserEmailId!=NULL && ct.email.equalsIgnorecase(extUserEmailId)){
    //if(externalSystem.equalsIgnorecase('External User')){
    technicalcontact.put( 'External User', ct.id );
    System.debug('External User');

    // And then? What meaningful thing happens with the technicalcontact map?
    System.assert(false, 'We made it, now what?');

  }else{
      con = new Contact();
      // blah blah blah
  }
}
// Should something happen with technicalcontact before the method ends?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.