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
nksfnksf 

Trigger to Create/Populate Custom Object Record

Hi Guys,
Can you please help me with a Trigger. We have Custom Object called Tax Info and we have lookup field to Order Object and Lookup field to Account Object Tax Info Custom Object. We also have few more custom fields on Tax Info but Tax ID field is required field and it needs to be populated. I need to write a Trigger that whenever we create New Order then Tax Info record needs to be created/associated to the Order.
Step 1: First we need to look for the existing Tax Info Record off the related Account on Order. If we find existing record then we don't need to create new Tax Info Record. Just associate the existing Tax Info Record to that Order which will also appear on Order Page Tax Info Related List.
Step 2: If there is no existing Tax Info Record off the related Account on Order then we need to create new Tax Info record which will also appear on Order Page Tax Info Related List and Pouplate Order Numer, Account Name and Tax ID on this new record. 
Please let me know if you have any questions.
Thanks in advance!!!!
LBKLBK
Hi nksf,

Assuming that you are using the standard Order object and a custom object (TaxInfo__c) for Tax Information, your trigger will approximately look like this.
 
trigger populateTaxInfo on Order (before insert, before update) {
	
	//List of account ids
    List<Id> lstAccountIds = new List<Id>();
    for (Order ord : trigger.new){ 
        if(ord.AccountId != null){
            lstAccountIds.add(ord.AccountId);
        }
    }

	//map the existing TaxInfo__c
	List<TaxInfo__c> lstTaxInfo = [SELECT Id, AccountId FROM TaxInfo__c WHERE Account__c =: lstAccountIds];
	Map<String, TaxInfo__c> mapTaxInfo = new Map<String, TaxInfo__c>();
	for (TaxInfo__c tx : lstTaxInfo){
		mapTaxInfo.put((String)tx.AccountId, tx);
	}
	
	List<TaxInfo__c> lstNewTaxInfo = new List<TaxInfo__c>();
	for (Id acctId : lstAccountIds){
		if(mapTaxInfo.get((String)acctId) == null){
			TaxInfo__c newTaxInfo = new TaxInfo__c();
			newTaxInfo.Name = 'TAX INFO NAME HERE';
			newTaxInfo.Account__c = acctId;
			//Add all your TaxInfo__c fields here.
			lstNewTaxInfo.add(newTaxInfo);
		} 
	}
    insert lstNewTaxInfo;
	
    //Add the new TaxInfo__c to the map.
	for (TaxInfo__c tx : lstNewTaxInfo){
		mapTaxInfo.put((String)tx.AccountId, tx);
	}
	
	//Update the order
    for (Order ord : trigger.new){ 
        if(ord.AccountId != null){
            ord.TaxInfo__c = mapTaxInfo.get((String)ord.AccountId).Id;
        }
    } 
}

Please go through the code completely and look for syntactical errors, because I have assumed the custom field names.

Let me know if this helps.
 
rajat Maheshwari 6rajat Maheshwari 6

Hi nksf,

As per your requirement, Tax Info is child of Order and account. One Account have one Tax info, Please let me know, if I am wrong.

trigger populateTaxInfo on Order (after insert, after update)
{
//List of account ids
Set<Id> setAccountIds = new Set<Id>();
List<TaxInfo__c> lst_Tax = new List<TaxInfo__c>();

for (Order ord : trigger.new)
{
   if(ord.AccountId != null)
     setAccountIds.add(ord.AccountId);
 }
Map<Id, TaxInfo__c> mapTaxInfo = new Map<Id, TaxInfo__c>();
for (TaxInfo__c tx : [SELECT Id, AccountId,OrderId FROM TaxInfo__c WHERE AccountId IN : setAccountIds])]
  {
      mapTaxInfo.put(tx.AccountId, tx);
  }

for(Order ord : Trigger.new)
  {
     if(ord.AccountId!=null && mapTaxInfo.containsKey(ord.AccountId) && mapTaxInfo.get(ord.AccountId)!=null)
             mapTaxInfo.get(ord.AccountId).OrderId = ord.id;

     else if(ord.AccountId!=null)
       {
           TaxInfo__c tx = new TaxInfo__c();
            tx.OrderId = ord.id;
            tx.AccountId = ord.AccountId;
            tx.name = 'TAX INFO NAME HERE';
            lst_Tax.add(tx);
        }
    }

if(mapTaxInfo!=null && mapTaxInfo.values()!=null)
    update mapTaxInfo.values();

if(lst_Tax!=null && lst_Tax.size()>0)
   insert lst_Tax;

}


          
            
            

 

nksfnksf
Thanks guys for your help. I tried this code below with slight change but getting this error. Apex trigger populateTaxInfo caused an unexpected exception, contact your administrator: populateTaxInfo: execution of AfterInsert caused by: System.SObjectException: Field is not writeable: TaxInfo__c.Order__c: Trigger.populateTaxInfo: line 21, column 1
The changes I mad are in Bold text. Also I want to enter TaxInfo Name same as Account Name how is it possible?


trigger populateTaxInfo on Order (after insert, after update)
{
//List of account ids
Set<Id> setAccountIds = new Set<Id>();
List<TaxInfo__c> lst_Tax = new List<TaxInfo__c>();

for (Order ord : trigger.new)
{
   if(ord.AccountId != null)
     setAccountIds.add(ord.AccountId);
 }
Map<Id, TaxInfo__c> mapTaxInfo = new Map<Id, TaxInfo__c>();
for (TaxInfo__c tx : [SELECT Id, Account__c,Order__c FROM TaxInfo__c WHERE Account__c IN : setAccountIds])
  {
      mapTaxInfo.put(tx.Account__c, tx);
  }

for(Order ord : Trigger.new)
  {
     if(ord.AccountId!=null && mapTaxInfo.containsKey(ord.AccountId) && mapTaxInfo.get(ord.AccountId)!=null)
             mapTaxInfo.get(ord.AccountId).Order__c = ord.id;

     else if(ord.AccountId!=null)
       {
           TaxInfo__c tx = new TaxInfo__c();
            tx.Order__c = ord.id;
            tx.Account__c = ord.AccountId;
            tx.name = 'TAX INFO NAME HERE';
            lst_Tax.add(tx);
        }
    }

if(mapTaxInfo!=null && mapTaxInfo.values()!=null)
    update mapTaxInfo.values();

if(lst_Tax!=null && lst_Tax.size()>0)
   insert lst_Tax;

}
nksfnksf
Hi Rajat, I used your code. Yes TaxInfo is child of Account and Order. Currently I have created Master Detail Relationship for both and fields names are Account__c and Order__c. If instead of Master Detail Relationship I use Lookup Relationship for both then still would it be same code or it will be slightly different?
LBKLBK
Hi nksf,

if you are using a Master - Detail relationship, make sure you have "Allow reparenting" checkbox is checked.

Your error "System.SObjectException: Field is not writeable" could most probably because this checkbox is not checked.
rajat Maheshwari 6rajat Maheshwari 6

Hi nksf,

The code would be same, if you use lookup relationship instead master detail.

Thanks