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
civiccivic 

Inserting the records into xxx object

I have the class with the following code..

My issue is now i need to write a method or an SOQL query which will retrieve all the the subscription members of an subscription and insert them into a campaign member list.

As i am having a hardtime doing this pls help me with the code.

I believe we need to replicate the setsubscription method from the following code, but  not sure how we can achieve this..

Please help me with the code for the method or query.

 

 

Publicl class xxx
{
/***** VARIABLE *****/
  private string cId;
  public string strFirstName{get;set;}
  public string strLastName{get;set;}
  public List<RecordSet> lstRSet = new List<RecordSet>();
  private List<RecordSet> lstRSetBackup;
  public Boolean chkStatus{get;set;}
  public Map<String,Boolean> checkOldSubscribeVal=new Map<String,Boolean>();
/*** END VARIABLE ***/

/***** PROPERTY *****/
      List<Id> setSubscription = new List<Id>();
    if(!lstSGS.isEmpty())
      for(Subscription_Group_Subscription__c sgs: lstSGS)
        setSubscription.add(sgs.Subscription__r.Id);
   
    List<Subscription_Member__c> lstSM = [SELECT s.Id, s.Subscription__c,s.Subscription_Unsubscribed_Date__c,s.Subscribed__c, s.Contact__c FROM Subscription_Member__c s WHERE s.Contact__c =: Apexpages.Currentpage().getParameters().get('cId') ];     
   
    if(!lstSGS.isEmpty())
    {
      if(!lstRSet.isEmpty())
        lstRSet.clear();
      for(Subscription_Group_Subscription__c sgs: lstSGS)
      {

          RecordSet rs = new RecordSet();
          rs.SubGroup = sgs.Subscription_Group__r.Name;
          rs.SubGroupId = sgs.Subscription_Group__r.Id;
          rs.SubType = sgs.Subscription__r.Subscription_Type__r.Name;
          rs.SubTypeId = sgs.Subscription__r.Subscription_Type__r.Id;
          rs.SubName = sgs.Subscription__r.Name;
          rs.SubId = sgs.Subscription__r.Id;
          for(Subscription_Member__c sm: lstSM)
          {
            if(sm.Subscription__c == sgs.Subscription__r.Id)
            {
              rs.Subscribed = sm.Subscribed__c;
              rs.subUnsubDate=sm.Subscription_Unsubscribed_Date__c;
              rs.SubMemId = sm.Id;
            }
          }
          lstRSet.add(rs);
      }
      lstRSetBackup = new List<RecordSet>();
      lstRSetBackup = lstRSet.clone();
      checkOldSubscribeVal=new Map<String,Boolean>();
      for(RecordSet rec:lstRSetBackup)
      {
        checkOldSubscribeVal.put(rec.SubId,rec.Subscribed);
      }
      return lstRSet;
    }
    return null;
    }
 
  public void SetSubscriptions(List<RecordSet> value)
  {
      lstRSet = value;
  }
/*** END PROPERTY ****/
  public string getSubscriptionGroupId(string contactId)
  {
     Contact con=[Select Id,Email,FirstName,LastName,Subscription_Group_Derived_Id__c from Contact Where Id=:contactID];
     return con.Subscription_Group_Derived_Id__c;
  }
 
  public PageReference setSubscribtion()
  {
    Map<String, Subscription_Member__c> mapChanges = new Map<String, Subscription_Member__c>();
    System.Debug('lstRSet'+lstRSet);
    /*for(RecordSet rs : lstRSet)
    {
        mapChanges.put(rs.SubId, rs.Subscribed);
        System.Debug('mapChangesIterator'+mapChanges);
    }*/
  
    List<Subscription_Member__c> lstRecInsert = new List<Subscription_Member__c>();
    List<Subscription_Member__c> lstRecUpdate = new List<Subscription_Member__c>();
    List<Subscription_Member__c> lstRec=[Select Id,Subscription__c,Subscribed__c,Subscription_Unsubscribed_Date__c,Contact__c,Composite_Key_ExternalId_SId_CId_OR_LId__c from Subscription_Member__c where Contact__c=:ApexPages.currentPage().getParameters().get('cId')];
    System.Debug('lstRec'+lstRec);
    Contact con=[Select Id,Email,FirstName,LastName from Contact Where Id=:ApexPages.currentPage().getParameters().get('cId') LIMIT 1];

        System.Debug('lstRecUpdate'+lstRecUpdate);
    System.Debug('lstRecInsert'+lstRecInsert);
    List<Contact> lstContact=new List<Contact>();
    Contact conRecord;
    if(ApexPages.currentPage().getParameters().get('cId')!=null)
    {
       conRecord=[Select Id,FirstName,Email,LastName,Privacy_Policy_Sent_Date__c,Privacy_Policy_Web_Ack_Date__c from Contact Where Id=:ApexPages.currentPage().getParameters().get('cId')];
    }
    if(!lstRecUpdate.isEmpty())
    {
        Database.SaveResult[] sr = Database.update(lstRecUpdate,false);
        for(Database.SaveResult srRec : sr)
        {
            if(srRec.isSuccess())
            {
              System.Debug('Entered');
              chkStatus=true;
              
            }
            else
            {
                   Database.Error err = srRec.getErrors()[0];
                   ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,err.getMessage());
                   ApexPages.addMessage(msg);
                   return null;
              
            }
        }
    }
    if(!lstRecInsert.isEmpty())
    {
        Database.SaveResult[] sr = Database.Insert(lstRecInsert,false);
        for(Database.SaveResult srRec : sr)
        {
            if(srRec.isSuccess())
            {
               System.Debug('Entered2');
               chkStatus=true;
              
            }
            else
            {
                   Database.Error err = srRec.getErrors()[0];
                   ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,err.getMessage());
                   ApexPages.addMessage(msg);
                   return null;
              
            }
        }
    }
    if(conRecord.Privacy_Policy_Sent_Date__c==null && conRecord.Privacy_Policy_Web_Ack_Date__c==null && chkStatus==true)
    {
            System.Debug('Entered3');
            Contact conData=new Contact(Id=conRecord.Id);
            conData.Privacy_Policy_Sent_Date__c=System.now();
            if(lstContact.size()==0)
            {
                lstContact.add(conData);
            }
            List<Privacy_policy_Email_Template__c> tempName = Privacy_policy_Email_Template__c.getall().values();
            for(EmailTemplate etemp : [select id from EmailTemplate where Name=:tempName[0].Name and isactive=TRUE ])
            {
                messaging.Singleemailmessage mail=new messaging.Singleemailmessage();
                mail.setSenderDisplayName(conRecord.FirstName +' '+conRecord.LastName);
                mail.setReplyTo(conRecord.Email);
                mail.setTargetObjectId(conRecord.Id);
                mail.setTemplateId(etemp.id);
                messaging.sendEmail(new messaging.Singleemailmessage[]{mail});
            }
    }

    System.Debug('lstContact'+lstContact);
    if(!lstContact.isEmpty())
    {
       update lstContact;
    }
    PageReference contPage = new PageReference('/'+ApexPages.currentPage().getParameters().get('cid'));
    return contPage;
   
  }//End Func Unsubscribe
  public PageReference cancelRecord()
  {
    PageReference contPage = new PageReference('/'+ApexPages.currentPage().getParameters().get('cid'));
    return contPage;
  }

/*** END FUNCTION ***/

/***** WRAPPER *****/
public class Recordset