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
Dave ScottDave Scott 

Help with trigger

Hi folks,

I have a trigger that updates a custom field on the Opportunity called 'Primary Contact' with the name of the Primary Contact on the related Opportunity when the Stage is changed to either '8-Deal Lost' or '9-Deal Withdrawn'.

Please can you help me make changes to this trigger that would carry out this update whenever the Opportunity record is updated rather than when the Stage Name is chanaged?
 
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity 
(
    after Insert, 
    after Update
) {

    Set setOpportunity = new Set();
    for(Opportunity oppty : trigger.new){
        if(
            oppty.StageName == '8-Deal Lost' ||
            oppty.StageName == '9-Deal Withdrawn'
        ){
            setOpportunity.add(oppty.Id);
        }
    }

    if(!setOpportunity.isEmpty()){
        Map opptyToOpportunityContactRoleMap = 
            new Map();

        for(
            OpportunityContactRole conRole:
            [
                SELECT  Id, OpportunityId, ContactId
                FROM    OpportunityContactRole
                WHERE   
                        OpportunityId IN :setOpportunity AND
                        IsPrimary = TRUE
            ]
        ){
            opptyToOpportunityContactRoleMap.put(
                conRole.OpportunityId,
                conRole
            );
        }

        List lstOpportunityToUpdate = 
            new List();
        if(!opptyToOpportunityContactRoleMap.isEmpty()){
            for(Opportunity oppty : trigger.new){
                if(
                    opptyToOpportunityContactRoleMap.containsKey(oppty.Id) &&
                    oppty.Primary_Contact__c != 
                        opptyToOpportunityContactRoleMap
                            .get(oppty.Id)
                            .ContactId
                ){
                    lstOpportunityToUpdate.add(
                        new Opportunity(
                            Id = oppty.Id,
                            Primary_Contact__c = 
                                opptyToOpportunityContactRoleMap
                                    .get(oppty.Id)
                                    .ContactId
                        )
                    );
                }
            }

            if(!lstOpportunityToUpdate.isEmpty()){
                UPDATE lstOpportunityToUpdate;
            }
        }
    }
}

Hope that's ok, many thanks in advance for all your help.

Many thanks,
Dave
Best Answer chosen by Dave Scott
Amit Chaudhary 8Amit Chaudhary 8
Just Replace the line number 9 to 14 with below code :-

    for(Opportunity oppty : trigger.new)
    {
            setOpportunity.add(oppty.Id);
    }

All Answers

Himanshu ParasharHimanshu Parashar
Hi Dave,

You can remove line 9-14. Please check the code below.
 
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity 
(
    after Insert, 
    after Update
) {

    Set setOpportunity = new Set();
    for(Opportunity oppty : trigger.new){

            setOpportunity.add(oppty.Id);
        
    }

    if(!setOpportunity.isEmpty()){
        Map opptyToOpportunityContactRoleMap = 
            new Map();

        for(
            OpportunityContactRole conRole:
            [
                SELECT  Id, OpportunityId, ContactId
                FROM    OpportunityContactRole
                WHERE   
                        OpportunityId IN :setOpportunity AND
                        IsPrimary = TRUE
            ]
        ){
            opptyToOpportunityContactRoleMap.put(
                conRole.OpportunityId,
                conRole
            );
        }

        List lstOpportunityToUpdate = 
            new List();
        if(!opptyToOpportunityContactRoleMap.isEmpty()){
            for(Opportunity oppty : trigger.new){
                if(
                    opptyToOpportunityContactRoleMap.containsKey(oppty.Id) &&
                    oppty.Primary_Contact__c != 
                        opptyToOpportunityContactRoleMap
                            .get(oppty.Id)
                            .ContactId
                ){
                    lstOpportunityToUpdate.add(
                        new Opportunity(
                            Id = oppty.Id,
                            Primary_Contact__c = 
                                opptyToOpportunityContactRoleMap
                                    .get(oppty.Id)
                                    .ContactId
                        )
                    );
                }
            }

            if(!lstOpportunityToUpdate.isEmpty()){
                UPDATE lstOpportunityToUpdate;
            }
        }
    }
}

Thanks,
Himanshu
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity (    after Insert,     after Update) 
{
    Set setOpportunity = new Set();
	
	if( Trigger.isInsert )
	{
		for(Opportunity oppty : trigger.new)
		{
			if(
				oppty.StageName == '8-Deal Lost' ||
				oppty.StageName == '9-Deal Withdrawn'
				)
			{
				setOpportunity.add(oppty.Id);
			}
		}
	}
	else if(Trigger.isUpdate )
	{
		for(Opportunity oppty : trigger.new)
		{
			Opportunity oldOpp = Trigger.OldMap.get(oppty.id);
			if(oldOpp.StageName != oppty.StageName)
			{
				if(	oppty.StageName == '8-Deal Lost' || oppty.StageName == '9-Deal Withdrawn' )
				{
					setOpportunity.add(oppty.Id);
				}
			}	
		}
	}	

    if(!setOpportunity.isEmpty())
	{
        Map opptyToOpportunityContactRoleMap = 
            new Map();

        for(
            OpportunityContactRole conRole:
            [
                SELECT  Id, OpportunityId, ContactId
                FROM    OpportunityContactRole
                WHERE   
                        OpportunityId IN :setOpportunity AND
                        IsPrimary = TRUE
            ]
        ){
            opptyToOpportunityContactRoleMap.put(
                conRole.OpportunityId,
                conRole
            );
        }

        List lstOpportunityToUpdate = 
            new List();
        if(!opptyToOpportunityContactRoleMap.isEmpty()){
            for(Opportunity oppty : trigger.new){
                if(
                    opptyToOpportunityContactRoleMap.containsKey(oppty.Id) &&
                    oppty.Primary_Contact__c != 
                        opptyToOpportunityContactRoleMap
                            .get(oppty.Id)
                            .ContactId
                ){
                    lstOpportunityToUpdate.add(
                        new Opportunity(
                            Id = oppty.Id,
                            Primary_Contact__c = 
                                opptyToOpportunityContactRoleMap
                                    .get(oppty.Id)
                                    .ContactId
                        )
                    );
                }
            }

            if(!lstOpportunityToUpdate.isEmpty()){
                UPDATE lstOpportunityToUpdate;
            }
        }
    }
}

