Salesforce 0 Basic Development Primer Learning (ix) Approval Process Introduction

Source: Internet
Author: User

Before reading this article, you can refer to the operation of the approval process summarized by a predecessor. The following links are for reference:

Http://www.cnblogs.com/mingmingruyuedlut/p/3765777.html

The Approval process is used for process approval, and apex primarily involves namespaces and classes that are Approval namespaces and Approval classes under the System namespace.

The main classes under the approval namespace are:

    • ProcessRequest
    • Processsubmitrequest
    • Processworkitemrequest
    • Processresult

Here is a piece of code that describes the role of these classes and how they are used, copied from the sample in the official PDF document.

1  Public classTestapproval {2     voidsubmitandprocessapprovalrequest () {3         //Insert an account4Account A =NewAccount (name= ' Test ', annualrevenue=100.0); 5 insert A;6User User1 = [SELECT Id from User WHERE alias= ' Somestandarduser ']; 7         //Create An approval request for the account8Approval.processsubmitrequest req1 =Newapproval.processsubmitrequest ();9Req1.setcomments (' submitting request for approval. ')); Ten Req1.setobjectid (a.id); One         //Submit on behalf of a specific submitter A Req1.setsubmitterid (user1. ID);  -         //Submit the record to specific process and skip the criteria evaluation -Req1.setprocessdefinitionnameorid (' pto_request_process ');  theReq1.setskipentrycriteria (true);//Submit The approval request for the account -Approval.processresult result = approval.process (REQ1);//Verify The result -System.assert(Result.issuccess ()); -System.assertequals (' Pending ', Result.getinstancestatus (), ' Instance Status ' +result.getinstancestatus ());  +         //Approve the submitted request -         //First , get the ID of the newly created item +List<id> Newworkitemids =result.getnewworkitemids (); A         //instantiate the new Processworkitemrequest object and populate it atApproval.processworkitemrequest REQ2 =Newapproval.processworkitemrequest (); -Req2.setcomments (' approving request. '));  -Req2.setaction (' Approve ');  -Req2.setnextapproverids (Newid[] {userinfo.getuserid ()});  -         //Use the ID of the newly created item to specify the item to be worked -Req2.setworkitemid (Newworkitemids.get (0));  in         //Submit the request for approval -Approval.processresult RESULT2 =approval.process (REQ2); to         //Verify The results +System.assert(Result2.issuccess (), ' Result Status: ' +result2.issuccess ());  -System.assertequals (' Approved ', Result2.getinstancestatus (), ' Instance Status ' +result2.getinstancestatus ()); the     } *}

Note: This code cannot be run directly, if you need to run this code, please refer to the link above to set up an approval process named ' pto_request_process ' for the Account table.

Use the code above to better understand the usage of the following classes.

A) ProcessRequest

The ProcessRequest class, as the parent class for the processsubmitrequest and Processworkitemrequest classes, provides four built-in methods:

    1. Setcomments (Comments): This method is used to set the approval comment when the approval is submitted;
    2. Setnextapproverids (id[] nextapproverids): This method is used for the next level of approval of process approval;
    3. Getcomments (): This method is used to obtain approval comments;
    4. Getnextapproverids (): Returns a list of user IDs for the user who is the approver.

The ProcessRequest encapsulation method is the most basic process approval method, and these methods are instantiation methods. Typically, a new process approval class is not instantiated for this class, but instead instantiates two of his corresponding subclasses.

II) processsubmitrequest

Use this class to submit a record to the process approval, in addition to inheriting the ProcessRequest class method, as follows for its own method:

    1. Setobjectid (RECORDID): Sets the record ID of the specified sobject to process approval. If you insert an account in the sample above, you can submit the account to the approval process, for example, if the Annualrevenue field exceeds how many cases the process approval is executed
    2. Setprocessdefinitionnameorid (Nameorid): Sets the name or number of the process definition to be submitted to a specific approval process. You can set the specified approval process in Salesforce through setup->create->workflow&approvals->approval Processes , if set to NULL, The standard process is followed, and the default value is null;
    3. Setskipentrycriteria (Skipentrycriteria): If Skipentrycriterial is set to True, The request submission skips the checksum in the Setprocessdefinitionnameorid process settings and automatically ignores this parameter if no Nameorid is specified and follows the standard process sequence. If set to false, or do not call this method, the checksum is not skipped;
    4. Setsubmittedid (USERID): Sets the user ID of the record that needs to be submitted to the process approval, which must be a user who is allowed to submit the process in the process definition settings, and the default current user if not set;
    5. The corresponding Get method.

This type of approach shows that this class is primarily used to submit a piece of data to process approval.

III) processworkitemrequest

