• Harsh 007
  • NEWBIE
  • 30 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 9
    Replies
public class OpportunityPdfController {
		
	
    
    @future(callout=true)
      
    public static void pdfGenration(Map<Id, Id> oppIdWithAccId){  
        Set<Id> OppIds=oppIdWithAccId.keySet();
        System.debug('OPPIDS'+ OppIds);

        Map<Id, ContentVersion> contentVersionMap = createContentVersion(OppIds, oppIdWithAccId);
        createContentDocumentLink(contentVersionMap);       
        
    }

    private static Map<Id, ContentVersion> createContentVersion(Set<Id> OppIds, Map<Id, Id> oppIdWithAccId){

        System.debug('OPPIDS2'+ OppIds);
        Map<Id, ContentVersion> contentVersionMap = new Map<Id, ContentVersion>(); 
		Blob body;        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.EmailFileAttachment> attachList = new List<Messaging.EmailFileAttachment>();
        List<String> sendTo= new List<String>();
        
		List<Contact>  conList=[SELECT Id, AccountId, Email FROM Contact WHERE AccountId IN: oppIdWithAccId.values() AND Email!=Null];
       
        system.debug('conList'+conList );
        
       Map<Id, String> oppIdWithEmail= new Map<Id, String>();  
         
        for(Id OppId: OppIds)
        {		
            PageReference pdf = new PageReference('/apex/InvoicePDF');
            pdf.getParameters().put('Id',OppId);     
            
            ContentVersion cv=new ContentVersion();            
            try{   
                
                body=pdf.getContentAsPDF();
                System.debug('body : ' + body);
                
            }
            catch (Exception e) {
                
                body = Blob.valueOf('Text');
                System.debug(e.getMessage());
            }                     
            cv.PathOnClient= 'Invoice'+'.pdf';            	
            cv.Title= 'Invoice'+' '+ Date.today();
            cv.IsMajorVersion = true;  
            cv.VersionData=body;  
            
            contentVersionMap.put(OppId, cv);    
          
        } 
        if(!contentVersionMap.Values().isEmpty()){      
        
            insert contentVersionMap.Values(); 
            for(Id oid:  contentVersionMap.keySet()){
                
                Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
                attach.setContentType('.pdf/txt');
                attach.setFileName('Invoice.pdf');
                attach.setInline(false);
                attach.Body = contentVersionMap.get(oid).VersionData; 
                attachList.add(attach);
                
            }
            For(Contact mailList: conList){               
          
                
            Messaging.SingleEmailMessage  mail= new Messaging.SingleEmailMessage();
            
            sendTo.add(mailList.Email);
                
            system.debug('EmailList'+ sendTo);
            mail.setToAddresses(sendTo);
            mail.setSubject('Product Invoice');
            mail.setHtmlBody('');
       		mail.setFileAttachments(attachList);            
            mails.add(mail);         
            
        }
          Messaging.sendEmail(mails);            
      
        }return contentVersionMap;      
          
    }

    private static void createContentDocumentLink(Map<Id, ContentVersion> idOppContentVersionMap){

        Map<Id, Id> idConDocMap = new Map<Id, Id>();
        List<ContentDocumentLink> ContentDocumentLinkList = new List<ContentDocumentLink>();

        for(ContentVersion cv : [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:idOppContentVersionMap.Values()])
        {
            idConDocMap.put(cv.Id, cv.ContentDocumentId);
        }

        if(idConDocMap.isEmpty()) return;

        for(Id OppId : idOppContentVersionMap.keySet()){

            ContentVersion contentVersion = idOppContentVersionMap.get(OppId);

            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId = OppId;           
            cdl.ContentDocumentId = idConDocMap.get(contentVersion.Id);           
            cdl.shareType = 'V';
            
            ContentDocumentLinkList.add(cdl);

        }
		if(!ContentDocumentLinkList.isEmpty()){    
       		insert ContentDocumentLinkList;            
        } 
    }   

}

 
Trigger:
trigger PDFGenrate on Opportunity (after insert, after update) {
	 
   Set<Id> OppIds= new Set<Id>();
    
    for(Opportunity opp: trigger.new){        
        if(opp.StageName=='Closed Won'){            
         OppIds.add(Opp.Id);             
        } 
       }OpportunityPdfController.pdfGenration(OppIds);      
}

