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
manishtiwary1.390210540410199E12manishtiwary1.390210540410199E12 

Creating a New Record in Custom Object

Hi All,

I have written a trigger for creating a new  record  in custom object when account field  "Top_Account__c== True".

Problem is this trigger is even working when we are doing updation in account
It Should not fire on updating the account record. if "Top_Account__c== True".

trigger Topaccount on Account(before insert, before update) {
  List<Top_Account__c> newTop = new List<Top_Account__c>();
  for (Account acc : Trigger.new) {
  if (acc.)
  {
    Top_Account__c tp = new Top_Account__c();
    tp.Name        = acc.Name;
    tp.releted_acc__c = acc.Id; // Use the trigger record's ID
    newTop .add(tp);
  }
  }
  insert newTop ;
  }
Amritesh SahuAmritesh Sahu
Hi,

Please remove the "before update" from
trigger Topaccount on Account(before insert, before update) 

It should be like
trigger Topaccount on Account(before insert){
........

Hope this helps.
Regards.
sivaextsivaext
Hi , 

You wrote two trigger events  i.e. before insert and before update, 

The trigger will work on both insert and update operation. can user update top account field after account creation? if yes, you need to use before update. 

if you don't want trigger fire every time update, use trigger.old logic

compare values between trigger.old and trigger.new


let me know if you are not clear.


Anoop yadavAnoop yadav
Try this

trigger Topaccount on Account(after insert) {
 You code
}
Subhash GarhwalSubhash Garhwal
Hi Manish,

You need to apply some conditions in you code. So trigger will only fire when user change Top_Account__c fields value to true
Also use after insert and after update event,otherwise in releted_acc__c field acc Id will be null in insert case.


trigger Topaccount on Account(after insert, after update) {
 
  List<Top_Account__c> newTop = new List<Top_Account__c>();
 
  for (Account acc : Trigger.new) {
 
  //Check for Top_Account__c field
  if (acc.Top_Account__c == true && (Trigger.oldMap == null || acc.Top_Account__c != Trigger.oldMap.get(acc.Id).Top_Account__c))
  {
    Top_Account__c tp = new Top_Account__c();
    tp.Name        = acc.Name;
    tp.releted_acc__c = acc.Id; // Use the trigger record's ID
    newTop .add(tp);
  }
  }
  insert newTop ;
  }
manishtiwary1.390210540410199E12manishtiwary1.390210540410199E12
Hi All,

Now One problem-  For account that is already Exist and acc.Top_Account__c == False.
And when I am updating acc.Top_Account__c == False. for Existing account.then
Two  new records are getting created.

Below is the code

trigger Topaccount on Account(After insert, after update) {
  List<Top_Account__c> newTop = new List<Top_Account__c>();
  for (Account acc : Trigger.new) {
  if ((trigger.isInsert || acc.Top_Account__c != trigger.oldMap.get(acc.Id).Top_Account__c) && acc.Top_Account__c == True)
  {
    Top_Account__c tp = new Top_Account__c();
    tp.Name = acc.Name;
    tp.releted_acc__c = acc.Id; // Use the trigger record's ID
    newTop .add(tp);
  }
  }
  insert newTop ;
  }


User-added image

Regards,
Manish Tiwary
Subhash GarhwalSubhash Garhwal
Hi Manish,
If you want to create only one record for one account than you need to check Top Accounts record on Account.

Try this Code

trigger Topaccount on Account(after insert, after update) {
 
  List<Top_Account__c> newTop = new List<Top_Account__c>();
 
//User Child Relationship name in Inline query
  for (Account acc : [Select Id, Top_Account__c, (Select Id, Name, Releted_acc__c From Top_Accounts__r LIMIT 1) From Account Where Id In : Trigger.new]) {
 
	  //Check for top account
	  if(acc.Top_Accounts__r.size() == 0) {

		  //Check for Top_Account__c field
		  if((trigger.isInsert || acc.Top_Account__c != trigger.oldMap.get(acc.Id).Top_Account__c)) && acc.Top_Account__c == True)
		  {
			Top_Account__c tp = new Top_Account__c();
			tp.Name        = acc.Name;
			tp.releted_acc__c = acc.Id; // Use the trigger record's ID
			newTop .add(tp);
		  }
	  }
  }
  insert newTop ;
  }


manishtiwary1.390210540410199E12manishtiwary1.390210540410199E12
Hi Subhash,

Below code is perfectly working for the account that is create now .It is working for both update and insert(geting one line item )

But for the account that is already existing before today .when we are updatting with and  updating acc.Top_Account__c == True[Checkbox].
Then in Cutom object automatically  two line item is comming with same details.


trigger Topaccount on Account(After insert, after update) {
  List<Top_Account__c> newTop = new List<Top_Account__c>();
  for (Account acc : Trigger.new) {
  if ((trigger.isInsert || acc.Top_Account__c != trigger.oldMap.get(acc.Id).Top_Account__c) && acc.Top_Account__c == True)
  {
    Top_Account__c tp = new Top_Account__c();
    tp.Name = acc.Name;
    tp.releted_acc__c = acc.Id; // Use the trigger record's ID
    newTop .add(tp);
  }
  }
  insert newTop ;
  }
Subhash GarhwalSubhash Garhwal
Hi Manish,

Try my code that i posted in earlier post. That will only create new Top Account record on account it there is no record related to that account.
manishtiwary1.390210540410199E12manishtiwary1.390210540410199E12
Hii Subhash,,
Dont no but it is showing error message when i am creating new account with checked

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Topaccount caused an unexpected exception, contact your administrator: Topaccount: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Name: Trigger.Topaccount: line 15, column 1



below is the query


trigger Topaccount on Account(after insert, after update) {

  List<Top_Account__c> newTop = new List<Top_Account__c>();

//User Child Relationship name in Inline query
  for (Account acc : [Select Id, Top_Account__c, (Select Id, Name, Releted_acc__c From Top_Accounts__r LIMIT 1) From Account Where Id In : Trigger.new]) {

      //Check for top account
      if(acc.Top_Accounts__r.size() == 0) {

          //Check for Top_Account__c field
          if((trigger.isInsert || acc.Top_Account__c != trigger.oldMap.get(acc.Id).Top_Account__c) && acc.Top_Account__c == True)
          {
            Top_Account__c tp = new Top_Account__c();
            tp.Name = acc.Name;
            tp.releted_acc__c = acc.Id; // Use the trigger record's ID
            newTop .add(tp);
          }
      }
  }
  insert newTop ;
  }