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
Kondal Rao learnerKondal Rao learner 

Hi experts regarding save button

Hi ,

 

My requirmeent is that there is deal page so we need to create button name called save , when user click on that button what ever data he give it has to save and when user click on the program asscoation it has go deal probram setup page and there is one more button called  program associton when user click on that it has throw an error .

 

This is the code:

 

public class VFC_ProgramSetup{

    public Deal__c dealRecord;
    public List<ProgramWrapper> lstProgramWrapper{get;set;}
    public id DealId2;
    public class ProgramWrapper{
        public Campaign ProgramRecord{get;set;}
        public boolean checkBox{get;set;}
    }

    public VFC_ProgramSetup(){
        PageReference pageRef = ApexPages.currentPage();
        Id DealId = pageRef.getParameters().get('DealId');
        Id CustId = pageRef.getParameters().get('CustId');
        
        DealId2 = DealId;
        //logged in partner details
        User loggedInUser = [select id,name,AccountId from User where id=:UserInfo.getUserId()];
        Account partnerAcc= [select id,Partner_Specializations__c from Account where id = :loggedInUser.AccountId];
       
        //details of End Customer
        Account endCustomer= [select id,Segment__c,Industry__c from Account where id = :CustId];
        
        lstProgramWrapper = new List<ProgramWrapper>();
        Deal__c dealRec = [select Quantity__c, Value__c,id, Product_Category__c from Deal__c where id=:DealId];
        system.debug('####Segment:'+endCustomer.Segment__c); 
        system.debug('####Industry:'+endCustomer.Industry__c);
        system.debug('####Partner_Specializations:'+partnerAcc.Partner_Specializations__c);
        
        string query = 'select id,name, Program_Type__c, Expiration_Date__c, Expiration_duration__c, Quantity__c, Value__c from Campaign';
        String[] opts =  partnerAcc.Partner_Specializations__c.split(';');
        String[] optsProdCat =  dealRec.Product_Category__c.split(';');
        
        //for Partner's Specializations
        boolean hasCondition = false;
        if(!opts.isempty()) {
          hasCondition = true;
          query += '  where Partner_Specializations__c includes (\''+opts[0]+'\'';
          opts.remove(0);
        }
        while(!opts.isempty()) {
          query += ',\''+opts[0]+'\'';
          opts.remove(0);
        }
        if(hasCondition)
          query += ')';

        //for Product Category on Deal
        boolean hasConditionProdCat = false;
        if(!optsProdCat.isempty()) {
          hasConditionProdCat = true;
          query += '  and Product_Category__c includes (\''+optsProdCat[0]+'\'';
          optsProdCat.remove(0);
        }
        while(!optsProdCat.isempty()) {
          query += ',\''+optsProdCat[0]+'\'';
          optsProdCat.remove(0);
        }
        if(hasConditionProdCat)
          query += ')';
          
        //for End Customer's Segment and Industry
        query += ' and Segment__c includes (\''+endCustomer.Segment__c+'\') and Industry__c includes (\''+endCustomer.Industry__c+'\')';
        
        //for Quantity and Value on Deal
        query += ' and Quantity__c <='+dealRec.Quantity__c + ' and Value__c <='+dealRec.Value__c;
        
        system.debug('#########Final Query:'+query);
        List<Campaign> lstEligiblePrograms = Database.query(query);
        
        //fetching the associated programs
        List<String> lstCampaignId = new List<String>();
        for(Deal_Program_Association__c AssociatedProgram :[select id,Campaign__c  from Deal_Program_Association__c where Deal__c=:DealId])
        {
            lstCampaignId.add(AssociatedProgram.Campaign__c);
        }
        
        List<Campaign> lstAssociatedPrograms = [select id from Campaign where id in :lstCampaignId];
        List<Campaign> lstFinalEligiblePrograms = new List<Campaign>();
        boolean flag=false;
        for(Campaign camp:lstEligiblePrograms)
        {
            for(Campaign assCampaign: lstAssociatedPrograms)
            {
                 if(assCampaign.id == camp.id)
                 {
                     flag = true;
                 }   
            }
            if(flag == false)
            {
                lstFinalEligiblePrograms.add(camp);
            }
            flag=false;
        }    

        for(Campaign camp:lstFinalEligiblePrograms)
        {
            ProgramWrapper prog = new ProgramWrapper();
            prog.ProgramRecord = camp;
            prog.checkBox  = false;
            lstProgramWrapper.add(prog);
        }
        if(lstProgramWrapper.size()<1)
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No Eligible Programs found'));
        }
    }
    
    public VFC_ProgramSetup(ApexPages.StandardController controller){
        this.dealRecord = (Deal__c)controller.getRecord();
    }
        
    public PageReference proceed() 
    {
       try{
           database.upsert(dealRecord);
       }catch(Exception e)
       {
           return null;
       }
       
       PageReference pageRef = Page.SelectProgram;
       pageRef.getParameters().put('DealId', dealRecord.id);
       pageRef.getParameters().put('CustId', dealRecord.End_Customer__c);
       //pageRef.getParameters().put('PartnerId', dealRecord.End_Customer__c);
       //system.debug('##USER Details:'+UserInfo.getUserId());
       return pageRef;
    }
    
    public PageReference SaveProgram() 
    {
       PageReference pageRef = ApexPages.currentPage();
       Id DealId = pageRef.getParameters().get('DealId');
       
       List<ProgramWrapper> lstSelectedPrograms = new List<ProgramWrapper>();
       List<Deal_Program_Association__c> lstDealProgramAssn = new List<Deal_Program_Association__c>();
       for(ProgramWrapper pg:lstProgramWrapper)
       {
           if(pg.checkbox == true){
               lstSelectedPrograms.add(pg);
           }
       }
       
       if(lstSelectedPrograms.size()>0){
           string tempProgramType = lstSelectedPrograms[0].ProgramRecord.Program_Type__c;
           List<Deal_Program_Association__c> lstExistingDPA = [select id,Campaign__r.Program_Type__c  from Deal_Program_Association__c where Deal__c=:DealId];

           for(ProgramWrapper pg:lstSelectedPrograms)
           {
               if(tempProgramType != pg.ProgramRecord.Program_Type__c)
               {
                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Programs of similar type can be stacked'));
                   return null;
               }
               else{
                   Deal_Program_Association__c dpa = new Deal_Program_Association__c();
                   dpa.Deal__c = DealId;
                   dpa.Campaign__c = pg.ProgramRecord.id;
                   //dpa.Deal_Registration_Status__c = 'Draft';
                   lstDealProgramAssn.add(dpa);
                   
               }
           }
           if(lstExistingDPA.size()>0 && lstExistingDPA[0].Campaign__r.Program_Type__c != tempProgramType){
                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Programs of different type already exists for this Deal'));
                   return null;
           }
           else if(lstExistingDPA.size()+lstDealProgramAssn.size()>3)
           {
               ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'No more than 3 programs of similar type can be stacked'));
               return null;
           }
           else{
               database.insert(lstDealProgramAssn);
           }
       }
       String redirectURL = URL.getSalesforceBaseUrl().toExternalForm()+'/'+DealId;
       PageReference pageRef2 = new PageReference(redirectURL);
       return pageRef2;
    }
    
    public PageReference cancelProgram() 
    {
       /*//try{
       //    if(dealRecord==null){
               Deal__c deal = new Deal__c(id=DealId2);
               database.delete(deal);
          
       //}catch(Exception e)
       //{
         //  return null;
      // }*/
       PageReference pageRef = ApexPages.currentPage();
       Id DealId = pageRef.getParameters().get('DealId');
       //String redirectURL = URL.getSalesforceBaseUrl().toExternalForm()+'/home/home.jsp';
       String redirectURL = URL.getSalesforceBaseUrl().toExternalForm()+'/'+DealId;
       PageReference pageRef2 = new PageReference(redirectURL);
       return pageRef2;
    }
    
}