0

So I have created a trigger and a trigger handler class, but I cant seem to make either of them trigger in an isAfter && isUpdate call. My updateInstallStatusDesign works fine when it is an isBefore && isUpdate but I need it at the isAfter && isUpdate call.

Trigger

else if(Trigger.isAfter && Trigger.isUpdate)
    {
        OpportunityTriggerHandler.updateInstallStatusDesign(Trigger.old, Trigger.new);
        OpportunityTriggerHandler.createRedesignRecords(Trigger.new);
    }

Tirgger Handler

public static void updateInstallStatusDesign(List<Opportunity> originals, List<Opportunity> results)
    {
        updateInstallStatusToDesign(originals, results);
    }
    private static void updateInstallStatusToDesign(List<Opportunity> originals, List<Opportunity> results)
    {
        Map<Id, Opportunity> oldMap = new Map<Id, Opportunity>(originals);
        Map<Id, Opportunity> newMap = new Map<Id, Opportunity>(results);

        Set<Id> oppIdsWithStageChange = new Set<Id>();
        Set<Id> installIdsToGet = new Set<Id>();

        for(Id current : oldMap.keySet())
        {
            Opportunity oldOpp = oldMap.get(current);
            Opportunity newOpp = newMap.get(current);

            if(oldOpp == null || newOpp == null)
                continue;

            if(oldOpp.RecordTypeId != '012700000001Sgi' || newOpp.RecordTypeId != '012700000001Sgi')
                continue;

            if(oldOpp.StageName != OpportunityStage.ContractApproved && newOpp.StageName == OpportunityStage.ContractApproved)
            {
                oppIdsWithStageChange.add(current);
                installIdsToGet.add(oldOpp.Installation__c);
                installIdsToGet.add(newOpp.Installation__c);
            }
        } 
        List<Installation__c> neededInstalls = [
               SELECT Id, Name, Install_Status__c
               FROM Installation__c
               WHERE Id IN :installIdsToGet 
        ];
        Map<Id, Installation__c> installationMap = new Map<Id, Installation__c>(neededInstalls);

        List<Installation__c> updateInstallations = new List<Installation__c>();

        for(Id current : oppIdsWithStageChange)
        {
            Opportunity oldOpp = oldMap.get(current);
            Opportunity newOpp = newMap.get(current);
            Installation__c oldInstall = installationMap.get(oldOpp.Installation__c);
            Installation__c newInstall = installationMap.get(newOpp.Installation__c);

            if(oldInstall == null || newInstall == null)
                continue;

            if(oldInstall.Install_Status__c == InstallationStatus.SalesRepReview && newInstall.Install_Status__c == InstallationStatus.SalesRepReview)
            {
                updateInstallations.add(new Installation__c(
                    Id = oldInstall.Id,
                    Install_Status__c = InstallationStatus.Design
                ));
            }
        }
        if(updateInstallations.size() > 0)
        {
            update updateInstallations;
        }
    }

    public static void createRedesignRecords(List<Opportunity> results)
    {      
        Set<Id> installsNeedingRedesignRecord = new Set<Id>();     

        for(Opportunity curr : results)
        {
            installsNeedingRedesignRecord.add(curr.Installation__c);
        }

        List<Installation__c> installs = [
               SELECT Id, Name, Install_Status__c, of_Designs__c
               FROM Installation__c
               WHERE Id IN :installsNeedingRedesignRecord 
        ];
        Map<Id, Installation__c> allInstallMap = new Map<Id, Installation__c>(installs);

        List<Design__c> desingsToBeCreated = new List<Design__c>();

        for(Id installId : installsNeedingRedesignRecord ) 
        {
            Installation__c currInstall = allInstallMap.get(installId);
            if(currInstall.of_Designs__c <= 1 && currInstall.OppStage__c == OpportunityStage.ContractApproved && currInstall.RecordTypeId == '012700000001Sgi' && currInstall.Install_Status__c == InstallationStatus.Design )
            {
                desingsToBeCreated.add(new Design__c(
                    RecordTypeId = '01270000000MzZ8',
                    Installation__c = installId,
                    Design_Status__c = 'Revisions Needed',
                    Redesign_Reason__c = 'Building Department',
                    Revision_Number__c = '0'
                ));
            }
        }

        insert desingsToBeCreated;
    }

closed as off-topic by David Reed, Mohith Shrivastava, glls, Reshma, Pranay Jaiswal Mar 18 at 11:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – David Reed, Mohith Shrivastava, glls, Reshma, Pranay Jaiswal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Does your trigger declaration (line 1) actually say "after update" in it? – sfdcfox Mar 15 at 18:50
  • Yes it does have after update in the decleration. – PabloL Mar 18 at 15:44
  • I have gone ahead and re-wrote the trigger and it seems to be in working order now. Thank you for your suggestions. – PabloL Mar 20 at 20:41
2

Can you please remove else if for the condition and only try with if in your trigger.

  • I have tried it without the else if and it still did not work unfortunately. – PabloL Mar 18 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.