• divya gourigari 14
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 4
    Replies
Apex class:
public class Deactiveusercls {
@future    
public static void processAsync() {
    
     DateTime dt = DateTime.now();
        String dateString = dt.format().replace('/',' ');
       System.debug(dateString);
      String[] strDTDivided = dateString.split(' ');
        system.debug('strDTDivided'+strDTDivided);
       string month = strDTDivided.get(0);
       system.debug('month'+month);
      string day = strDTDivided.get(1).replace(',', '');
       system.debug('day'+day);
      string year = strDTDivided.get(2);
      system.debug('year'+year);

      string hour = strDTDivided.get(3).split(':').get(0);
      string minute = strDTDivided.get(3).split(':').get(1);
      string second = '00';
      String ampm = strDTDivided.get(4);
    /*  if (strDTDivided.get(4) == 'PM')
     {
     hour = String.ValueOf(Integer.ValueOf(hour) + 12);
     }*/
   // string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
   string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ' ' +ampm; 
    system.debug('stringDate'+stringDate);
    
    map<string, Deactiveuser__c> ObjMap = new map<string, Deactiveuser__c>();
    LIst<Deactiveuser__c> activeLst =[select id,UserName__c,Name__c,TerminateDate__c from Deactiveuser__c where TerminateDate__c =:stringDate];
    system.debug('activeLst'+activeLst);
    for(Deactiveuser__c obj: activeLst)
    {
        if (obj.UserName__c != Null)
        {
            ObjMap.put(obj.UserName__c, obj);
        }
    }
    System.debug('ObjMap'+ObjMap);
    List<User> users = [SELECT isActive,email,Username,Alias FROM User WHERE Alias IN :ObjMap.KeySet()  and  isActive = true];
    system.debug('users'+users);
    List<User> userUpdateList = new List<User>();
   // String dateFormat = 'yyyy-MM-dd\'T\'HH:mm:ss\'Z\''.replace('\'T\'',' ').replace('\'Z\'','');
    for(User u: users)
    {
        u.isActive = false;
        userUpdateList.add(u);
   }
    if(userUpdateList.size() > 0)
    {
        update userUpdateList;
        system.debug('userUpdateList'+userUpdateList);
    }  
}   
}

Test Class:

@isTest
public class DeactiveuserclsTest {
 static testMethod void deactive() {
   
     
     
      Profile pf= [Select Id from profile where Name='System Administrator']; 
     User u = new User(
     ProfileId = pf.Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'test',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
  
     );
     insert u;
     
     Deactiveuser__c d=new Deactiveuser__c();
     d.Name__c ='testUser';
     d.UserName__c = 'test';
     d.TerminateDate__c = '2022-4-25 6 PM';
     insert d;
    
     u.IsActive = false;
    // update u;
    
    Test.startTest();
     Deactiveusercls.processAsync();
      Test.stopTest(); 
     
//   User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
        System.assertEquals(false, u.IsActive);  
     }
}
The failing line is like Datetime.parse('3-31-2022 12:00 AM')
The exception is:
System.TypeException: Invalid date/time: 3-31-2022 12:00 AM
However, if I run this line below in anonymous Apex in dev console, there is no exception.
System.debug('parse'+Datetime.parse('3-31-2022 12:00 AM'));
trigger onOpportunity_AddPointofContact on Opportunity (after update)
{
 List<Point_of_Contact__c> getPrimaryLst=[select id,Primary__c from Point_of_Contact__c where Primary__c =:true];
    List<Point_of_Contact__c> insertPointConList=new List<Point_of_Contact__c>();
    for(Opportunity opp:Trigger.new)
    {
        Opportunity oldRec =Trigger.oldMap.get(opp.Id);
        if(oldRec.StageName != opp.StageName && opp.StageName == 'Approved')
        {
           Point_of_Contact__c con=new Point_of_Contact__c();
           ngsCon.Point_of_Contact__c=opp.Program_Officer__c;
           ngsCon.Contact__c=opp.npsp__Primary_Contact__c;
           ngsCon.Type__c='grant';
           ngsCon.Start_Date__c=opp.FGM_Base__Award_Date__c;
            if(getPrimaryLst.size() == 0){
             ngsCon.Primary__c =true;   
            }
           insertPointConList.add(ngsCon);
        }
        
    }
    if(insertPointConList.size()>0)
    {
        try {
            insert insertPointConList;
            system.debug('====>insertPointConList'+insertPointConList);
           }
        catch (Exception Ex){
            system.debug(Ex);
        }
    }
 
}