Trigger Handler: 
public class OpportunityPdfController {
    
    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){
        List<Sobject> attachmentlist = new List<Sobject>();
        List<Sobject> ContentDocLink = new List<Sobject>();
        for(Id OppId: OppIds){

            Blob body;
            
                    PageReference pdf = new  PageReference('/apex/PrintPDF');
                    pdf.getParameters().put('Id',OppId);     
                  
                    ContentVersion cv=new ContentVersion();            
                    try{                
                        body=pdf.getContentAsPDF();
                    }catch (Exception e) {
                        body = Blob.valueOf('Text');
                        system.debug(e.getMessage());
                    }                     
                    cv.PathOnClient= 'Invoice'+'.pdf';            	
                    cv.Title= 'Invoice';
                    cv.IsMajorVersion = true;  
            		cv.VersionData=body;
                    attachmentlist.add(cv);     
            
        //   }     
     
     //  if(attachmentlist.size()>0){
            insert  attachmentlist;              
            
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            system.debug(conDocId);
            ContentDocumentLink cdl = New ContentDocumentLink();

            cdl.LinkedEntityId = OppId;
            
            cdl.ContentDocumentId = conDocId;
            
            cdl.shareType = 'V';
            
        ContentDocLink.add(cdl);
            insert ContentDocLink;
            
        }
        
    }
}

 
Hi Everyone I have one cusotm object "cus_project__c" and one custome filed which name is Product Family( General, Stationery and other  ) , so I want when order is actived then in cus_project __c record will be created when according to product family. For Ex: If in order we are added one product and which project family is General then project record will be created with the name of "General" and all greneral category record will be store in this project.
 
So can any one please help me for resolve this issue with the help of trigger.
I Just write the following code but the project record is not created. 
trigger CreateProject on Order (after insert, after update) {
    List<cus_project__c> prolist= new List<cus_project__c>();
    List<Id> productId = new List<Id>();
    List<Id> priceBookid = new List<Id>();
    Set<Id> orderId= new Set<Id>();
    Set<Id> orderRecId= new Set<Id>();
    
    List<Product2> productlist=[SELECT id, Family from Product2 where id IN : productId];// to get all products.
    for(Order ord: trigger.new)
    {
        if(ord.Activated__c == true && (trigger.oldMap==Null || trigger.oldMap.get(ord.id).Activated__c==false) ) 
        {  
            orderId.add(ord.id);            
           
        }      
        for(Product2 p2: productlist){
                if(p2.family=='other'){
                    if(!prName.contains('other')){
                        Cus_project__c pro1= new Cus_project__c();
                        pro1.Order__c= ord.id;
                        pro1.Name='other';
                        pro1.Start__c=system.today();
                        pro1.End__c=system.today()+80;
                        prolist.add(pro1);  
                    }                      
                }             
            } 
    } 	insert prolist;   
      list<OrderItem> orderIds =[select Product2Id, PricebookEntryId from OrderItem where OrderId in :orderId];// Find OrderProduct ids 
            for(integer i=0;i<orderIds.size();i++){ 
                productId.add(orderIds.get(i).Product2Id);  // Find productsIds
                priceBookid.add(orderIds.get(i).PricebookEntryId);  // Find PriceBooks Ids                      
            }          
         List<cus_project__c> orderRec= [SELECT ID, Name FROM cus_project__c WHERE Order__c In :orderId]; // To get all project of Order.
            List<String> prName= new List<String>();
            for(integer i=0;i<orderRec.size();i++){
                prName.add(orderRec.get(i).Name);                        
            } 
    
     
    
        }

 
