You need to sign in to do that
Don't have an account?
Madhuri Kanthekar
Unable to create parent and child account in after insert trigger
Below is my code, I want to create Parent Account and Child Account from same group id field i.e.(Pro_Alpha_ID__c - external Id).
Here If Parent account of the perticular group id does not exists then it should create Parent Account with that group id and then its child account.....Currently only child account is created
Please help to solve this
trigger AccountAfterInsertUpdate on Account (after insert)
{
Account parentAccObj;
List<Account> insertAccMapping = new List<Account>();
List<Account> updateAccMapping = new List<Account>();
try{
if(CommonUtility.check){
CommonUtility.check = false;
ParentAccObj = [SELECT Id,Name,Pro_Alpha_ID__c FROM Account WHERE RecordTypeId='0129E0000004Ozp' AND Pro_Alpha_ID__c =:trigger.new[0].Pro_Alpha_ID__c];
Account myAccount=trigger.new[0];
Account updacc=[select id,Pro_Alpha_ID__c,ParentId from account where id = :myAccount.id];
system.debug('---------------updacc----->'+updacc);
system.debug('---------------parentAccObj.Id----->'+parentAccObj.Id);
updacc.ParentId = parentAccObj.Id;
updacc.Pro_Alpha_ID__c = parentAccObj.Pro_Alpha_ID__c;
update updacc;
// system.debug('---------------accObj.Pro_Alpha_ID__c----->'+accObj.Pro_Alpha_ID__c);
// updateAccMapping.add(updacc);
}
}
catch(Exception e)
{
if(CommonUtility.check){
CommonUtility.check = false;
//create child account first
Account childAccount = new Account();
childAccount.Name = trigger.new[0].Name;
childAccount.Phone = trigger.new[0].Phone;
childAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
childAccount.ParentId = trigger.new[0].Id;
childAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
system.debug('-------------Exception---------'+e.getMessage());
//upsert childAccount;
//create a parent reference
Account parentReference = new Account(Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c);
childAccount.ParentId = parentReference.Id;
system.debug('----------------childAccount.ParentId--------------'+parentReference.ParentId);
// create parent account now
Account parentAccount = new Account();
parentAccount.Name = trigger.new[0].Name;
parentAccount.Phone = trigger.new[0].Phone;
parentAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
parentAccount.RecordTypeId = '0129E0000004Ozp';
parentAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
system.debug('-------------Exception---------'+e.getMessage());
//upsert parentAccount;
Database.SaveResult[] results = Database.insert(new SObject[] { parentAccount, childAccount});
// Check results.
for (Integer i = 0; i < results.size(); i++) {
if (results[i].isSuccess()) {
System.debug('Successfully Created ID: '+ results[i].getId());
} else {
System.debug('Error: could not create sobject '+ 'for array element ' + i + '.');
System.debug(' The error reported was: '+ results[i].getErrors()[0].getMessage() + '\n');
}
}
}
}
}
Here If Parent account of the perticular group id does not exists then it should create Parent Account with that group id and then its child account.....Currently only child account is created
Please help to solve this
trigger AccountAfterInsertUpdate on Account (after insert)
{
Account parentAccObj;
List<Account> insertAccMapping = new List<Account>();
List<Account> updateAccMapping = new List<Account>();
try{
if(CommonUtility.check){
CommonUtility.check = false;
ParentAccObj = [SELECT Id,Name,Pro_Alpha_ID__c FROM Account WHERE RecordTypeId='0129E0000004Ozp' AND Pro_Alpha_ID__c =:trigger.new[0].Pro_Alpha_ID__c];
Account myAccount=trigger.new[0];
Account updacc=[select id,Pro_Alpha_ID__c,ParentId from account where id = :myAccount.id];
system.debug('---------------updacc----->'+updacc);
system.debug('---------------parentAccObj.Id----->'+parentAccObj.Id);
updacc.ParentId = parentAccObj.Id;
updacc.Pro_Alpha_ID__c = parentAccObj.Pro_Alpha_ID__c;
update updacc;
// system.debug('---------------accObj.Pro_Alpha_ID__c----->'+accObj.Pro_Alpha_ID__c);
// updateAccMapping.add(updacc);
}
}
catch(Exception e)
{
if(CommonUtility.check){
CommonUtility.check = false;
//create child account first
Account childAccount = new Account();
childAccount.Name = trigger.new[0].Name;
childAccount.Phone = trigger.new[0].Phone;
childAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
childAccount.ParentId = trigger.new[0].Id;
childAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
system.debug('-------------Exception---------'+e.getMessage());
//upsert childAccount;
//create a parent reference
Account parentReference = new Account(Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c);
childAccount.ParentId = parentReference.Id;
system.debug('----------------childAccount.ParentId--------------'+parentReference.ParentId);
// create parent account now
Account parentAccount = new Account();
parentAccount.Name = trigger.new[0].Name;
parentAccount.Phone = trigger.new[0].Phone;
parentAccount.Allgemeine_EMail_Adresse__c = trigger.new[0].Allgemeine_EMail_Adresse__c;
parentAccount.RecordTypeId = '0129E0000004Ozp';
parentAccount.Pro_Alpha_ID__c = trigger.new[0].Pro_Alpha_ID__c;
system.debug('-------------Exception---------'+e.getMessage());
//upsert parentAccount;
Database.SaveResult[] results = Database.insert(new SObject[] { parentAccount, childAccount});
// Check results.
for (Integer i = 0; i < results.size(); i++) {
if (results[i].isSuccess()) {
System.debug('Successfully Created ID: '+ results[i].getId());
} else {
System.debug('Error: could not create sobject '+ 'for array element ' + i + '.');
System.debug(' The error reported was: '+ results[i].getErrors()[0].getMessage() + '\n');
}
}
}
}
}
Hi Madhuri,
Hope you are doing great !!!
Below is bulkified simple code to support your use case, It will help you definately :)
Thanks
Rajat Maheshwari
Hi Madhuri,
Hope your issue get solved !!!
Please let me know, in case of any help :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com