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
EMasterEMaster 

Help with editing a Trigger

Hi

I need to edit an existing Trigger on a custom object, and never having had previous experience with Apex code before, i need some help with it.

Basically what i am trying to achieve is, after a custom object is edited, i need to check a value of a field in that object, and if that field has a specific value, then i need to set the value of another field in that same object by doing a lookup to anoter object.

Here is a summary of the 2 custom objects:
User-added image

The logic i need to develop is, after the a record in License_Users__c is edited:
  1. If the value for License_Users__c.Calculate_Supported__c = "No"
    1. Do nothing (just continue on with the rest of the trigger)
  2. If the value for License_Users__c.Calculate_Supported__c = "Yes"
    1. ​Use the License_Users__c.Software_Type__c field to find the matching record in the Software__c object
    2. Get the Software__c.Version__c value for that matching record
    3. Put that value in the License_Users__c.Version_Maximum__c field for the original License_Users__c record

Below is the current Trigger. Can someone please help me on how to edit this to incorporate the above logic?
trigger LicenseUser_Trigger_SaveEdit on License_Users__c (before update, before insert, after insert) {
    if (trigger.isUpdate)
    {
        if (!Software_Webservice_Class.getIgnoreLicenseUserTrigger())
        {
            // Instantiate Map for processing
            Map<Id, Boolean> mapContactId = new Map<Id, Boolean>();
        
            // Get related Contacts
            for (License_Users__c newLicenseUser : Trigger.new) {
                mapContactId.put(newLicenseUser.Contact__c, true);
            }
            
            // Fill an array of related Contacts
            Contact[] relatedContact = [SELECT Id, Name, AccountId
                                          FROM Contact
                                         WHERE Id in :mapContactId.keySet()];
            
            // Logic to ensure Account & Contact is related. Else Error!
            // If no Account specified on Contact, then update Account to Client/Prospect.
            for (License_Users__c newLU : Trigger.new) {
                License_Users__c oldLU = Trigger.oldMap.get(newLU.Id);

                if ((oldLU.Version_Active__c != newLU.Version_Active__c) || (oldLU.Software_Type__c != newLU.Software_Type__c)
                || (oldLU.PCID__c != newLU.PCID__c) || (newLU.Registration_Number__c == '')) {
                    // Blank out Registration Number and call external webservice
                    newLU.Registration_Number__c = '';
                }
                
                if (newLU.Contact__c != null && oldLU.Contact__c != newLU.Contact__c)
                {               
                    for (Integer i = 0; i < relatedContact.size(); i++) 
                    {
                        if (relatedContact[i].Id == newLU.Contact__c) 
                        {
                            if (relatedContact[i].AccountId != null) 
                            {
                                if (relatedContact[i].AccountId != newLU.Account__c) 
                                    newLU.Contact__c.addError('Account/Contact are not related!');
                            } else {
                                relatedContact[i].AccountId = newLU.Account__c;
                            }
                        }
                    }   
                }
            }
            
            // Update(bulk) any Contact changes
            Database.UpsertResult[] updateResults = Database.Upsert(relatedContact, true);
        }   
    }  // end isUpdate

Many Thanks
ArmouryArmoury
Hi - Why not use formula field instead of trigger? Can you try changing the Version_Maximum__c into a formula field and add the below logic.
IF(Calculate_Supported__c ="Yes", Software_Type__r.Version__c, 0)
EMasterEMaster
Hi Armoury

I needed it to be a static field, not a formula. I think i have actually achieved what i wanted through a Workflow with a Field Update action.

Thanks