public  with sharing class ProjectClass {    
     public List<Cus_Project__c> proj{get;set;}//show Order Data 
        Public Cus_Project__c pr{get;set;} 
     Public Cus_Project__c project{get;set;}
     Public Boolean isEditable {get;set;}       
        
     Public ProjectClass(){ 
          project=[Select Id, name,Order__c,Start__c, End__c,Project_Amount__c, Total_Cost__c,Status__c
                 from Cus_Project__c where id =:ApexPages.currentPage().getParameters().get('id')];         
        
         isEditable= false;     }   
    
     Public void getProject(){
        pr=[Select Id, name,Order__c,Start__c, End__c,Project_Amount__c, Total_Cost__c,Status__c
                 from Cus_Project__c where id =:ApexPages.currentPage().getParameters().get('id')];
     }    
   public PageReference Savepr(){
       
              update project;
            pageReference pnext = new PageReference('/apex/ProjectDetailPage?id='+pr.id);
            pnext.setRedirect(true);
            return pnext;                 
    }   
     public void recEdit(){
         isEditable= true;
         system.debug('Project Callaed');
    }
}

VF: 
<apex:page  controller="ProjectClass" action="{!getProject}" lightningStylesheets="true">
  <apex:form>             
      <script  type="text/javascript">
          function validate(){
             if(document.getElementById('{!$Component.form.pb1.ps1.Start__c}').value==''){
                if(document.getElementById('{!$Component.form.pb1.ps1.Status__c}').value=='In Progress'){
                        alert('Unable to update');
                }
            } else{                
                acc();               
            }       
        }
      </script>
       <apex:pageBlock rendered="true" >              
            <apex:pageBlockSection title=" Project Information" columns="2" >                
                <apex:outputField value="{!pr.Name}"/>
                <apex:outputField value="{!pr.Order__c}"/> 
                <apex:outputField value="{!pr.Start__c}"/>
                <apex:outputField value="{!pr.End__c}"/>
                <apex:outputField value="{!pr.Status__c}"/>
                <apex:outputField value="{!pr.Project_Amount__c}"/>
                <apex:outputField value="{!pr.Total_Cost__c}"/>             
            </apex:pageBlockSection>
       </apex:pageBlock>
  </apex:form>     
        <apex:form id='form'>   
              <apex:actionFunction action="{!savepr}" Name="acc"></apex:actionFunction>
    <apex:pageBlock rendered="true" id="pb1">           
            <apex:pageBlockSection title=" Project Information" columns="2" id="ps1">                
                <apex:inputField value="{!project.Name}"/>
                <apex:inputField value="{!project.Order__c}"/>
                <apex:inputField value="{!project.Start__c}" id="Start__c"/>
                <apex:inputField value="{!project.End__c}"/>
                <apex:inputField value="{!project.Status__c}" id="Status__c"/>
                <apex:inputField value="{!project.Project_Amount__c}"/>
                <apex:inputField value="{!project.Total_Cost__c}"/>             
            </apex:pageBlockSection>
         <apex:pageBlockButtons >
                <apex:commandButton value="SaveData" onclick="validate();"/>
            </apex:pageBlockButtons>
    </apex:pageBlock> 
  </apex:form>
   </apex:page>
