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

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!
@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
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.
Let me know if this solution works for you
--
Thanks,
Prashant
Thanks Prashant!
Wouldn't it be better to use Set instead of List collection to store the ids?
like this:
@Krishnakumari - Thanks for bringing up process builder, Certainly that would be easy and OOB. But the original Question was asked for trigger...
--
Thanks,
Prashant
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 :)