Please mark this as solution if this will help. So that some one has same issue this post can help

Thanks,
Amit Chaudhary
 
Dave ScottDave Scott
Hi Himanshu,

I tried to remove lines 9-14, however when deploying I get an error stating:
trgr_Oppty_FetchPrimaryContactEmail_Test.triggerInAction(), Details: System.AssertException: Assertion Failed: Expected: null, Actual: 003w000001S5f8TAAR Class.trgr_Oppty_FetchPrimaryContactEmail_Test.triggerInAction: line 47, column 1
 
, Details: Average test coverage across all Apex Classes and Triggers is 58%, at least 75% test coverage is required.

Any ideas?

Many thanks,
Dave
Dave ScottDave Scott
Hi Amit,

I tried to use your code, however it wouldn't let me save it and gave me the error message:
Error: Compile Error: expecting a semi-colon, found 'setOpportunity' at line 7 column 8
This is the code I used:
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity 
(
    after Insert, 
    after Update
) {

    Set setOpportunity = new Set();
    for(Opportunity oppty : trigger.new){

            setOpportunity.add(oppty.Id);
        
    }

    if(!setOpportunity.isEmpty()){
        Map opptyToOpportunityContactRoleMap = 
            new Map();

        for(
            OpportunityContactRole conRole:
            [
                SELECT  Id, OpportunityId, ContactId
                FROM    OpportunityContactRole
                WHERE   
                        OpportunityId IN :setOpportunity AND
                        IsPrimary = TRUE
            ]
        ){
            opptyToOpportunityContactRoleMap.put(
                conRole.OpportunityId,
                conRole
            );
        }

        List lstOpportunityToUpdate = 
            new List();
        if(!opptyToOpportunityContactRoleMap.isEmpty()){
            for(Opportunity oppty : trigger.new){
                if(
                    opptyToOpportunityContactRoleMap.containsKey(oppty.Id) &&
                    oppty.Primary_Contact__c != 
                        opptyToOpportunityContactRoleMap
                            .get(oppty.Id)
                            .ContactId
                ){
                    lstOpportunityToUpdate.add(
                        new Opportunity(
                            Id = oppty.Id,
                            Primary_Contact__c = 
                                opptyToOpportunityContactRoleMap
                                    .get(oppty.Id)
                                    .ContactId
                        )
                    );
                }
            }

            if(!lstOpportunityToUpdate.isEmpty()){
                UPDATE lstOpportunityToUpdate;
            }
        }
    }
}
Many thanks,
Dave
Dave ScottDave Scott
Guys, I've just realised the original code I posted was an old version, really hope you can help, here's the actual code I should have posted in the first place!
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity 
(
    after Insert, 
    after Update
) {

    Set setOpportunity = new Set();
    for(Opportunity oppty : trigger.new){
        if(
            oppty.StageName == '8-Deal Lost' ||
            oppty.StageName == '9-Deal Withdrawn'
        ){
            setOpportunity.add(oppty.Id);
        }
    }

    if(!setOpportunity.isEmpty()){
        Map opptyToOpportunityContactRoleMap = 
            new Map();

        for(
            OpportunityContactRole conRole:
            [
                SELECT  Id, OpportunityId, ContactId
                FROM    OpportunityContactRole
                WHERE   
                        OpportunityId IN :setOpportunity AND
                        IsPrimary = TRUE
            ]
        ){
            opptyToOpportunityContactRoleMap.put(
                conRole.OpportunityId,
                conRole
            );
        }

        List lstOpportunityToUpdate = 
            new List();
        if(!opptyToOpportunityContactRoleMap.isEmpty()){
            for(Opportunity oppty : trigger.new){
                if(
                    opptyToOpportunityContactRoleMap.containsKey(oppty.Id) &&
                    oppty.Primary_Contact__c != 
                        opptyToOpportunityContactRoleMap
                            .get(oppty.Id)
                            .ContactId
                ){
                    lstOpportunityToUpdate.add(
                        new Opportunity(
                            Id = oppty.Id,
                            Primary_Contact__c = 
                                opptyToOpportunityContactRoleMap
                                    .get(oppty.Id)
                                    .ContactId
                        )
                    );
                }
            }

            if(!lstOpportunityToUpdate.isEmpty()){
                UPDATE lstOpportunityToUpdate;
            }
        }
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Just Replace the line number 9 to 14 with below code :-

    for(Opportunity oppty : trigger.new)
    {
            setOpportunity.add(oppty.Id);
    }
This was selected as the best answer