This class is used to process a process approval request when a record is submitted to process approval, and the status of processing can be divided into approve,reject,removed three cases. In addition to inheriting the methods of the ProcessRequest class, the following is a method of its own:

    1. Setaction (ActionType): Sets the action type to handle a process approval request where actiontype can be the value: approve,reject,removed. Where only the system administrator can specify removed;
    2. Setworkitemid (ID): Sets the number of approval requests that are approved, rejected, or removed, which can be obtained in the Processinstanceworkitem table, which is a table that is encapsulated by Salesforce itself;
    3. The corresponding Get method.

This class of methods describes the classes that are used primarily for approval processing of records that have been submitted to process approval.

IV) Processresult

When you submit a record to process approval, you can use this class to process the result status of the process approval.

Such methods are as follows:

    1. Getentityid (): Gets the number of the record being submitted to the process approval, which can be seen in the Processinstanceworkitem table, corresponding to the field Targetobjectid value for this table;
    2. GetErrors (): Returns an array containing the database object error code and description, if an error occurs;
    3. Getinstanceid (): Gets the number of the process approval, which can be seen in the Processinstanceworkitem table, corresponding to the field ID value for this table;
    4. Getinstancestatus (): Get the status of process approval, mainly have the following several: approved,reject,removed,pending;
    5. Getnewworkitemids (): Gets the ID of a new project submitted to process approval and can have 0 or 1 process approvals. Can be seen in the Processinstanceworkitem table, corresponding to the ID field value of this table;
    6. Issuccess (): Returns True if the approval process is committed correctly, otherwise false.

V) Approval

Approval is located under the System namespace, the above 1-4 are under the approval namespace.

The approval class contains many methods, the main introduction to the process () method, and other methods, please check the official PDF document yourself. This method acts either to submit a new request, or to pass or reject a record of an existing approval. This method parameter has an entry for ProcessRequest, you can specify the Processsubmitrequest or Processworkitemrequest class as a parameter to implement different functions. This method returns a type of Approval.processresult object. When a record is submitted correctly to the approval process, a record of information about this record is added to the corresponding Processinstanceworkitem and ProcessInstance tables.

Summary: ProcessRequest as the parent of the approval request, encapsulates two important methods, two subclasses of the extension implement different functions, processsubmitrequest implementation of a record into the approval process, Processworkitemrequest implementation approval already exists in the approval process for the record.

Make adjustments to the above example so that it can be manipulated in a way that is not done by the predecessor (link above).

As you can see from the above, when you set the Setprocessdefinitionnameorid () method parameter to null or do not call this method, you can not perform the method described above by defining the Setnextapproverids () of the parent class Method Set approval user number can be run through the approval process, so the above code can do the following deformation will run directly:

1  Public classTestapproval {2      Public voidsubmitandprocessapprovalrequest () {3         //Insert an account4Account A =NewAccount (name= ' Test ', annualrevenue=100.0); 5 insert A;6User User1 = [SELECT Id from User WHERE alias= ' zero ']; 7         //Create An approval request for the account8Approval.processsubmitrequest req1 =Newapproval.processsubmitrequest ();9Req1.setcomments (' submitting request for approval. ')); Ten Req1.setobjectid (a.id); One         //Submit on behalf of a specific submitter A Req1.setsubmitterid (user1. ID); -         id[] ids = new Id[]{user1. ID};  - Req1.setnextapproverids (IDS); the         //Submit the record to specific process and skip the criteria evaluation -         //Req1.setprocessdefinitionnameorid (' pto_request_process '); -Req1.setskipentrycriteria (true);//Submit The approval request for the account -Approval.processresult result = approval.process (REQ1);//Verify The result +System.assert(Result.issuccess ()); -System.assertequals (' Pending ', Result.getinstancestatus (), ' Instance Status ' +result.getinstancestatus ());  +         //Approve the submitted request A         //First , get the ID of the newly created item atList<id> Newworkitemids =result.getnewworkitemids (); -         //instantiate the new Processworkitemrequest object and populate it -Approval.processworkitemrequest REQ2 =Newapproval.processworkitemrequest (); -Req2.setcomments (' approving request. '));  -Req2.setaction (' Approve ');  -Req2.setnextapproverids (Newid[] {userinfo.getuserid ()});  in         //Use the ID of the newly created item to specify the item to be worked -Req2.setworkitemid (Newworkitemids.get (0));  to         //Submit the request for approval +Approval.processresult RESULT2 =approval.process (REQ2); -         //Verify The results theSystem.assert(Result2.issuccess (), ' Result Status: ' +result2.issuccess ());  *System.assertequals (' Approved ', Result2.getinstancestatus (), ' Instance Status ' +result2.getinstancestatus ()); $     }Panax Notoginseng}

As for setup->create->workflow&approvals->approval processes a new custom approval process to use better, or in the program to dynamically set the next approver better, Look at the specific project requirements. As for the difference between the two, I do not have much in-depth understanding of the need to better understand the relevant forums or official documents to view. If there is something wrong with the content, please criticize, if there is no understanding of where you can leave a message and contact me.

Salesforce 0 Basic Development Primer Learning (ix) Approval Process Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.