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
StaciStaci 

trigger to check box after approval process

I have an approval process set for solutions.  What I want is if the INTERNAL ONLY checkbox is unchecked and the status is Accepted, the Visible in Self-Service Portal box needs to get checked.  A workflow isn't working as I don't think it thinks it has been edited.  If I go into the solution and change something and it fits the criteria, it will then check the box.  It won't do this right after the approval process is run.

 

Can I accomplish this in a trigger?  If so, please tell me how.  I SUCK at triggers!

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

Lets see if this approach works for you..

 

I suggest you create a Checkbox custom field on Solutions called isApproved__c and in your ApprovalProcess under 'Final Approval Actions' add a field update and update this checkbox with 'Checked'. So once some approves a Solution record you'll have that custom field Checked. Now add this trigger on Solutions object and this should update the field 'Visible in Self-Service Portal' to checked if IsApproved__c and Internal_Only__c checkbox are selected. I'm assuming the api name of Internal_only__c field.

 

trigger SoutionsTrigger on Solution (before update) {
	
	for(Solution sol : trigger.new)
	{
		if(sol.isApproved__c && Internal_only__c)
		sol.IsPublished = True;
	}
}

 

Here is your test class.

@isTest
public class TestSolutionTrigger {
	static testMethod void  SampleTestMethod{

		Solution sol = new Solution();
		sol.SolutionName = 'Test Solution';
		/* Again I'm assuming the API name and you'll need to change this if different and also populate any other mandatory fields */
		sol.Internal_only__c = True; 
		insert sol;
		sol.isApproved__c = True;
		update sol;
		List<Solution> updatedSol = [Select id, IsPublished from Solution where id = :sol.Id];
		System.assert(updatedSol.IsPublished == True);
	}

}

 

 

 

All Answers

magicforce9magicforce9

Hi,

 

Lets see if this approach works for you..

 

I suggest you create a Checkbox custom field on Solutions called isApproved__c and in your ApprovalProcess under 'Final Approval Actions' add a field update and update this checkbox with 'Checked'. So once some approves a Solution record you'll have that custom field Checked. Now add this trigger on Solutions object and this should update the field 'Visible in Self-Service Portal' to checked if IsApproved__c and Internal_Only__c checkbox are selected. I'm assuming the api name of Internal_only__c field.

 

trigger SoutionsTrigger on Solution (before update) {
	
	for(Solution sol : trigger.new)
	{
		if(sol.isApproved__c && Internal_only__c)
		sol.IsPublished = True;
	}
}

 

Here is your test class.

@isTest
public class TestSolutionTrigger {
	static testMethod void  SampleTestMethod{

		Solution sol = new Solution();
		sol.SolutionName = 'Test Solution';
		/* Again I'm assuming the API name and you'll need to change this if different and also populate any other mandatory fields */
		sol.Internal_only__c = True; 
		insert sol;
		sol.isApproved__c = True;
		update sol;
		List<Solution> updatedSol = [Select id, IsPublished from Solution where id = :sol.Id];
		System.assert(updatedSol.IsPublished == True);
	}

}

 

 

 

This was selected as the best answer
StaciStaci

Thank you Magicforce!  I just changed one small thing, !Internal_only__c and it worked beautifully!