You need to sign in to do that
Don't have an account?

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!!!!
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!!!!
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.
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.
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;
}
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;
}
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.
Hi nksf,
The code would be same, if you use lookup relationship instead master detail.
Thanks