here i wrote the code but record is inserting duplicate records
execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.emailmatchdatacontact: line  29,column 1

trigger emailmatchdatacontact on ContactRelationship__c (before insert,before update) 
{
    List<ContactRelationship__c> con= new List<ContactRelationship__c>();
    List<Contact> conlist=new List<Contact>();
    Set<String> valtocheck=new Set<String>();
    Map<String,Contact> valfound=new Map<String,Contact>();
    for(ContactRelationship__c cr:trigger.new)
    {
      if(!string.isBlank(cr.EmailId__c)) valToCheck.add(cr.EmailId__c);
    }
    system.debug(valToCheck);
    Map<String, boolean> contactRelationCheckMap = new Map<String, boolean>();
    if(valtocheck != null)
    {
    List<Contact> conrelList = new List<Contact>();
    conrelList = [select id,Email from Contact where Email IN:valtocheck];
    if(conrelList.size()>0)
    {
    for(Contact c:conrelList)
    {   
        contactRelationCheckMap.put(c.Email, true);
    }
        for(ContactRelationship__c cr:trigger.new) 
        {  
            for(Contact c:conrelList)
            {
            if(cr.EmailId__c !=null)
            {
            if(contactRelationCheckMap.get(cr.EmailId__c))
            {  
                
                contact connn=new contact();
                connn.Opt_out_status__c=true; 
                connn.opt_out_transaction__c=system.today();
                connn.id=c.Id;
                conlist.add(connn);
                system.debug(c);
                system.debug(connn);
                system.debug('data available');
                system.debug(contactRelationCheckMap);
                } 
            }
            }
        }
         system.debug('conlistconlistconlistconlist');
         system.debug(conlist);
        // valfound.put(cr.Email,cr);
    } 
     // update conlist;
      //System.debug(conlist);
      
    }
    
    try
   {
    system.debug('data available');
    system.debug(conlist);
    update conlist;
    }
    catch(system.NullPointerException e)
    {
        system.debug('data not available');
    }  
}    

 
User-added image


@isTest(SeeAllData=true)
public class emailmatchtestclass 
{
    static testMethod void emilmatch(){
        ContactRelationship__c condata=new ContactRelationship__c();
        condata.Name='divya';
        condata.EmailId__c='update234@gmail.com';
        
        insert condata;
        Contact con=new Contact();
        con.Id=condata.ListofContacts__c;
        con.LastName='divya223';
        con.Email='update234@gmail.com';
        con.Email=condata.EmailId__c;
        system.assertEquals(condata.EmailId__c, 'update234@gmail.com');
        con.Opt_out_status__c=true;
        system.assertEquals(condata.EmailId__c, con.Email);
        test.startTest();
        insert con;
        test.stopTest();
        
        
        
    }
}
Apex class:
public class Deactiveusercls {
@future    
public static void processAsync() {
    
     DateTime dt = DateTime.now();
        String dateString = dt.format().replace('/',' ');
       System.debug(dateString);
      String[] strDTDivided = dateString.split(' ');
        system.debug('strDTDivided'+strDTDivided);
       string month = strDTDivided.get(0);
       system.debug('month'+month);
      string day = strDTDivided.get(1).replace(',', '');
       system.debug('day'+day);
      string year = strDTDivided.get(2);
      system.debug('year'+year);

      string hour = strDTDivided.get(3).split(':').get(0);
      string minute = strDTDivided.get(3).split(':').get(1);
      string second = '00';
      String ampm = strDTDivided.get(4);
    /*  if (strDTDivided.get(4) == 'PM')
     {
     hour = String.ValueOf(Integer.ValueOf(hour) + 12);
     }*/
   // string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
   string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ' ' +ampm; 
    system.debug('stringDate'+stringDate);
    
    map<string, Deactiveuser__c> ObjMap = new map<string, Deactiveuser__c>();
    LIst<Deactiveuser__c> activeLst =[select id,UserName__c,Name__c,TerminateDate__c from Deactiveuser__c where TerminateDate__c =:stringDate];
    system.debug('activeLst'+activeLst);
    for(Deactiveuser__c obj: activeLst)
    {
        if (obj.UserName__c != Null)
        {
            ObjMap.put(obj.UserName__c, obj);
        }
    }
    System.debug('ObjMap'+ObjMap);
    List<User> users = [SELECT isActive,email,Username,Alias FROM User WHERE Alias IN :ObjMap.KeySet()  and  isActive = true];
    system.debug('users'+users);
    List<User> userUpdateList = new List<User>();
   // String dateFormat = 'yyyy-MM-dd\'T\'HH:mm:ss\'Z\''.replace('\'T\'',' ').replace('\'Z\'','');
    for(User u: users)
    {
        u.isActive = false;
        userUpdateList.add(u);
   }
    if(userUpdateList.size() > 0)
    {
        update userUpdateList;
        system.debug('userUpdateList'+userUpdateList);
    }  
}   
}

