Wednesday, December 24, 2014

How To Use The @Future Annotation With Exmple.....

Scanrio:Creating User In Salesforce Based Some picklist values is Added to particular Chatter Group Based upon Picklist value added to group.


=================Trigger===============================








After inserting/updating user Record






trigger AddChatterGroup on User (after insert,after update) {
    if(Trigger.Isinsert || Trigger.Isupdate){
        for(User uids:Trigger.new){
           if(uids.nnr__c=='knr'){
               id userId = uids.id;
           asyncApex.addUserToGroup(userId);

           }
        }
  }    
}


=============Apex Class======================

global class AsyncApex {
      @future
      public static void AddUserToGroup(id userId ) {
          try {
              List<CollaborationGroupMember> CG= new List<CollaborationGroupMember>();                     
              Id cgID = [ Select Id
                          FROM CollaborationGroup
                          WHERE Name ='Nnrgroup' LIMIT 1 ].ID;
             CG.add(new CollaborationGroupMember (CollaborationGroupId = cgID, MemberId = userId));  
             insert CG;
          } catch (QueryException qe) {
              System.debug('QueryException in AsyncApex.AddUserToGroup is :' + qe); 
          } catch (Exception ex) {
              System.debug('Exception in AsyncApex.AddUserToGroup is :' + ex);
          }   
      }
  }

No comments:

Post a Comment