You are on page 1of 5

Reference Apex Classes

List<Id> newWorkItemIds = result.getNewWorkitemIds();

// Instantiate the new ProcessWorkitemRequest object and populate it


Approval.ProcessWorkitemRequest req2 =
new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve');
req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

// Use the ID from the newly created item to specify the item to be worked
req2.setWorkitemId(newWorkItemIds.get(0));

// Submit the request for approval


Approval.ProcessResult result2 = Approval.process(req2);

// Verify the results


System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());

System.assertEquals(
'Approved', result2.getInstanceStatus(),
'Instance Status'+result2.getInstanceStatus());
}
}

ProcessRequest Class
The ProcessRequest class is the parent class for the ProcessSubmitRequest and ProcessWorkitemResult classes.
Use the ProcessRequest class to write generic Apex that can process objects from either class.
You must specify the Approval namespace when creating an instance of this class. The constructor for this class takes no
arguments. For example:

Approval.ProcessRequest pr = new Approval.ProcessRequest();

The ProcessRequest class has the following methods.

Name Arguments Return Type Description


getComments String Returns the comments that have been added
previously to the approval request.
getNextApproverIds ID[] Returns the list of user IDs of user specified as
approvers.
setComments String Void The comments to be added to the approval
request.
setNextApproverIds ID[] Void If the next step in your approval process is another
Apex approval process, you specify exactly one
user ID as the next approver. If not, you cannot
specify a user ID and this method must be null.

ProcessResult Class
After you submit a record for approval, use the ProcessResult class to process the results of an approval process.

502
Reference Apex Classes

A ProcessResult object is returned by the process method. You must specify the Approval namespace when creating an
instance of this class. For example:

Approval.ProcessResult result = Approval.process(req1);

The ProcessResult class has the following methods. These methods take no arguments.

Name Return Type Description


getEntityId String The ID of the record being processed.
getErrors Database.Error[] If an error occurred, returns an array of one or more
database error objects including the error code and
description. For more information, see Database.Error
Class on page 327.
getInstanceId String The ID of the approval process that has been submitted for
approval.
getInstanceStatus String The status of the current approval process. Valid values are:
Approved, Rejected, Removed or Pending.
getNewWorkitemIds ID[] The IDs of the new items submitted to the approval process.
There can be 0 or 1 approval processes.
isSuccess Boolean A Boolean value that is set to true if the approval process
completed successfully; otherwise, it is set to false.

ProcessSubmitRequest Class
Use the ProcessSubmitRequest class to submit a record for approval.
You must specify the Approval namespace when creating an instance of this class. The constructor for this class takes no
arguments. For example:

Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();

The following methods are unique to the ProcessSubmitRequest class. In addition to these methods, the
ProcessSubmitRequest class has access to all the methods in its parent class, ProcessRequest.

Name Arguments Return Type Description


getObjectId String Returns the ID of the record that has been
submitted for approval. For example, it can return
an account, contact, or custom object record.
setObjectId String Id Void Sets the ID of the record to be submitted for
approval. For example, it can specify an account,
contact, or custom object record.

The ProcessSubmitRequest class shares the following methods with ProcessRequest.

503
Reference Apex Classes

Name Arguments Return Type Description


getComments String Returns the comments that have been added
previously to the approval request.
getNextApproverIds ID[] Returns the list of user IDs of user specified as
approvers.
setComments String Void The comments to be added to the approval
request.
setNextApproverIds ID[] Void If the next step in your approval process is another
Apex approval process, you specify exactly one
user ID as the next approver. If not, you cannot
specify a user ID and this method must be null.

ProcessWorkitemRequest Class
Use the ProcessWorkitemRequest class for processing an approval request after it is submitted.
You must specify the Approval namespace when creating an instance of this class. The constructor for this class takes no
arguments. For example:

Approval.ProcessWorkitemRequest pwr = new Approval.ProcessWorkitemRequest();

