function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Priyakaran1320Priyakaran1320 

apex code logics

hey guys i am workin on a code i need to make some adding in i tht if the subscriber is add to a subcription i want tht he doesnt get more then one document. like a subscriber is subcribered to one particular document of that particular type.

like in simple there are a,b,c,d,e these are documents 
so if subscriber 1 is subscribing for a,b,c he can 
but if he is subscribing for 2A,2b 2c den he cant soo we have to make sure tht each subcriber is give one particular doc once and there is no DUPLICATION of doc sent to any subcriber 

  

 

here is the particular code 

@RestResource(urlMapping='/upsertSubscription/*')
global with sharing class upsertSubscription
{
    //Method to add a new subscription for a subscriber. 
    //HTTP Post annotation..... 
    @HttpPost
    global static String addSubscriptions(String name,String email, String docId,String extID,String url,String mode,String subscriptionType,String contentType,Date lastSent)
    {
      Subscriber__c subscriberInstance= new Subscriber__c();
      List<Subscribers__c> subscriberList=[SELECT name,id,Email_Address__c  FROM Subscribers__c WHERE Email_Address__c=:email limit 1];
      system.debug('subscriber----->>'+SubscriberList);
      Document_Subscription__c docSubscription=new Document_Subscription__c();
      List<Document_Subscription__c> DocList=new List<Document_Subscription__c>();
      //Check if the Subscriber exists
      if(SubscriberList.size()==0)
          system.debug(SubscriberList);
      //Insert subscriber;            
      docSubscription.Document_Name__c = name;            
      docSubscription.Document_Id__c = docId;            
      docSubscription.Document_Source_URL__c = url;
      docSubscription.Content_Type__c = contentType;
      docSubscription.Last_Sent__c = lastSent;
      docSubscription.Mode__c = mode;
      docSubscription.Subscription_Type__c = subscriptionType;
      //Loop for each Subscriber
      for(Subscribers__c subscriberObj:subscriberList)
      {
          docSubscription.Subscriber__c=subscriberObj.Id;
          docSubscription.External_ID__c=subscriberObj.name+'-'+docId;
          DocList.add(docSubscription);
      }    
      insert DocList;
      return docSubscription.Id;
    }
       
    //Method to delete subscriptions for a particular subscriber.
    @HttpDelete 
    global static String deleteSelectedSubscriptions() 
    {
      RestRequest req = RestContext.request;        
      String emailAddress = req.params.get('emailAddress');  
      String DocumentId = req.params.get('DocumentId'); 
      Subscriber__c Subscriber= [ Select ID, Name from Subscriber__c where Email_Address__c= :emailAddress LIMIT 1];          
      List<Document_Subscription__c> Subscriptions= [SELECT Id,Name,Subscriber__c, Document_Id__c from Document_Subscription__c 
                                           WHERE Subscriber__c =:Subscriber.name AND Document_Id__c=:DocumentId];
      delete Subscriptions;         
      return Subscriptions[0].Id;  
    }
  
    global static String deleteallSubscriptions()
    {   
      RestRequest req = RestContext.request;        
      String emailAddress = req.params.get('emailAddress');         
      Subscriber__c Subscriber= [ Select ID, Name from Subscriber__c where Email_Address__c= :emailAddress LIMIT 1];          
      List<Document_Subscription__c> Subscriptions= [SELECT Id,Name,Subscriber__c, Document_Id__c from Document_Subscription__c 
                                           WHERE Subscriber__c =:Subscriber.name];
      delete Subscriptions;             
      return 'All Subscriptions Deleted';
    }
    /*         
    @HttpGet
    //method to return list of subscriptions for particular selected subscriber.......         
    global static List<Document_Subscription__c> listSubscriptions()
    {
        RestRequest req = RestContext.request;
        String mailId= req.params.get('email');        
        system.debug('mailId----->>>>'+mailId);
        Subscriber__c sub=[SELECT id, Name FROM Subscriber__c WHERE Email_Address__c=:mailId];
        system.debug('sub----->>>>>>>>>>>>'+sub.Name);
        List<Document_Subscription__c> subscriptions= [SELECT Id,Name,Document_Id__c,Document_Name__c,Content_Type__c,Last_Sent__c,
                                                        Document_Source_URL__c from Document_Subscription__c WHERE Subscriber__c =:sub.name];                       
        return subscriptions;  
    }
    //method to return list of subscribers for one subscription....       
    global static List<Document_Subscription__c> listSubscribers()
    {
        RestRequest req = RestContext.request;
        String docId= req.params.get('docId');        
        system.debug('docId----->>>>'+docId);
        List<Document_Subscription__c> subscribers= [SELECT Id,Name,Subscriber__c ,Document_Name__c,Content_Type__c,Last_Sent__c,
        Document_Source_URL__c from Document_Subscription__c WHERE Document_Id__c =:docId]; 
        System.debug('subscribers----->>>'+subscribers);                      
        return subscribers;  
     } */    
 }