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
H 007H 007 

Need Trigger for Cus_Project__c object.

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);                        
            } 
    
     
    
        }

 
PriyaPriya (Salesforce Developers) 
Hey Harsh,
 
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);  
                    }                      
                }             
            } 
    } 
    system.debug('Size of prolist'+ prolist.size());
    if(!prolist.isEmpty(){
        system.debug('creating the pro');
        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);                        
            } 
    
     
    
        }


Check what you are getting in debug.

Thanks & Regards,

Priya Ranjan

H 007H 007
Hey Priya I don't recived any of the bug after running the above code.  Is that any other way to write the above logic, so please help me..

Thank you. 
PriyaPriya (Salesforce Developers) 
Logic what you built is seems to be correct only . Just wanted to check if you are getting any vaue inside the log or not. Accordingly we can proceed ahead.

Thanks!
mukesh guptamukesh gupta
HI Harsh,


Please use below code:-
 
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>();
	
	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);            
        }  
	}
	
	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);                        
	} 
	
	
    List<Product2> productlist=[SELECT id, Family from Product2 where id IN : productId];// to get all products.
    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);  
			}                      
		}             
	} 
   
    if(prolist.size() > 0)
		insert prolist;   
      
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh