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
Laura StanleyHubbardLaura StanleyHubbard 

Trigger that looks up values on previously written record to see if new record created is a new value

2 week deadline!

We have a custom object on the Account called Pricing. This object has a field called Workers Compensation Code and Last 2 Digits WC Code

The 'Workers Compensation Code' field is a 6 digit text string, and the 'Last 2 Digits' field is a formula field that pulls the far right two digits of the other field. These two digits translate to one of 50 states.

Here's the goal:

1. Search other records already created for a specific Account I'm working on within the Pricing Object to see if that 2 digit code (state) already exists
2. If the state 2 digits code is already present in any previously saved records, do nothing.
3. if the 2 digit code means a new state is being added for this account, then fire a case and a task.

Does this need to be a trigger? Can we use basic configuration to accomplish this?

Thank you!
mritzimritzi
Please correct typos, syntax errors. Correct field Api names in the code


Trigger:
trigger on Pricing__c(before Insert){
	if(Trigger.isBefore && Trigger.isInsert){
		PricingHandler.handlerMethod1(Trigger.new);
	}
}

Class (trigger hanlder):
public class PricingHandler{
	public static void handlerMethod1(List<Pricing__c> pricingList){
		Set<Id> accountIds = new Set<Id>();
		for(Pricing__c p:pricingList){
			//assuming that Lookup field "Account__c"(if that's its API name) and Workers_Compensation_Code__c are required fields
			// in case they are not, you need to check that they are not empty
			accountIds.add(p.Account__c);
		}
		// map holds account id, List of Pricing__c records pair for each account
		// for each Account Id, map holds list of all related pricing__c records
		// which is used to check that the Last_2_Digits__c of newly inserted record exists or not
		Map<Id,List<Pricing__c>> pricingData = new Map<Id,List<Pricing__c>>();
		// loop to populate map with existing records
		for(Pricing__c p:[Select Id,Last_2_Digits__c,Account__c From Pricing__c Where Account__c IN:accountIds]){
			if(pricingData.containsKey(p.Account__c)){
				List<Pricing__c> tmpList = pricingData.get(p.Account__c);
				tmpList.add(p);
			}
			else{
				List<Pricing__c> tmpList = new List<Pricing__c>();
				tmpList.add(p);
				pricingData.put(p.Account__c,tmpList);
			}
		}
		// loop through trigger record list again to check Last_2_Digits__c of new record with existing records
		for(Pricing__c p:pricingList){
			Boolean found = true;
			for(Pricing__c oldP:pricingData.get(p.Account__c)){
				if(p.Last_2_Digits__c==oldP.Last_2_Digits__c){
					found = true;
					break;
				}
				else
					found = false;
			}
			if(!found){
				// create task and case, add values to their fields as your logic, insert them in respective list
				// add account id with cases and tasks
			}
		}
		// insert task list, and case list here
	}
}


Mark this as Best Answer, if this helps solve your problem.
Laura StanleyHubbardLaura StanleyHubbard
I cant get that trigger to fire without error: I have attached my updated Class
public class PricingHandler{ 
    public static void handlerMethod1(List<Pricing__c> pricingList){ 
        Set<Id> accountIds = new Set<Id>(); 
        for(Pricing__c p:pricingList){ 
            //assuming that Lookup field "Account__c"(if that's its API name) and Workers_Compensation_Code__c are required fields 
            // in case they are not, you need to check that they are not empty 
            accountIds.add(p.Account__c); 
        } 
        // map holds account id, List of Pricing__c records pair for each account 
        // for each Account Id, map holds list of all related pricing__c records 
        // which is used to check that the Last_2_Digits__c of newly inserted record exists or not 
        Map<Id,List<Pricing__c>> pricingData = new Map<Id,List<Pricing__c>>(); 
        // loop to populate map with existing records 
        for(Pricing__c p:[Select Id,Last_Digits_WC_Code__c,Account__c From Pricing__c Where Account__c IN:accountIds]){ 
            if(pricingData.containsKey(p.Account__c)){ 
                List<Pricing__c> tmpList = pricingData.get(p.Account__c); 
                tmpList.add(p); 
            } 
            else{ 
                List<Pricing__c> tmpList = new List<Pricing__c>(); 
                tmpList.add(p); 
                pricingData.put(p.Account__c,tmpList); 
            } 
        } 
        // loop through trigger record list again to check Last_Digits_WC_Code__c of new record with existing records 
        for(Pricing__c p:pricingList){ 
            Boolean found = true; 
            for(Pricing__c oldP:pricingData.get(p.Account__c)){ 
                if(p.Last_Digits_WC_Code__c==oldP.Last_Digits_WC_Code__c){ 
                    found = true; 
                    break; 
                } 
                else 
                    found = false; 
            } 
            if(!found){ 
                // create task and case, add values to their fields as your logic, insert them in respective list 
                // add account id with cases and tasks 
            } 
        } 
        // insert task list, and case list here 
    } 
}