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
Chelsea LukowskiChelsea Lukowski 

Trigger to create Record when one doesn't exist

I need help creating a trigger, below are the details.

Commission_Program__c that is a Parent Object with Commission_Product__c as the Child
Commission_Product__c is a Parent Object with Commission_Acocunt__c as the Child
 
When an Titan_Invoice__c is created I need a Commission_Account__c created based on the Titan_Invoice__r.Account__r.Channel_of_Distribution__c matching the Commission_Program__r.Customer_Type__c
and  
Commission_Program__r.Active equals True
and
Titan_Invoice__r.Product__c matching the Commission_Product__r.Product_Description__c.
 
The Commission_Account__c should only be created once and be assiciated with the correct Commission Product. I did create a key field on Commission_Account_c called Account_Column_key__c that is a formula field populating a unique string. 

Hopefully this makes since. I need help getting started with creating the Commission_Acocunt__c trigger. We want to track commission based on the invoice that are populating from our ERP system. 
 

 
mritzimritzi
And how is the Titan_Invoice__c related to Commission_Product__c ??
Chelsea LukowskiChelsea Lukowski
It is not related, it is a seperate object. 
mritzimritzi
Try executing following trigger.

Please correct syntax error's if any, populate field values of Commision_Account record to be inserted

Let me know, how it works
 
trigger CreateCommisionAccount on Titan_Invoice__c(before Insert, after Insert,
											before Update, after Update,
											before Delete, after Delete){
	if(Trigger.isAfter && Trigger.isInsert){
		// com_product List to check the three mentioned conditions
		List<Commission_Product__c> pdList = [Select id,Product_Description__c,Commission_Program__c From Commission_Product__c];
		// com_account list to check for existing records
		List<Commission_Account__c> acListOld = [Select id,Commission_Product__c From Commission_Account__c];
		// new com_Account list to insert new com_Account records
		List<Commission_Account__c> acList = new List<Commission_Account__c>();
		for(Titan_Invoice__c t : Trigger.new){
			for(Commission_Product__c pd : pdList){
				//condition 1: Titan_Invoice__r.Product__c matching the Commission_Product__r.Product_Description__c
				//condition 2: Titan_Invoice__r.Account__r.Channel_of_Distribution__c matching the Commission_Program__r.Customer_Type__c
				//condition 3: Commission_Program__r.Active__c equals True
				// i assumed that Active is a custom field
				if(t.product__c == pd.Product_Description__c && t.Account__r.Channel_Of_Distribution__c == pd.Commission_Program__r.Customer_type__c && pd.Commission_Program__r.Active__c=true){
					//check for existing com_Account records
					for(Commission_Account__c acOld : acListOld){
						if(ac.Commission_Product__c == pd.id)
							t.addError('Commission Account record already exists');
					}
					//if trigger error is not thrown, it means no such Com_Account exists
					Commission_Account__c ac = new Commission_Account__c();
                    // populate name and other fields of com_account record as per your logic here
					ac.name='SomeName';
                    // linking com_account with correct com_product record
					ac.Commission_Product__c = pd.id;
					acList.add(ac);
					break;
				}
			}
		}
	}
	insert acList;
}

 
Chelsea LukowskiChelsea Lukowski
I am getting an error that says line 20 Variable does not exist: ac.Commission_Product__c.
mritzimritzi
Please correct syntax error's if any, populate field values of Commision_Account record to be inserted

Let me know, how it works
 
trigger CreateCommisionAccount on Titan_Invoice__c(before Insert, after Insert,
											before Update, after Update,
											before Delete, after Delete){
	if(Trigger.isAfter && Trigger.isInsert){
		// com_product List to check the three mentioned conditions
		List<Commission_Product__c> pdList = [Select id,Product_Description__c,Commission_Program__c From Commission_Product__c];
		// com_account list to check for existing records
		List<Commission_Account__c> acListOld = [Select id,Commission_Product__c From Commission_Account__c];
		// new com_Account list to insert new com_Account records
		List<Commission_Account__c> acList = new List<Commission_Account__c>();
		for(Titan_Invoice__c t : Trigger.new){
			for(Commission_Product__c pd : pdList){
				//condition 1: Titan_Invoice__r.Product__c matching the Commission_Product__r.Product_Description__c
				//condition 2: Titan_Invoice__r.Account__r.Channel_of_Distribution__c matching the Commission_Program__r.Customer_Type__c
				//condition 3: Commission_Program__r.Active__c equals True
				// i assumed that Active is a custom field
				if(t.product__c == pd.Product_Description__c && t.Account__r.Channel_Of_Distribution__c == pd.Commission_Program__r.Customer_type__c && pd.Commission_Program__r.Active__c=true){
					//check for existing com_Account records
					for(Commission_Account__c acOld : acListOld){
						if(acOld.Commission_Product__c == pd.id)
							t.addError('Commission Account record already exists');
					}
					//if trigger error is not thrown, it means no such Com_Account exists
					Commission_Account__c ac = new Commission_Account__c();
                    // populate name and other fields of com_account record as per your logic here
					ac.name='SomeName';
                    // linking com_account with correct com_product record
					ac.Commission_Product__c = pd.id;
					acList.add(ac);
					break;
				}
			}
		}
	}
	insert acList;
}

Have made correction at line 20.

Mark it as BEST ANSWER, if it solves your problem.