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
It just me!It just me! 

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

 

Avishek Nanda 14Avishek Nanda 14
Hi,

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:
[namespace__]relationship_name__r

So if the relationship_name is Impacted_Products and the namespace is MC, your sub-query would be:
(SELECT Id FROM MC__Impacted_Products__r)

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
 
It just me!It just me!
How would you code this?  
It just me!It just me!
Update:
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
 
Trigger CurrentLease on Space__c (Before Insert, Before Update, 
                                   After Insert, After Update, After Delete, After UnDelete) {
    
     List<Id> actIds = New List<Id>();
     List<Lease__c> comingCons = New List<Lease__c>();
     List<Id> conIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Lease__c Con : Trigger.New)
         {
             If(Con.Current_Lease__c == TRUE)
             {
                actIds.add(Con.Space__cId);
                conIds.add(Con.Id);
                comingCons.add(Con);
             }
             
         }
     }
     List<Space__c> allRelatedSpaces = [Select Id FROM Lease__r WHERE Current_Lease__c = TRUE AND Id !=: conIds
                                                        FROM Space__c WHERE Id =: actIds];
     
     
     For(Lease__c EveryCon : comingCons)
     {
         For(Space__c EveryAccount : allRelatedSpaces){
             If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryCon.addError('There is already a current lease for this space');
             }
         }
     }                                  
                                       
     
                                   
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Lease__c Con : Trigger.New){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Lease__c Con : Trigger.Old){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    
    List<Space__c> actFinalListToUpdte = New List<Space__c>();
    
    
    For(Space__c act : [Select ID, Current_Lease__c,
                            (Select Id,
                                    Current_Lease__c FROM Lease__c WHERE Current_Lease__c = TRUE LIMIT 1)
                                FROM Space__c WHERE Id =: Space__cIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Current_Lease__c = act.Contacts[0].Id;
           // act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage());
    }
}

 
It just me!It just me!
Update:

Getting compile error: Compile Error: Unexpected token '<'. at line 21 column 11
Trigger CurrentLease on Lease__c (Before Insert, Before Update, 
                                   After Insert, After Update) {
    
     List<Id> SpaceIds = New List<Id>();
     List<Lease__c> comingCons = New List<Lease__c>();
     List<Id> LeaseIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Lease__c Con : Trigger.New)
         {
             If(Con.Current_Lease__c == TRUE)
             {
                SpaceIds.add(Con.AccountId);
                LeaseIds.add(Con.Id);
                comingCons.add(Con);
             }
             
         }
     }
     List <Lease__c> allRelatedSpaces = [Select ID, Current_Lease__c,
                            (Select Id,
                                    Current_Lease__c FROM Lease__c WHERE Current_Lease__c = TRUE LIMIT 1)
                                FROM Lease__c WHERE Id =: SpaceIds])
     
     {
     For(Lease__c EveryLease : comingCons)
     {
         For(Lease__c EveryAccount : allRelatedSpaces){
             If(EveryLease.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryLease.addError('There is already a current lease for this space');
             }
         }
     }                                  
                                   
    List<Id> SpaceIds = New List<Id>();    
    List<Lease__c> actFinalListToUpdte = New List<Lease__c>();
    
    
    For(Lease__c act : [Select ID, Current_Lease__c,
                            (Select Id,
                                    Current_Lease__c FROM Lease__c WHERE Current_Lease__c = TRUE LIMIT 1)
                                FROM Lease__c WHERE Id =: SpaceIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Current_Lease__c = act.Contacts[0].Id;
           // act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for CurrentLease is: ' + e.getMessage());
    }
}