Order Detail Page:
<apex:page standardController="Order" action="{!details}" extensions="OrderClass" lightningStylesheets="true">
    
  <apex:form >
     <script>
        function fun(){
            EditProject();
      }
    </script>
    <apex:pageBlock rendered="{!ShowpageDetailPage}">
            <apex:pageBlockSection title="Order Record Information" columns="2" >
                <apex:outputField value="{!Order.Name}"/>
                <apex:outputField value="{!Order.OrderNumber}"/>
                <apex:outputField value="{!Order.AccountId}"/>
                <apex:outputField value="{!Order.OpportunityId}"/>
                <apex:outputField value="{!Order.Type}"/>
                <apex:outputField value="{!Order.CurrencyIsoCode}"/>
                <apex:outputField value="{!Order.CompanyAuthorizedById}"/>
                <apex:outputField value="{!Order.CustomerAuthorizedDate}"/>
                <apex:outputField value="{!Order.ShipToContactId}"/>
                <apex:outputField value="{!Order.Project_Start_Date__c}"/>
                <apex:outputField value="{!Order.Project_End_Date__c}"/>
                <apex:outputField value="{!Order.Activated__c}"/>
                <apex:outputField value="{!Order.Invoice_Date__c}"/>
                <apex:outputField value="{!Order.TotalAmount}"/>
                <apex:outputField value="{!Order.Close_Date__c}"/>
                <apex:outputField value="{!Order.Description}"/>                  
            </apex:pageBlockSection>  
        <apex:pageBlockButtons >
                <apex:commandButton value="Edit"  action="{!ShowBlockMethod}"/>
            </apex:pageBlockButtons>                  
    </apex:pageBlock>
  </apex:form> 
    
    <apex:form >
    <apex:pageBlock rendered="{!ShowpageEditPage}">
            <apex:pageBlockSection title="Order Record Information" columns="2" >
                <apex:inputField value="{!Order.Name}"/>
                <apex:inputField value="{!Order.OrderNumber}"/>
                <apex:inputField value="{!Order.AccountId}"/>
                <apex:inputField value="{!Order.OpportunityId}"/>
                <apex:inputField value="{!Order.Type}"/>
                <apex:inputField value="{!Order.CurrencyIsoCode}"/>
                <apex:inputField value="{!Order.CompanyAuthorizedById}"/>
                <apex:inputField value="{!Order.CustomerAuthorizedDate}"/>
                <apex:inputField value="{!Order.ShipToContactId}"/>
                <apex:inputField value="{!Order.Project_Start_Date__c}"/>
                <apex:inputField value="{!Order.Project_End_Date__c}"/>
                <apex:inputField value="{!Order.Activated__c}"/>
                <apex:inputField value="{!Order.Invoice_Date__c}"/>
                <apex:inputField value="{!Order.TotalAmount}"/>
                <apex:inputField value="{!Order.Close_Date__c}"/>
                <apex:inputField value="{!Order.Description}"/>                  
            </apex:pageBlockSection>  
        <apex:pageBlockButtons >
                <apex:commandButton value="Save"  action="{!Save}"/>
            </apex:pageBlockButtons>                  
    </apex:pageBlock>
  </apex:form> 
     <apex:form >
    <apex:pageBlock >  
        <apex:actionFunction action="{!recEdit}" Name="EditProject" immediate="true"></apex:actionFunction>
        <apex:pageBlockTable title="Releted Record" value="{!releted}" var="r"> 
              <apex:column >
                <apex:outputLink value="/apex/ProjectDetailPage?id={!r.id}">Del | </apex:outputLink>            
                <apex:outputLink onclick="fun();" value="/apex/ProjectDetailPage?id={!r.id}" >Edit | </apex:outputLink>     
             </apex:column> 
            <apex:column >
                 <apex:outputLink value="/apex/ProjectDetailPage?id={!r.id}"> {!r.name}</apex:outputLink>
             </apex:column> 
             <apex:column value="{!r.Start__c}"/>
            <apex:column value="{!r.End__c}"/>
            <apex:column value="{!r.Status__c}"/>
            <apex:column value="{!r.Project_Amount__c}"/>
            <apex:column value="{!r.Total_Cost__c}"/>            
        </apex:pageBlockTable>               
    </apex:pageBlock>
       </apex:form>     
</apex:page>
Related Object VF page
<apex:page controller="ProjectClass" action="{!getProject}" lightningStylesheets="true">
  <apex:form >  
       <apex:pageBlock rendered="true" >              
            <apex:pageBlockSection title=" Project Information" columns="2" >                
                <apex:outputField value="{!pr.Name}"/>
                <apex:outputField value="{!pr.Order__c}"/>
                <apex:outputField value="{!pr.Start__c}"/>
                <apex:outputField value="{!pr.End__c}"/>
                <apex:outputField value="{!pr.Status__c}"/>
                <apex:outputField value="{!pr.Project_Amount__c}"/>
                <apex:outputField value="{!pr.Total_Cost__c}"/>             
            </apex:pageBlockSection>
       </apex:pageBlock>
  </apex:form>     
      <apex:form >  
    <apex:pageBlock rendered="isEditable">           
            <apex:pageBlockSection title=" Project Information" columns="2" >                
                <apex:inputField value="{!pr.Name}"/>
                <apex:inputField value="{!pr.Order__c}"/>
                <apex:inputField value="{!pr.Start__c}"/>
                <apex:inputField value="{!pr.End__c}"/>
                <apex:inputField value="{!pr.Status__c}"/>
                <apex:inputField value="{!pr.Project_Amount__c}"/>
                <apex:inputField value="{!pr.Total_Cost__c}"/>             
            </apex:pageBlockSection>
         <apex:pageBlockButtons >
                <apex:commandButton value="Save"  action="{!Save}"/>
            </apex:pageBlockButtons>
    </apex:pageBlock> 
  </apex:form>
   </apex:page>