Test Class:

@isTest
public class DeactiveuserclsTest {
 static testMethod void deactive() {
   
     
     
      Profile pf= [Select Id from profile where Name='System Administrator']; 
     User u = new User(
     ProfileId = pf.Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'test',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
  
     );
     insert u;
     
     Deactiveuser__c d=new Deactiveuser__c();
     d.Name__c ='testUser';
     d.UserName__c = 'test';
     d.TerminateDate__c = '2022-4-25 6 PM';
     insert d;
    
     u.IsActive = false;
    // update u;
    
    Test.startTest();
     Deactiveusercls.processAsync();
      Test.stopTest(); 
     
//   User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
        System.assertEquals(false, u.IsActive);  
     }
}
The failing line is like Datetime.parse('3-31-2022 12:00 AM')
The exception is:
System.TypeException: Invalid date/time: 3-31-2022 12:00 AM
However, if I run this line below in anonymous Apex in dev console, there is no exception.
System.debug('parse'+Datetime.parse('3-31-2022 12:00 AM'));
execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.emailmatchdatacontact: line  29,column 1

trigger emailmatchdatacontact on ContactRelationship__c (before insert,before update) 
{
    List<ContactRelationship__c> con= new List<ContactRelationship__c>();
    List<Contact> conlist=new List<Contact>();
    Set<String> valtocheck=new Set<String>();
    Map<String,Contact> valfound=new Map<String,Contact>();
    for(ContactRelationship__c cr:trigger.new)
    {
      if(!string.isBlank(cr.EmailId__c)) valToCheck.add(cr.EmailId__c);
    }
    system.debug(valToCheck);
    Map<String, boolean> contactRelationCheckMap = new Map<String, boolean>();
    if(valtocheck != null)
    {
    List<Contact> conrelList = new List<Contact>();
    conrelList = [select id,Email from Contact where Email IN:valtocheck];
    if(conrelList.size()>0)
    {
    for(Contact c:conrelList)
    {   
        contactRelationCheckMap.put(c.Email, true);
    }
        for(ContactRelationship__c cr:trigger.new) 
        {  
            for(Contact c:conrelList)
            {
            if(cr.EmailId__c !=null)
            {
            if(contactRelationCheckMap.get(cr.EmailId__c))
            {  
                
                contact connn=new contact();
                connn.Opt_out_status__c=true; 
                connn.opt_out_transaction__c=system.today();
                connn.id=c.Id;
                conlist.add(connn);
                system.debug(c);
                system.debug(connn);
                system.debug('data available');
                system.debug(contactRelationCheckMap);
                } 
            }
            }
        }
         system.debug('conlistconlistconlistconlist');
         system.debug(conlist);
        // valfound.put(cr.Email,cr);
    } 
     // update conlist;
      //System.debug(conlist);
      
    }
    
    try
   {
    system.debug('data available');
    system.debug(conlist);
    update conlist;
    }
    catch(system.NullPointerException e)
    {
        system.debug('data not available');
    }  
}