需要通过在商机详细信息页面上单击生成许可证按钮来在多个许可证记录上创建多个商机产品列表

时间:2019-02-19 09:24:19

标签: salesforce apex visualforce

我需要创建许可证记录,其许可证名称是与商机对象相关的自动编号,开始日期,结束日期。在机会中,​​我具有许可证的开始日期和订购期限(以月为单位),因此当我同时填写这些字段和机会时,就会具有产品列表。在机会详细信息页面中,我创建了“生成许可证”按钮,因此当我单击它时,它会自动执行所有可能的计算并生成许可证记录。请记住我要创建的许可证不超过1年,因此如果机会为24个月,则将创建两个许可证,如果机会为18个月,则还将创建两个许可证。

我的问题是,当我们单击“生成许可证”按钮时,如何在每个许可证记录详细信息页面上获得机会产品列表?

顶点代码:

public class GetLicense {
private final Opportunity o;
public GetLicense(ApexPages.StandardController con){
    this.o = (Opportunity)con.getRecord();        
}    
public static boolean Recursion=true;
public PageReference  Licensedetail(){
    Id theId = ApexPages.currentPage().getParameters().get('id');
    System.debug('@@'+theId);       
    List<License__c> lics=new List<License__c>();
    List<License_Line__c> llclist =new List<License_Line__c>();
    List<Opportunity> oplist=[Select Id,Name,License_Start_Date__c,Subscription_Term_inmonths__c from Opportunity where Id =:theId];        
    List<OpportunityLineItem> olIlist=[SELECT Product2.Name FROM OpportunityLineItem WHERE OpportunityId =:theId];
    for(Opportunity o:oplist){  
        //n is number of Licenses
        Integer n=Math.ceil(o.Subscription_Term_inmonths__c/12).intValue();            
        Date date1=o.License_Start_Date__c;         
        for(Integer i=1;i<=n;i++){
        if(theId != null   && date1!=null ){                    
            License__c li = new License__c();    
            License_Line__c llc =new License_Line__c();           
            li.Opportunity__c=o.Id;
            li.Start_Date__c=date1; 
            //date1=date1-1;
           li.End_Date__c= date1.addmonths(12)-1;                
            if(i==n){
                 li.End_Date__c=date1.addMonths(Math.ceil(o.Subscription_Term_inmonths__c-((n-1)*12)).intValue())-1;                   
            }
             date1=li.End_Date__c+1;               
            lics.add(li);

            //
            //ll.Product__c=o.Pricebook2Id;
          //llclist.add(ll);
        }
        }


    }          
    insert lics;
    for(OpportunityLineItem objQ : olIlist){
              License_Line__c ll =new License_Line__c();
        License__c li = new License__c();
        ll.License__c=li.Id;
                ll.Product__c=objQ.Product2Id ;
        llclist.add(ll);
            }
    if(!llclist.isEmpty()){
            insert llclist;
        }
  //  insert llclist;
    Recursion = false;
    // Redirect the user back to the original page
    PageReference pageRef = new PageReference('/' + theId);
    pageRef.setRedirect(true);
    return pageRef;
}
}

Visualforce页面:

 <apex:page standardController="Opportunity" extensions="GetLicense" action="{!Licensedetail}" >
<apex:sectionHeader title="Auto-Running Apex Code"/>
<apex:outputPanel >
    You tried calling Apex Code from a button.  If you see this page, 
    something went wrong.  You should have 
    been redirected back to the record you clicked the button from.
</apex:outputPanel>
</apex:page>

0 个答案:

没有答案