You need to sign in to do that
Don't have an account?
Primary Record on Custom Object
I have 2 custom Object.
A__c <- Main Object
B__c <- Related Object with many records.
Primaryrecord__c <- Checkbox in B__c object
Aname__c <- is the lookup in the A__c object to B__c.
I'm current trying to create Primary Record that will produce and error when another primary record checkbox is selected. I'm trying to copy the code below by replacing the objects and line commands but doesn't seem to work well. Are there any Apex trigger that can help me with this?
this code is functioning well on my Accounts and Contacts object. I just need it for 2 different objects
Code Below
Trigger PrimaryContact on Contact (Before Insert, Before Update, After Insert, After Update, After Delete, After UnDelete) { List<Id> actIds = New List<Id>(); List<Contact> comingCons = New List<Contact>(); List<Id> conIds = New List<Id>(); If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) { For(Contact Con : Trigger.New) { If(Con.Primary_Contact__c == TRUE) { actIds.add(Con.AccountId); conIds.add(Con.Id); comingCons.add(Con); } } } List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds) FROM Account WHERE Id =: actIds]; For(Contact EveryCon : comingCons) { For(Account EveryAccount : allRelatedAccounts){ If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){ EveryCon.addError('There is already a primary contact for this account'); } } } List<Id> accountIds = New List<Id>(); If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){ For(Contact Con : Trigger.New){ If(Con.AccountId != NULL){ accountIds.add(Con.AccountId); } } } If(Trigger.IsDelete){ For(Contact Con : Trigger.Old){ If(Con.AccountId != NULL){ accountIds.add(Con.AccountId); } } } List<Account> actFinalListToUpdte = New List<Account>(); For(Account act : [Select ID, Contact2__c, (Select Id, FirstName, LastName, Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1) FROM Account WHERE Id =: accountIds]) { If(act.Contacts.size() > 0) { act.Contact2__c = act.Contacts[0].Id; actFinalListToUpdte.add(act); } } try{ If(!actFinalListToUpdte.isEmpty()){ update actFinalListToUpdte; } }Catch(Exception e){ system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage()); } }
when I change things I get errors like...
- Error: Compile Error: Variable does not exist: AccountID at line 14 column 32
What should I change AccountID to?
- Didn't understand relationship 'B__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.
Any guidance will help. Thanks
A:- Curly brackets are used to indicate scope in Apex (and other C-like languages). It could be the scope of your class, method, or a separate scope that you've defined within one of those.
You can access variables that have been declared in your current scope and parent scope(s), but not those defined in child scopes or parallel scopes.
In addition to this, you cannot access instance variables from a static scope without an object reference.
B:- The syntax for a child relationship is:
So if the relationship_name is Impacted_Products and the namespace is MC, your sub-query would be:
Try these. I'm sure you would be able to execute the code. Let me know if this helps. If it does mark this as your best answer.
Regards,
Avishek
A__c = Space__c
B__c = Lease__c
Primaryrecord__c = Current_Lease__c <- Checkbox in Lease__c object
I updated to this code but getting
Error: Compile Error: Unexpected token '<'. at line 21 column 10
Getting compile error: Compile Error: Unexpected token '<'. at line 21 column 11