public with sharing class OrderClass {
   public Order orderRecord { get; set; }
    public Boolean ShowpageEditPage {get; set;}
    public Boolean ShowpageDetailPage {get; set;}
    Public List<Cus_Project__c> releted{get;set;}
    public OrderClass (ApexPages.StandardController controller) {
        ShowpageEditPage=false;
        ShowpageDetailPage=true;    
     }     
      Public void ShowBlockMethod(){
       ShowpageEditPage =true;
      ShowpageDetailPage= false;
    }        
        Public void details(){                      // Use for Show Releted  Record  on Detailpage       
       
        releted=[Select Id, name, Order__c,Start__c, End__c,Project_Amount__c, Total_Cost__c,Status__c
                 from Cus_Project__c where Order__c =:ApexPages.currentPage().getParameters().get('id')];
    } 
}
public with sharing class ProjectClass {
    
     public List<Cus_Project__c> proj{get;set;}//show Order Data 
        Public Cus_Project__c pr{get;set;}
     Public Boolean isEditable {get;set;}
    
     Public ProjectClass(){
         isEditable= false;
     }   
    
     Public void getProject(){
        pr=[Select Id, name,Order__c,Start__c, End__c,Project_Amount__c, Total_Cost__c,Status__c
                 from Cus_Project__c where id =:ApexPages.currentPage().getParameters().get('id')];       
    }
     public PageReference Save(){
            update pr;
            PageReference pnext = new PageReference('/apex/ProjectDetailPage?id='+pr.id);
            pnext.setRedirect(true);
            return pnext;
    }    
    Public void recEdit(){
        isEditable= true;
    }
}
global class BatchOnContact implements Database.Batchable<sObject>, Database.Stateful{
    
    global Database.QueryLocator start(Database.BatchableContext bc){     
        
        return Database.getQueryLocator('select Id, name, StageName from opportunity where StageName=\'Closed Won\'');
        
    }
    global void execute(Database.BatchableContext bc, List<opportunity> Opplist){
        List<OpportunityLineItem> OList = new List<OpportunityLineItem>();
        List<Product2> prolist= [SELECT Id from Product2 Limit 10];
              For(opportunity opp:  Opplist){
            for(Integer i=0; i<10; i++){
               OpportunityLineItem op= new OpportunityLineItem(opportunityId=opp.id, Product2Id=prolist.get(i).id,Quantity =1, TotalPrice=1000);
                OList.add(op);                                                               
            }  opp.Pricebook2Id='0066F00000yUeDpQAK';            
        }update opplist;
        insert OList;      
    }
 global void finish(Database.BatchableContext bc){ }
}
public with sharing class CusOrderData {
    public List<Cus_Order__c> order{get;set;}//show Order Data 
   public Cus_Order__c orderdata{get;set;}
     public Object items{get;set;}
 
    public CusOrderData(){
        orderdata=new Cus_Order__c();
        order=[SELECT Name,Activated__c,Order_Start_Date__c,Order_End_Date__c,Invoice_Date__c,
               Project_Start_Date__c, Project_End_Date__c, Down_payment_amount__c FROM Cus_Order__c];// Show Order Data   
         items = [SELECT Id, name FROM Cus_Order__c where Id=:ApexPages.currentpage().getparameters().get('Id')];
        system.debug(items); 
    }   
   public PageReference Save(){
           upsert order;
               PageReference pnext = new PageReference('/apex/CusOrderData?id='+orderdata.id);
                pnext.setRedirect(true);
            return pnext;
    }
    public void Edit(){                    
    }
}

