You need to sign in to do that
Don't have an account?
rushi
insert record when account has affiliation
Hello,
Account object has territory field and affiliation child objects with lookup relation
how to write a trigger on territory field object
i.e record has to be inserted in territory field object when the lookup account has affiliation
could you please give me a snapshot of code
Thanks in advance..
Account object has territory field and affiliation child objects with lookup relation
how to write a trigger on territory field object
i.e record has to be inserted in territory field object when the lookup account has affiliation
could you please give me a snapshot of code
Thanks in advance..
Here is the modified code.
Thanks
AR
All Answers
Hello,
I assume that Account object is parent object, Territory and Affiliation are child object which are related to account object. So we can say that account have multiple territory and affiliation records.
And your requirement is that if any account record have affiliation record insert at the same time territory record should also create.
If my assumption is correct then below is the trigger to created territory reocrd.
May be you need to update object and field API names in below code.
Thanks & Cheers,
Jigar(pateljb90@gmail.com)
This is what I understood about your requirement -
You need to create a new Territory Field record when you are associating an Affiliation record with the Account. Considering this to be correct, you need to create a trigger on the Affiliation object and NOT on the Territory Field object. Because the trigger is happening when the Affiliation record is getting associated with an Account. The sample code should look something like this -
Thanks
AR
If you find this reply useful then please mark it as best answer.
but my requirement is
when I try to create/update record on territory field object
the lookup account field must have atleast one affiliation
otherwise the record won't be saved
trigger checkAccount on Territory_Field__c (before insert,before update) {
List<Account> a = [select IDfrom Account where ID in(select Account__c from Affiliation__c) ];
for(Territory_Field__c c :trigger.new){
for(Account acc : a ){
if( c.Account__c == acc.id ){
system.debug('insert is successful');
break;
}}
}
}
i tried this code
but it is not working
Hello,
As per you clearification, I assume that before insert or update any territory field record, you need to check that if account(which is related to inserted/updated territory field record) have any affiliation exist or not. And if there is no any affiliation record exist in selected account then you need to give validation message like please enter affiliation in account, otherwise need to insert territory field record without any validation.
If my assumption is correct, below is apex trigger and apex class code.
Apex Trigger
Apex Class
Thanks & Cheers,
Jigar(pateljb90@gmail.com)
Here is the modified code.
Thanks
AR
Thank you for giving me the idea to use map
I modified above code like this
now it is working fine
trigger checkAccount on Territory_Field__c (before insert, before update) {
Map<ID,ID> mapAff = new Map<ID,ID>();
List<Affiliation__c > a =[select Account__c ,ID from Affiliation__c where Account__c !=null];
//create a map for affiliation
for(Affiliation__c aff:a){
mapAff.put(aff.Account__c ,aff.ID);
}
//
for(Territory_Field__c tf:trigger.new){
if(!(mapAff.ContainsKey(tf.Account__c)) ){
f.addError('account dont have affiliation choose another account');
}
}
}
Good that your problem is resolved.
If my reply helped you solve the problem please mark it as best answer.
Thanks
AR