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
LaurenP6777LaurenP6777 

Apex Trigger Conditional Queries

Hi guys, 

I just wrote a quick trigger that will link a child record (NSSO_Parent__c) to an Opportunity based on matching Protocol Number fields. This is working fine but there will be instances when multiple opportunities has the same Protocol Number. In this case, i would like to link the record to the Parent Opportunity - i would know the record is the parent opportunity IF the field "Parent_Opportunity__c" is blank. 

Basically I want a nice way of saying "Run this query and ONLY IF it returns multiple results, loop back thru and choose the one that has "Parent_Opportunity__c = null"

Here is my trigger. Any help would be greatly appreciated!!!!
trigger UpdateParentonNSSO on NSSO_Parent__c (before insert, before update)
{

      for(NSSO_Parent__c n : trigger.new) { 

                if(n.Opportunity__c == null &&n.Protocol_Number__c!=null) { 

                           Opportunity SOpp = [SELECT Id from Opportunity where Protocol_Number__c = :n.protocol_number__c]; 

                            n.Opportunity__c=SOpp.Id; 

     } 
        } 
           }


 
Best Answer chosen by LaurenP6777
Abhishek BansalAbhishek Bansal
Hi Lauren,

I have update the code as there was some error in the previous one.
Please find the updated code below :
trigger UpdateParentonNSSO on NSSO_Parent__c (before insert, before update)
{
	Set<String> setOfProtocolNumbers = new Set<String>();
	for(NSSO_Parent__c n : trigger.new){
		if(n.Opportunity__c == null && n.Protocol_Number__c != null) { 
			setOfProtocolNumbers.add(n.Protocol_Number__c);
		}
	}
	Map<Integer,Opportunity> mapOfMatchingOpps = new Map<Integer,Opportunity>();
	for(Opportunity opp : [Select id,Protocol_Number__c,Parent_Opportunity__c from Opportunity where Protocol_Number__c IN :setOfProtocolNumbers]){
		if(!mapOfMatchingOpps.containsKey(opp.Protocol_Number__c)){
			mapOfMatchingOpps.put(opp.Protocol_Number__c,opp);
		}
		else if(opp.Parent_Opportunity__c != null){
			mapOfMatchingOpps.put(opp.Protocol_Number__c,opp);
		}
	}
	
	for(NSSO_Parent__c n : trigger.new){
		if(n.Protocol_Number__c != null && mapOfMatchingOpps.containsKey(n.Protocol_Number__c)){
			n.Opportunity__c = mapOfMatchingOpps.get(n.Protocol_Number__c).id; 
		}
	}
}


Let me know if you any issue with the above code and you can also contact me on my gmail or Skype if you have any issues,
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790

Thanks,
Abhishek Bansal

All Answers

LaurenP6777LaurenP6777
Thank you so much. Your help is greatly appreciated, however, the Protocol # field is actually a text field (and it needs to be). I tried changing your "Set setOfProtocolNumbers = new Set(); to "Set setOfProtocolNumbers = new Set(); but it doesn't accept that. Can you help? Sorry.
Abhishek BansalAbhishek Bansal
Hi Lauren.

Please change it to Set<String>.

Thanks,
Abhishek
Abhishek BansalAbhishek Bansal
Hi Lauren,

I have update the code as there was some error in the previous one.
Please find the updated code below :
trigger UpdateParentonNSSO on NSSO_Parent__c (before insert, before update)
{
	Set<String> setOfProtocolNumbers = new Set<String>();
	for(NSSO_Parent__c n : trigger.new){
		if(n.Opportunity__c == null && n.Protocol_Number__c != null) { 
			setOfProtocolNumbers.add(n.Protocol_Number__c);
		}
	}
	Map<Integer,Opportunity> mapOfMatchingOpps = new Map<Integer,Opportunity>();
	for(Opportunity opp : [Select id,Protocol_Number__c,Parent_Opportunity__c from Opportunity where Protocol_Number__c IN :setOfProtocolNumbers]){
		if(!mapOfMatchingOpps.containsKey(opp.Protocol_Number__c)){
			mapOfMatchingOpps.put(opp.Protocol_Number__c,opp);
		}
		else if(opp.Parent_Opportunity__c != null){
			mapOfMatchingOpps.put(opp.Protocol_Number__c,opp);
		}
	}
	
	for(NSSO_Parent__c n : trigger.new){
		if(n.Protocol_Number__c != null && mapOfMatchingOpps.containsKey(n.Protocol_Number__c)){
			n.Opportunity__c = mapOfMatchingOpps.get(n.Protocol_Number__c).id; 
		}
	}
}


Let me know if you any issue with the above code and you can also contact me on my gmail or Skype if you have any issues,
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790

Thanks,
Abhishek Bansal

This was selected as the best answer
LaurenP6777LaurenP6777
Thank you!!! I actually made the same change you did and the trigger is working perfectly! The only snag was that it would only work when both fields were written in the same case. I added a workflow on the Opportunity object that automatically converts the text to Uppercase. Then I added a validation rule to the NSSO object forcing the user to enter in Uppercase. 

Thanks again!