VF page:
<apex:page controller="CusOrderData" lightningStylesheets="true" tabStyle="Account">
    <apex:form >
        <apex:pageBlock title="Order Edit Page">
               
                <apex:pageBlockSection >                
                <apex:inputField value="{!orderdata.Name}" />
                <apex:inputField value="{!orderdata.Activated__c}" />    
                <apex:inputField value="{!orderdata.Invoice_Date__c}"/>
                <apex:inputField value="{!orderdata.Project_Start_Date__c}"/>
                <apex:inputField value="{!orderdata.Project_End_Date__c}"/>
                <apex:inputField value="{!orderdata.Order_Start_Date__c}"/>
                <apex:inputField value="{!orderdata.Order_End_Date__c}"/>
                <apex:inputField value="{!orderdata.Down_payment_amount__c}"/> 
                    </apex:pageBlockSection>
            
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public class UpdateMinDateHandler {
    
    Public static Boolean firstcall=false; // Stop the Recursive Calling
    Public static void triggerCall(List<Cus_Project__c> newRecordList){
        Set<Id> OrderId= new Set<Id>();
    List<Cus_Order__c> orderlist= new List<Cus_Order__c>();
    for(Cus_Project__c proj: newRecordList){
        OrderId.add(proj.Cus_Order__c);
    }   
    List<AggregateResult> resultlist= [SELECT Cus_Order__c, Min(Start__c)mindate, Max(End__c)maxdate FROM Cus_Project__c 
                                      where Cus_Order__c IN: OrderId group by Cus_Order__c ];
    List<Cus_Order__c> Updatedata=new  List<Cus_Order__c>();
    system.debug(resultlist);
    for(AggregateResult result:resultlist )
    {
      Updatedata.add(new  Cus_Order__c(Id = (Id)result.get('Cus_Order__c'),
                                      Order_Start_Date__c=(date)result.get('mindate'),
                                      Order_End_Date__c=(date)result.get('maxdate')));
     }
    update Updatedata;
    }

}
Trigger:
trigger PDFGenrate on Opportunity (after insert, after update) {
	 
   Set<Id> OppIds= new Set<Id>();
    
    for(Opportunity opp: trigger.new){        
        if(opp.StageName=='Closed Won'){            
         OppIds.add(Opp.Id);             
        } 
       }OpportunityPdfController.pdfGenration(OppIds);      
}

Trigger Handler: 
public class OpportunityPdfController {
    
    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){
        List<Sobject> attachmentlist = new List<Sobject>();
        List<Sobject> ContentDocLink = new List<Sobject>();
        for(Id OppId: OppIds){

            Blob body;
            
                    PageReference pdf = new  PageReference('/apex/PrintPDF');
                    pdf.getParameters().put('Id',OppId);     
                  
                    ContentVersion cv=new ContentVersion();            
                    try{                
                        body=pdf.getContentAsPDF();
                    }catch (Exception e) {
                        body = Blob.valueOf('Text');
                        system.debug(e.getMessage());
                    }                     
                    cv.PathOnClient= 'Invoice'+'.pdf';            	
                    cv.Title= 'Invoice';
                    cv.IsMajorVersion = true;  
            		cv.VersionData=body;
                    attachmentlist.add(cv);     
            
        //   }     
     
     //  if(attachmentlist.size()>0){
            insert  attachmentlist;              
            
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            system.debug(conDocId);
            ContentDocumentLink cdl = New ContentDocumentLink();

            cdl.LinkedEntityId = OppId;
            
            cdl.ContentDocumentId = conDocId;
            
            cdl.shareType = 'V';
            
        ContentDocLink.add(cdl);
            insert ContentDocLink;
            
        }
        
    }
}

 
Hi Everyone I have one cusotm object "cus_project__c" and one custome filed which name is Product Family( General, Stationery and other  ) , so I want when order is actived then in cus_project __c record will be created when according to product family. For Ex: If in order we are added one product and which project family is General then project record will be created with the name of "General" and all greneral category record will be store in this project.
 
