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
Serghei SleptovSerghei Sleptov 

Trigger to add a custom object when Opportunity is created

Hi Everyone,

I've created a simpletrigger that creates a new record of custom object: Hosted_PBX_Deployment__c when a new Opportunity is created.
Now I need to set a value to a fiield: Rack_4_19_two_post_Qty__c in custom object that depends on a value of an Account field: All_Network_Devices_Qty__c
so if Account.All_Network_Devices_Qty__c <=11 then Rack_4_19_two_post_Qty__c =1 

what is the best way to reffer to an Account field from Hosted_PBX_Deployment__c trigger?

this is my trigger:

trigger AutoCreateHPBXdeployment on Opportunity (after insert) {


List<Hosted_PBX_Deployment__c> newHPBX = new List<Hosted_PBX_Deployment__c>();
  for (Opportunity opp : Trigger.new) {
      if(opp.Hosted_PBX__c=='Yes'){
    Hosted_PBX_Deployment__c hpbx = new Hosted_PBX_Deployment__c();
    hpbx.Opportunity__c  = opp.Id;
    hpbx.Opportunity_Account__c = opp.AccountId;
   
 
    newHPBX.add(hpbx);
      }
  }
  insert newHPBX;

}

Thank you!

Best Answer chosen by Serghei Sleptov
Prashant Pandey07Prashant Pandey07
@Serghei Sleptov - Yes..using set will be better approach.
@Krishnakumari - Thanks for bringing up process builder, Certainly that would be easy and OOB. But the original Question was asked for trigger...

--
Thanks,
Prashant

All Answers

Prashant Pandey07Prashant Pandey07
Hi Serghei,

A map would be the best way is to achieve the account fields..this will help you to avoid the nest for loop..if you not use map(collection) then you need to query the account related to the opportunity and loop through to get the All_Network_Devices_Qty__c field.
 
trigger AutoCreateHPBXdeployment on Opportunity (after insert) {


List<Hosted_PBX_Deployment__c> newHPBX = new List<Hosted_PBX_Deployment__c>();

List<id> ids =new List<id>();

for(Opportunity ops:trigger.new){
ids.add(ops.accountid);
}

map<id,account> mapacc=new map<id,account>([select id,All_Network_Devices_Qty__c,(select id from Opportunities) from account where id=:ids]);

  for (Opportunity opp :trigger.new) {
  
       Hosted_PBX_Deployment__c hpbx = new Hosted_PBX_Deployment__c();
       hpbx.Opportunity__c  = opp.Id;

       if(mapacc.get(opp.accountid).All_Network_Devices_Qty__c<11){        
      
         hpbx.Rack_4_19_two_post_Qty__c=1;
           
           }
           
            newHPBX.add(hpbx);
} 
   
 if(newHPBX.size()>0)
  insert newHPBX;

}

Let me know if this solution works for you

--
Thanks,
Prashant


 
Serghei SleptovSerghei Sleptov

Thanks Prashant!

Wouldn't it be better to use Set instead of List collection to store the ids?

like this:

Set<Id> accIds = new Set<Id>();
for(Opportunity opps : Trigger.new)
{if(Opportunity.AccountId !=null){accIds.add(opps.AccountId);}}
List<Account> hpbxAccount = [SELECT Id, All_Network_Devices_Qty__c FROM Account WHERE id in :accIds];
Map<Id, Account>AccToOppMap = new Map<Id,Account>();
for (Account a :hpbxAccount) {AccToOppMap.put(a.Id,a);}
KrishnakumariKrishnakumari
We can acheive the creation of child records when parent is created using Process builder instead of going for a trigger. Also, we can populate the parent's or grand parent's values to the child record..
Prashant Pandey07Prashant Pandey07
@Serghei Sleptov - Yes..using set will be better approach.
@Krishnakumari - Thanks for bringing up process builder, Certainly that would be easy and OOB. But the original Question was asked for trigger...

--
Thanks,
Prashant
This was selected as the best answer
Serghei SleptovSerghei Sleptov

Thanks Prashant!

@Krishnakumari - thanks for suggesting a process builder ! I found that for certain scenarious to add a trigger much easer then to build a Process :)