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
VaderVader 

Trigger: Set Opportunity Value based on Account Value

Synopsis:

There is a checkbox on the account named 'Private Account'.  If a user creates an opportunity and the account they reference has Private Account set to TRUE, I want to force a boolean field on the Opportunity name 'Private Opportunity' to also be set to TRUE at the time the opportunity is created or edited.  If the account does not have 'Private Account' set to TRUE, the user should be able to set the 'Private Opportunity' field to TRUE or FALSE without any intervention on the part of the trigger.

 

I have tried 4-5 different ways to do this and haven't been able to get it to work correctly.  Currently code below:

 

trigger validateOppAccountSharing on Opportunity (before insert, before update) {
	
	List<Opportunity> objOpps = [Select Account.Private_Account__c From Opportunity Where Id in: Trigger.new];
	
	for (Integer i = 0; i < Trigger.new.size(); i++) {
		if (objOpps[i].Account.Private_Account__c == true) {
			trigger.new[i].Private_Opportunity__c == TRUE;
		}
	}
}

 Right now I am getting an error that the 'Expression cannot be a statement.'  In other words my syntax or method of assigning TRUE to 'Private_Opportunity__c' is incorrect.

 

Any help is appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Ooops, Sorry

 

I gave you code without compile

 

use below

 

trigger validateOppAccountSharing on Opportunity (before insert, before update) {   
    Set<ID> setOppIds = new Set<ID>();
    for(Opportunity opp:Trigger.new){
        setOppIds.add(opp.Id);
    }
    Map<ID, Opportunity> mapAcc = new Map<ID, Opportunity>([Select Id, Account.Private_Account__c From Opportunity Where Id in:setOppIds]);
    if(mapAcc.size()>0){
        for(Opportunity opp:Trigger.New){
            if(mapAcc.containsKey(opp.Id)){
                opp.Private_Opportunity__c = mapAcc.get(opp.Id).Account.Private_Account__c;
            }
        }
    }
}

 

All Answers

Dhaval PanchalDhaval Panchal
remove extra "=" from
trigger.new[i].Private_Opportunity__c == TRUE;
Dhaval PanchalDhaval Panchal

Try this

 

trigger validateOppAccountSharing on Opportunity (before insert, before update) {	
	Set<ID> setOppIds = new Set<ID>();
	for(Opportunity opp:Trigger.new){
		setOppIds.add(opp.Id);
	}
	Map<ID, Boolean> mapAcc = new Map<ID, Boolean>([Select Id, Account.Private_Account__c From Opportunity Where Id in:setOppIds]);
	if(mapAcc.size()>0){
		for(Opportunity opp:Trigger.New){
			if(mapAcc.containsKey(opp.Id)){
				opp.Private_Opportunity__c = mapAcc.get(opp.Id);
			}
		}
	}
}

 

VaderVader

Thanks Dhaval.  That was certainly part of it.  (gotta love stupid mistakes!) 

 

Based on just changing the equals sign however, I am getting this error when trying to create an opportunity for an account that has Private Account marked as TRUE and not setting the Private Account field as true.

 

validateOppAccountSharing: execution of BeforeInsert

caused by: System.ListException: List index out of bounds: 0

Trigger.validateOppAccountSharing: line 7, column 1

 

Going to try to use what you gave in your second response and will post the results.

Dhaval PanchalDhaval Panchal
It happens some time with all of us :)
VaderVader

Happens to me more than others I think!  :-)

 

Tried your code snipet and got the following error on line 7 on trying to save:

 

Invalid initial type LIST<Opportunity> for MAP<Id,Boolean>

Dhaval PanchalDhaval Panchal

Ooops, Sorry

 

I gave you code without compile

 

use below

 

trigger validateOppAccountSharing on Opportunity (before insert, before update) {   
    Set<ID> setOppIds = new Set<ID>();
    for(Opportunity opp:Trigger.new){
        setOppIds.add(opp.Id);
    }
    Map<ID, Opportunity> mapAcc = new Map<ID, Opportunity>([Select Id, Account.Private_Account__c From Opportunity Where Id in:setOppIds]);
    if(mapAcc.size()>0){
        for(Opportunity opp:Trigger.New){
            if(mapAcc.containsKey(opp.Id)){
                opp.Private_Opportunity__c = mapAcc.get(opp.Id).Account.Private_Account__c;
            }
        }
    }
}

 

This was selected as the best answer
VaderVader

Thanks!  I am going to mark yours as the solution, however, I did make one tweak to the SOQL to meet a needed requirement that the user be able to mark the opportunity as private even if the account wasn't marked that way.

 

 Map<ID, Opportunity> mapAcc = new Map<ID, Opportunity>([Select Id, Account.Private_Account__c From Opportunity Where Account.Private_Account__c = TRUE AND Id in:setOppIds]);

 

This way, only the opportunities created under Accounts that have the value Private Account set to TRUE will force the opportunity to be TRUE.  The user can then have the option to set Private Opportuntiy to TRUE for any other opportunity they create regardless of what the account field value is.

 

Thanks again!