So can any one please help me for resolve this issue with the help of trigger.
I Just write the following code but the project record is not created. 
trigger CreateProject on Order (after insert, after update) {
    List<cus_project__c> prolist= new List<cus_project__c>();
    List<Id> productId = new List<Id>();
    List<Id> priceBookid = new List<Id>();
    Set<Id> orderId= new Set<Id>();
    Set<Id> orderRecId= new Set<Id>();
    
    List<Product2> productlist=[SELECT id, Family from Product2 where id IN : productId];// to get all products.
    for(Order ord: trigger.new)
    {
        if(ord.Activated__c == true && (trigger.oldMap==Null || trigger.oldMap.get(ord.id).Activated__c==false) ) 
        {  
            orderId.add(ord.id);            
           
        }      
        for(Product2 p2: productlist){
                if(p2.family=='other'){
                    if(!prName.contains('other')){
                        Cus_project__c pro1= new Cus_project__c();
                        pro1.Order__c= ord.id;
                        pro1.Name='other';
                        pro1.Start__c=system.today();
                        pro1.End__c=system.today()+80;
                        prolist.add(pro1);  
                    }                      
                }             
            } 
    } 	insert prolist;   
      list<OrderItem> orderIds =[select Product2Id, PricebookEntryId from OrderItem where OrderId in :orderId];// Find OrderProduct ids 
            for(integer i=0;i<orderIds.size();i++){ 
                productId.add(orderIds.get(i).Product2Id);  // Find productsIds
                priceBookid.add(orderIds.get(i).PricebookEntryId);  // Find PriceBooks Ids                      
            }          
         List<cus_project__c> orderRec= [SELECT ID, Name FROM cus_project__c WHERE Order__c In :orderId]; // To get all project of Order.
            List<String> prName= new List<String>();
            for(integer i=0;i<orderRec.size();i++){
                prName.add(orderRec.get(i).Name);                        
            } 
    
     
    
        }

 
global class BatchOnContact implements Database.Batchable<sObject>, Database.Stateful{
    
    global Database.QueryLocator start(Database.BatchableContext bc){     
        
        return Database.getQueryLocator('select Id, name, StageName from opportunity where StageName=\'Closed Won\'');
        
    }
    global void execute(Database.BatchableContext bc, List<opportunity> Opplist){
        List<OpportunityLineItem> OList = new List<OpportunityLineItem>();
        List<Product2> prolist= [SELECT Id from Product2 Limit 10];
              For(opportunity opp:  Opplist){
            for(Integer i=0; i<10; i++){
               OpportunityLineItem op= new OpportunityLineItem(opportunityId=opp.id, Product2Id=prolist.get(i).id,Quantity =1, TotalPrice=1000);
                OList.add(op);                                                               
            }  opp.Pricebook2Id='0066F00000yUeDpQAK';            
        }update opplist;
        insert OList;      
    }
 global void finish(Database.BatchableContext bc){ }
}
public class UpdateMinDateHandler {
    
    Public static Boolean firstcall=false; // Stop the Recursive Calling
    Public static void triggerCall(List<Cus_Project__c> newRecordList){
        Set<Id> OrderId= new Set<Id>();
    List<Cus_Order__c> orderlist= new List<Cus_Order__c>();
    for(Cus_Project__c proj: newRecordList){
        OrderId.add(proj.Cus_Order__c);
    }   
    List<AggregateResult> resultlist= [SELECT Cus_Order__c, Min(Start__c)mindate, Max(End__c)maxdate FROM Cus_Project__c 
                                      where Cus_Order__c IN: OrderId group by Cus_Order__c ];
    List<Cus_Order__c> Updatedata=new  List<Cus_Order__c>();
    system.debug(resultlist);
    for(AggregateResult result:resultlist )
    {
      Updatedata.add(new  Cus_Order__c(Id = (Id)result.get('Cus_Order__c'),
                                      Order_Start_Date__c=(date)result.get('mindate'),
                                      Order_End_Date__c=(date)result.get('maxdate')));
     }
    update Updatedata;
    }

}