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
nksfnksf 

Submit for Approval Trigger

Hi I need a trigger on Account to automatically submit for approval process. I have a custom checkbox field name Usage Exclusion. My approval process criteria is if Usage Exclusion checkbox is checked then user can send it for approval request and once it is submitted then I have another custom checkbox field as Requested and it will be checked. If approved then custom checkbox field as Approved will be checked.
So I am looking for a trigger if user check the Usage Exclusion checkbox field and save the record it should automatically send for approval.

Please help me asap.


Best Answer chosen by nksf
magicforce9magicforce9
Hi,


You can use the code below

trigger accountApprovalSubmit on Account (after insert, after update) {
List<Approval.ProcessSubmitRequest> recordsToBeSubmitted = new List<Approval.ProcessSubmitRequest>();
for(Account acc : trigger.new){
if(acc.Usage_Exclusion__c &&(Trigger.isInsert || trigger.newMap.get(acc.id).Usage_Exclusion__c != trigger.oldMap.get(acc.id).Usage_Exclusion__c))
{
Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
request.setComments('Submitting request for approval.');
request.setObjectId(acc.Id);
recordsToBeSubmitted.add(request);
}
}
if(!recordsToBeSubmitted.isEmpty())
Approval.ProcessResult[] results = Approval.process(recordsToBeSubmitted, False);
//You can loop over the above results list and debug the result if you want.
}

All Answers

magicforce9magicforce9
Hi,


You can use the code below

trigger accountApprovalSubmit on Account (after insert, after update) {
List<Approval.ProcessSubmitRequest> recordsToBeSubmitted = new List<Approval.ProcessSubmitRequest>();
for(Account acc : trigger.new){
if(acc.Usage_Exclusion__c &&(Trigger.isInsert || trigger.newMap.get(acc.id).Usage_Exclusion__c != trigger.oldMap.get(acc.id).Usage_Exclusion__c))
{
Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
request.setComments('Submitting request for approval.');
request.setObjectId(acc.Id);
recordsToBeSubmitted.add(request);
}
}
if(!recordsToBeSubmitted.isEmpty())
Approval.ProcessResult[] results = Approval.process(recordsToBeSubmitted, False);
//You can loop over the above results list and debug the result if you want.
}
This was selected as the best answer
nksfnksf
Great. Thanks for the quick response. Let me test it. 
nksfnksf
Hi magicforce9,

I just tested it and it is working fine. Thanks I really appreciate. I am not a developer but I believe I need a test class for this because it is showing code coverage 0. Can you please also help me with the test class. 
magicforce9magicforce9
Hi,

Here is your test class....
---------------------------------------------------------------------------------------------------


@isTest
private class Test_accountApprovalSubmitTrigger {
static testMethod void testApprovalProcessSubmission(){
Account acc = new Account();
acc.Name = 'Test Account';

//You'll also need to populate other required fields for Account Object
acc.Usage_Exclusion__c = True;

//This will cause the statements in trigger to execute
insert acc;

//Checking if the account record is submitted for approval request
List<ProcessInstance> approvalRequest = [Select Id From ProcessInstance where TargetObjectId = :acc.id];
System.assert(approvalRequest.size() > 0);
}
}
nksfnksf
Thanks you so much. I really appreciate it.
nksfnksf
Hi magicforce9 once again

 I need your help once again. No one else replied me with my this question so I though let me bother you one more time. 
I am looking for color background for a custom field value.
I have custom object Usage Data and I have a custom formula text field YTD Average Usage.
If YTD Average Usage is less than 25% color backgroud should be Red and if YTD Average Usage is between 26% to 75% then color background should be Yellow and if YTD Average Usage is more than 75% then color background should be Green.
I am not sure if we have any standard functionality in salesforce for color background. So I have created another Image formula field with Red, Green and Yellow but I am not able to export Report with image in excel sheet. In Excel sheet it is only showing Image URL instead of Image.
Can you please help me  with the alternative or Visualforce Page Report for this kind of report.
If Visualforce Page Report is the only solution then Please help me with the sample of it and I should be able to export report to excel sheet with either color background which is the first priority if not then image should be fine.
I am looking for VF page, Apex Class and Test Class. Here is the sample it should look like in VF Page.
Usage Data is Custom Object. Usage Number is Standard Auto Number Field. School Name is Custom Text Field. YTD Average Usage is Custom Formula Text Field.
Usage Number      School Name          YTD Average Usage
       1111                        ABC                                  24% User-added image       (First Priority will be if we can get color background instead of image)
       1112                        XYZ                                   65% User-added image
       1113                        MNO                                 85% User-added image


magicforce9magicforce9
Hi,

Looking at your requirement, I don't see that there is a need for Visualforce Page Report (Even if we had to do one, I don't see there is an easy way to exporting that data with color fomatting).

In your case what you need to do is to Create a Summary Report and use Conditional Highlighting where you'll specify the range for "YTD Average Usage" field to show in certain color depending on what its value is. And by export the report via clicking on 'Printable View' button on report screen after you run it and you'll retain the formatting.

For more info on Conditional Highlighting Refer : http://help.salesforce.com/HTViewHelpDoc?id=reports_builder_highlighting.htm&language=en_US

You can also watch this webinar on salesforce reporting(https://www.youtube.com/watch?v=O6m0L4FzcB0) where the guy shows Exporting reports at 44:10

Thanks,
Mohammed

tyurytyury
hi everyone! 
anyone can help me for this?

Whenever Auto Approve checkbox is checked, record should be automatically approved.


Once approved, retrieve contacts having the same city with the City Census record that was approved and create and Resident record for each contact retrieved. Create corresponding record type if Contact is Male/Female base on Mr/Ms of Contact Record