Thursday, October 17, 2013

Create or Update Contact When User Created or Updated

Create or Update Contact When User Created or Updated:
This example examples how to create a non related object records depends on dml action.

public class CONCreate

{
   @future
  public static void myMethod(Set<ID> usrids) 
  {
  
       User[] candsFromTrigger = [Select ID,Division,FirstName,LastName,Email,Department,Title,
                     Phone,MobilePhone,Street,City,State,Country,PostalCode,IsActive From User Where ID IN:usrids];
                     
        List<Contact> conList = new List<Contact>();
     
        
        
      
  
    ///candsFromTrigger = [Select ID,Division,FirstName,LastName,Email,Department,Title,
    //Phone,MobilePhone,Street,City,State,Country,PostalCode From User Where ID IN:uids];
    //TODO: Select the Recruiting record from the database and assign to an object called candAcct from the sObject Account class
     

    //Instantiate a Contact list object from the List class called conList

    
    
    //Declare a For list loop to loop through the input parameter list candsFromTrigger with an iterationvariable called currentCandidate
   
    
    
     for(User currentCandidate: candsFromTrigger){
    
    Account candAcct = [SELECT a.Id FROM Account a 
                        WHERE a.Name =:currentCandidate.Division];
      //Instantiate an object called con from the sObject class contact
      Contact con = new Contact();
      //con = [select id,FirstName,LastName,contact_status__c,Email from contact where email  =: currentCandidate.email  limit 1]; 
      //TODO: Set the attribute AccountID of the con object to the value of the Id attribute of the candAcct object
      con.AccountId = candAcct.Id;

      //Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object

      con.FirstName = currentCandidate.FirstName; 

      //Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object

      con.LastName = currentCandidate.LastName;

      //Set the attribute Email of the con object to the value of the Email__c attribute of the currentCandidate object

      con.Email = currentCandidate.Email;
      
      con.Department = currentCandidate.Department;
      
      con.Title = currentCandidate.Title;
      
      con.Phone = currentCandidate.Phone;
      
      con.MobilePhone = currentCandidate.MobilePhone;
      
      con.MailingStreet = currentCandidate.Street;
      
      con.MailingCity = currentCandidate.City;
      
      con.MailingState = currentCandidate.State;
      
      con.MailingCountry = currentCandidate.Country;
      
      con.MailingPostalCode = currentCandidate.PostalCode;
      
      con.Contact_Type__c = 'SFC User';
      
      
      con.Contact_Status__c = 'Active';
      
      
      

      //Add the con object to the conList

      conList.add(con);    
      
       }
       insert conList;   

  }

}


public class CONUpdate { @future public static void myMethod(Set<ID> usrids) { User[] candsFromTrigger = [Select ID,Division,FirstName,LastName,Email,Department,Title, Phone,MobilePhone,Street,City,State,Country,PostalCode,IsActive From User Where ID IN:usrids]; List<Contact> conList = new List<Contact>(); List<Contact> conList1 = new List<Contact>(); ///candsFromTrigger = [Select ID,Division,FirstName,LastName,Email,Department,Title, //Phone,MobilePhone,Street,City,State,Country,PostalCode From User Where ID IN:uids]; //TODO: Select the Recruiting record from the database and assign to an object called candAcct from the sObject Account class //Instantiate a Contact list object from the List class called conList //Declare a For list loop to loop through the input parameter list candsFromTrigger with an iterationvariable called currentCandidate Contact con1; for(User currentCandidate:candsFromTrigger){ Account candAcct = [SELECT a.Id FROM Account a WHERE a.Name =:currentCandidate.Division]; //Instantiate an object called con from the sObject class contact con1 = [select id,FirstName,LastName,contact_status__c,Email from contact where email =: currentCandidate.email limit 1]; If(currentCandidate.IsActive==TRUE){ //TODO: Set the attribute AccountID of the con object to the value of the Id attribute of the candAcct object con1.AccountId = candAcct.Id; //Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object con1.FirstName = currentCandidate.FirstName; //Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object con1.LastName = currentCandidate.LastName; con1.Department = currentCandidate.Department; con1.Title = currentCandidate.Title; con1.Phone = currentCandidate.Phone; con1.MobilePhone = currentCandidate.MobilePhone; con1.MailingStreet = currentCandidate.Street; con1.MailingCity = currentCandidate.City; con1.MailingState = currentCandidate.State; con1.MailingCountry = currentCandidate.Country; con1.MailingPostalCode = currentCandidate.PostalCode; //Set the attribute Email of the con object to the value of the Email__c attribute of the currentCandidate object // con1.Email = currentCandidate.Email; //con1.Contact_Type__c = 'SFC User'; con1.Contact_Status__c = 'Active'; //Add the con object to the conList // if(currentCandidate.IsActive==TRUE){ // con1.Contact_Status__c = 'Active';}else // If(currentCandidate.IsActive==FALSE){ // con1.Contact_Status__c='Inactive';} conList1.add(con1); System.Debug('After ADDED TO LEAD LIST VALUES *** : '+ conList1); } else if(currentCandidate.IsActive==FALSE) { con1.AccountId = candAcct.Id; //Set the attribute Firstname of the con object to the value of the First_Name__c attribute of the currentCandidate object con1.FirstName = currentCandidate.FirstName; //Set the attribute Lastname of the con object to the value of the Last_Name__c attribute of the currentCandidate object con1.LastName = currentCandidate.LastName; con1.Department = currentCandidate.Department; con1.Title = currentCandidate.Title; con1.Phone = currentCandidate.Phone; con1.MobilePhone = currentCandidate.MobilePhone; con1.MailingStreet = currentCandidate.Street; con1.MailingCity = currentCandidate.City; con1.MailingState = currentCandidate.State; con1.MailingCountry = currentCandidate.Country; con1.MailingPostalCode = currentCandidate.PostalCode; con1.Contact_Status__c='Inactive'; conList1.add(con1); System.Debug('After ADDED TO LEAD LIST VALUES *** : '+ conList1); } } //MyFutureClass.myMethod(conList1.Keyset()); update conList1; //Persist the conList to the database } }


Finally trigger looks like this

trigger CreateContact1 on User (after insert,after update) { Set<ID> uids = new Set<ID>(); for(User U : Trigger.New) { uids.add(U.ID); } if(Trigger.IsInsert) { CONCreate.myMethod(uids); } else if(Trigger.IsUpdate) { CONUpdate.myMethod(uids); } //TODO: Instantiate an object called cc from the class CreateContactFromCan //CreateContactFromCan1 cc = new CreateContactFromCan1(); //TODO: Inovke the method createContact and send a List of Candidates as an input parameter //cc.createContact1(uids); //CreateContactFromCan1.createContact1(uids); }

and test class is

@isTest private class TestTriggersCreateContactFromCan1 { static testMethod void TestCreateContactFromCan1() { account acc=new account(Name='CCS',Region__c='10',Industry='Aero'); insert acc; User u =new User(FirstName = 'TestFirstName' , TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', ProfileId='00e30000001YGorAAG',LanguageLocaleKey='en_US' , LastName = 'TestLastName', Alias = 'Test789' , Email = 'Test789@gmail.com' , UserName = 'Test559@gmail.com',CommunityNickname = 'TestNickName', Division='CCS'); try{ insert u; }catch (exception e){ system.debug('exception:'+e); } u.LastName='Final456'; try{ update u; }catch(exception e){ system.debug('exception:'+e); } u.IsActive=False; try{ update u; }catch(exception e){ system.debug('exception:'+e); } } }





No comments:

Post a Comment