The following methods are unique to the ProcessWorkitemRequest class. In addition to these methods, the
ProcessWorkitemRequest class has access to all the methods in its parent class, ProcessRequest.

Name Arguments Return Type Description


getAction String Returns the type of action already associated with
the approval request. Valid values are: Approve,
Reject, or Removed.
getWorkitemId String Returns the ID of the approval request that is in
the process of being approved, rejected, or
removed.
setAction String s Void Sets the type of action to take for processing an
approval request. Valid values are: Approve,
Reject, or Removed. Only system administrators
can specify Removed.
setWorkitemId String Id Void Sets the ID of the approval request that is being
approved, rejected, or removed.

The ProcessWorkitemRequest class shares the following methods with ProcessRequest.

504
Reference Apex Community (Zone) Classes

Name Arguments Return Type Description


getComments String Returns the comments that have been added
previously to the approval request.
getNextApproverIds ID[] Returns the list of user IDs of user specified as
approvers.
setComments String Void The comments to be added to the approval
request.
setNextApproverIds ID[] Void If the next step in your approval process is another
Apex approval process, you specify exactly one
user ID as the next approver. If not, you cannot
specify a user ID and this method must be null.

Apex Community (Zone) Classes


Note: Starting with the Summer ’13 release, “communities” have been renamed to “zones.”

Zones help organize ideas and answers into logical groups with each zone having its own focus and unique ideas and answers
topics. Apex includes the following classes related to a zone:

• Answers Class
• Ideas Class

See Also:
Answers Class
Ideas Class

Answers Class
Answers is a feature of the Community application that enables users to ask questions and have community members post
replies. Community members can then vote on the helpfulness of each reply, and the person who asked the question can mark
one reply as the best answer.
The following are the static methods for answers.

Name Arguments Return Type Description


findSimilar Question question ID[] Returns a list of similar questions based on the title of
question. Each findSimilar call counts against the
SOSL statements governor limit allowed for the process.
setBestReply String questionId Void Sets the specified reply for the specified question as the
best reply. Because a question can have multiple replies,
String replyId

505
Reference Apex Classes

Name Arguments Return Type Description


setting the best reply helps users quickly identify the
reply that contains the most helpful information.

For more information on answers, see “Answers Overview” in the Salesforce online help.

Answers Example
The following example finds questions in an internal zone that have similar titles as a new question:

public class FindSimilarQuestionController {

public static void test() {


// Instantiate a new question
Question question = new Question ();

// Specify a title for the new question


question.title = 'How much vacation time do full-time employees get?';

// Specify the communityID (INTERNAL_COMMUNITY) in which to find similar questions.


Community community = [ SELECT Id FROM Community WHERE Name = 'INTERNAL_COMMUNITY' ];

question.communityId = community.id;

ID[] results = Answers.findSimilar(question);


}
}

The following example marks a reply as the best reply:

ID questionId = [SELECT Id FROM Question WHERE Title = 'Testing setBestReplyId' LIMIT 1].Id;
ID replyID = [SELECT Id FROM Reply WHERE QuestionId = :questionId LIMIT 1].Id;
Answers.setBestReply(questionId,replyId);

See Also:
Apex Community (Zone) Classes
Ideas Class

Ideas Class
Ideas is a community of users who post, vote for, and comment on ideas. An Ideas community provides an online, transparent
way for you to attract, manage, and showcase innovation.
A set of recent replies (returned by methods, see below) includes ideas that a user has posted or commented on that already
have comments posted by another user. The returned ideas are listed based on the time of the last comment made by another
user, with the most recent ideas appearing first.
The userID argument is a required argument that filters the results so only the ideas that the specified user has posted or
commented on are returned.
The communityID argument filters the results so only the ideas within the specified zone are returned. If this argument is
the empty string, then all recent replies for the specified user are returned regardless of the zone.

506

You might also like