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
Phillip Mcelu 10Phillip Mcelu 10 

trigger or workflow to update a checkbox

Hello,
User-added image
I'm looking for a apex trigger or workflow that will update a checkbox, if certain condition are met. In my example (screenshot) if High Cedit Sub is greater than two. I want the two subjects with the minimum score and high credit to be checked. Or if that can't work the two subjects with the minimum score to add 20 on them. You are help will very must appreciated, Many Thanks 
Best Answer chosen by Phillip Mcelu 10
Veenesh VikramVeenesh Vikram
This Might work:
trigger updateSubjects on Assessment__c(after insert, after update){
	List<Assessment__c> records = new List<Assessment__c>();
	for(Assessment__c assessment : trigger.new){
		if(assessment.High_Credit_Sub__c > 2){
			records.add(assessment);
		}
	}
	List<Subject__c> recordsToProcess = new List<Subject__c>();
	if(records.size() > 0){
		for(Assessment__c rec : [SELECT Id,High_Credit_Sub__c, (SELECT Id,Extra_High_Credit_Sub__c,Score__c,High_Low__c FROM Subjects__r WHERE High_Low__c = 'High Credit'	 ORDER BY Score__c) FROM Assessment__c WHERE Id in:records]){
			if(rec.Subjects__r.size()>1){
				rec.Subjects__r[0].Extra_High_Credit_Sub__c = true;
				rec.Subjects__r[1].Extra_High_Credit_Sub__c = true;
				recordsToProcess.add(rec.Subjects__r[0]);
				recordsToProcess.add(rec.Subjects__r[1]);
			}
			else if(rec.Subjects__r.size()>0 && rec.Subjects__r.size()<2){
				rec.Subjects__r[0].Extra_High_Credit_Sub__c = true;
				recordsToProcess.add(rec.Subjects__r[0]);
			}
		}
		update recordsToProcess;
	}
}

Veenesh

All Answers

Veenesh VikramVeenesh Vikram
Hi Phillip,

In this case, you will need to write an apex trigger. In order to help yo out with the trigger, We will need the API Names of the Custom Objects and the related fields involved.

Veenesh
 
Phillip Mcelu 10Phillip Mcelu 10
Hi Veenesh, thank you for the reply. The parent object Assessment__c, the field is High_Credit_Sub__c when it’s greater than 2 the trigger should occur. The child object Subject__c, the fields that should be updated is checkbox Extra_High_Credit_Sub__c. But it should update only two the records with minimum value on the Score__c field and “High Credit” on the High_Low__c field. I hope that helps, again many thanks for the help.
Veenesh VikramVeenesh Vikram
Its a bit complex code, but i guess this will work:
trigger updateSubjects on Assessment__c(after insert, after update){
	List<Assessment__c> records = new List<Assessment__c>();
	for(Assessment__c assessment : trigger.new){
		if(assessment.High_Credit_Sub__c > 2){
			records.add(assessment);
		}
	}
	Map<Assessment__c,List<Subject__c>> recordsToProcess = new Map<Assessment__c,List<Subject__c>>();
	if(records.size() > 0){
		for(Assessment__c rec : [SELECT Id,High_Credit_Sub__c, (SELECT Id,Extra_High_Credit_Sub__c,Score__c,High_Low__c FROM Subjects__c WHERE High_Low__c = 'High Credit'	 ORDER BY Score__c) FROM Assessment__c WHERE Id in:records]){
			List<Subject__c> temp = new List<Subject__c>();
			if(rec.Subjects__c.size()>1){
				rec.Subjects__c[0].Extra_High_Credit_Sub__c = true;
				rec.Subjects__c[1].Extra_High_Credit_Sub__c = true;
				temp.add(rec.Subjects__c[0]);
				temp.add(rec.Subjects__c[1]);
			}
			else if(rec.Subjects__c.size()>0 && rec.Subjects__c.size()<2){
				rec.Subjects__c[0].Extra_High_Credit_Sub__c = true;
				temp.add(rec.Subjects__c[0]);
			}
			recordsToProcess.put(rec,temp);
		}
		update recordsToProcess.values();
	}
}

Regards
Veenesh
Phillip Mcelu 10Phillip Mcelu 10
Hi Veenesh, I'm getting a error on line 10.

"Didn't understand relationship 'Subjects__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names." 

This is super complex for me. Many Thanks for the help.
Veenesh VikramVeenesh Vikram
Hi Phil,

Try replacing 'Subjects__c' with 'Subjects__r'.

Veenesh
Phillip Mcelu 10Phillip Mcelu 10
Thanks Veenesh, that worked! I'm getting a new problem now on line 24. "DML requires SObject or SObject list type: List<List<Subject__c>>" Many Thanks, Phillip
Veenesh VikramVeenesh Vikram
This Might work:
trigger updateSubjects on Assessment__c(after insert, after update){
	List<Assessment__c> records = new List<Assessment__c>();
	for(Assessment__c assessment : trigger.new){
		if(assessment.High_Credit_Sub__c > 2){
			records.add(assessment);
		}
	}
	List<Subject__c> recordsToProcess = new List<Subject__c>();
	if(records.size() > 0){
		for(Assessment__c rec : [SELECT Id,High_Credit_Sub__c, (SELECT Id,Extra_High_Credit_Sub__c,Score__c,High_Low__c FROM Subjects__r WHERE High_Low__c = 'High Credit'	 ORDER BY Score__c) FROM Assessment__c WHERE Id in:records]){
			if(rec.Subjects__r.size()>1){
				rec.Subjects__r[0].Extra_High_Credit_Sub__c = true;
				rec.Subjects__r[1].Extra_High_Credit_Sub__c = true;
				recordsToProcess.add(rec.Subjects__r[0]);
				recordsToProcess.add(rec.Subjects__r[1]);
			}
			else if(rec.Subjects__r.size()>0 && rec.Subjects__r.size()<2){
				rec.Subjects__r[0].Extra_High_Credit_Sub__c = true;
				recordsToProcess.add(rec.Subjects__r[0]);
			}
		}
		update recordsToProcess;
	}
}

Veenesh
This was selected as the best answer
Phillip Mcelu 10Phillip Mcelu 10
Veenesh you are best man! Thank you very much. Works beautifully :)