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
Daniel MasonDaniel Mason 

Apex Class to only execute when Record developername = 'intermittent'

Afternoon Team, 

I think i am having one of these days where i cant do the basics in salesforce.

Aim : I would like the class below to only execute if the recordtype developername = "intermittent'". If the recordtype developername = "pending' then do nothing. Currently my class below executes on either Record type. "intermittent" or "pending ".

This is the query i use to Query RT Object for Flow object          
 select id,developername,Name from RecordType where SobjectType='flow__c' and developername='intermittent'  

I have created a trigger which calls an utils class

Trigger:

/********************************************************************
 * UpdateCurrencyInsert 
 *
 * Before Insert trigger on Flow
 ********************************************************************/
trigger UpdateCurrencyInsert on Flow__c (before insert) 
{
  FlowUtils.ValidateCurrencyUpdate(Trigger.new);
}
 

Class:

public class FlowUtils {
 
   public static void ValidateCurrencyUpdate(List<Flow__c> flows ) {


      List<Id> opportunityIds = new List<Id>();
      Map<Id, String> oppIdCurrencyCode = new Map<Id, String>();
      for (Flow__c Flow: flows ) {
          opportunityIds.add(Flow.opportunity__c);
      }
      
      for(Opportunity opp : [Select CurrencyIsoCode from Opportunity where Id in: opportunityIds]) {
        oppIdCurrencyCode.put(opp.Id, opp.CurrencyIsoCode);
      }
      
      for(Flow__c flow : flows) {
        flow.CurrencyIsoCode = oppIdCurrencyCode.get(flow.opportunity__c);
      }   
   }
 }
Looking forward to your help and advise 
Best Answer chosen by Daniel Mason
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like
public class FlowUtils {
 
	public static void ValidateCurrencyUpdate(List<Flow__c> flows ) 
	{

		Id contRecordTypeId = Schema.SObjectType.Flow__c.getRecordTypeInfosByName().get('intermittent').getRecordTypeId();
		
		List<Id> opportunityIds = new List<Id>();
		Map<Id, String> oppIdCurrencyCode = new Map<Id, String>();
		for (Flow__c Flow: flows ) 
		{
			if(Flow.recordTypeId == contRecordTypeId)
			{
				opportunityIds.add(Flow.opportunity__c);
			}	
		}

		for(Opportunity opp : [Select CurrencyIsoCode from Opportunity where Id in: opportunityIds]) 
		{
			oppIdCurrencyCode.put(opp.Id, opp.CurrencyIsoCode);
		}

		for(Flow__c flow : flows) 
		{
			if(flow.recordTypeId == contRecordTypeId)
			{
				flow.CurrencyIsoCode = oppIdCurrencyCode.get(flow.opportunity__c);
			}	
		}   
	}
 }

Let us know if this will help you
 

All Answers

Arvind KumarArvind Kumar
Hi Daniel,

Use below code. It would be helpful for you.

You have to be two changes in your code.
1.) Please write record type id of intermittent record type which I have mentioned below.
2.) Use opportunity CurrencyIsoCode field for update value in flow object field.
3.) Use set instead of List. because the set is used for giving a unique value.
public class FlowUtils {
 
   public static void ValidateCurrencyUpdate(List<Flow__c> flows ) {


      Set<Id> opportunityIds = new Set<Id>();
      Map<Id, String> oppIdCurrencyCode = new Map<Id, String>();
      for (Flow__c Flow: flows ) {
            if(flow.RecordTypeId == 'write record type id of intermittent') // please write here record type id of intermittent record type. after that your code will work for intermittent record type.
          {
          opportunityIds.add(Flow.opportunity__c);
          }  
      }
      
      for(Opportunity opp : [Select CurrencyIsoCode from Opportunity where Id in: opportunityIds]) {
        oppIdCurrencyCode.put(opp.Id, opp.CurrencyIsoCode);
      }
      
      for(Flow__c flow : flows) {
        flow.CurrencyIsoCode = oppIdCurrencyCode.get(flow.opportunity__c).CurrencyIsoCode ;  // Use opportunity CurrencyIsoCode field here for update value in flow object field.
      }   
   }
 }

Implement it,  if you have any query please let me know.

Thanks,
Arvind Kumar 
 
Daniel MasonDaniel Mason
@Arvind - you free for a screen share session ?
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like
public class FlowUtils {
 
	public static void ValidateCurrencyUpdate(List<Flow__c> flows ) 
	{

		Id contRecordTypeId = Schema.SObjectType.Flow__c.getRecordTypeInfosByName().get('intermittent').getRecordTypeId();
		
		List<Id> opportunityIds = new List<Id>();
		Map<Id, String> oppIdCurrencyCode = new Map<Id, String>();
		for (Flow__c Flow: flows ) 
		{
			if(Flow.recordTypeId == contRecordTypeId)
			{
				opportunityIds.add(Flow.opportunity__c);
			}	
		}

		for(Opportunity opp : [Select CurrencyIsoCode from Opportunity where Id in: opportunityIds]) 
		{
			oppIdCurrencyCode.put(opp.Id, opp.CurrencyIsoCode);
		}

		for(Flow__c flow : flows) 
		{
			if(flow.recordTypeId == contRecordTypeId)
			{
				flow.CurrencyIsoCode = oppIdCurrencyCode.get(flow.opportunity__c);
			}	
		}   
	}
 }

Let us know if this will help you
 
This was selected as the best answer
Daniel MasonDaniel Mason

@Amit - Thanks for providing that solution. that is exactly what i wanted :)