• S2
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 2
    Replies

Hi,

 

I wrote test class for controller called from visualforce page and i am facing problem in getting the id from the VF page. The below is the controller class wiritten

public class RegMassupdate {
  
    public RegMassupdate () {
    }
   
    public RegMassupdate (ApexPages.StandardSetController controller) {
    }

public List<Registration__c> queryList= new List<Deal_Registration__c>();
public String RegId{get;set;}
public Set <string> dealSet = new Set<string>();
public string approver{get;set;}

    public List<SelectOption> getApproverList(){
    List<SelectOption> optionsDealList = new List<SelectOption>();
    for(Registration__c dealList:[select Approver_1__c from Registration__c where Id=:RegId]){
       queryList.add(dealList);
      
    if(dealList.Level_1_Approver_1__c != null && dealList.Level_1_Approver_1__c != ''){
        dealSet.add(dealList.Level_1_Approver_1__c);
       }
        }

    for (String allDealList: dealSet){
            if (allDealList!= null){
                optionsDealList.add(new SelectOption(allDealList,allDealList));
              }   
       }
         return optionsDealList;
     }

 

The test class written is as follows

 

@isTest(seeAllData = true)
Private class RegMassupdateTest{


static testMethod void testdealMassupdateNew(){

Profile p = [select id, name from Profile limit 1];
system.debug('a1b'+p.Name);
       
User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test@salesforce.com', Alias='test', CommunityNickname='test Approve', Email='test@email.com',TimeZoneSidKey='America/Chicago',EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', LocaleSidKey='en_US',IsActive=false,Email_Address__c='test@email.com',EmployeeNumber='6534866',Legacy_Employee_Ref__c='487547');
Insert u;
   
Account acc=new Account(Name='Test Account3');
Insert acc;

Opportunity oppty=new Opportunity(Name='Test Oppty',AccountId=acc.Id,StageName='01 ',CloseDate=Date.today());
Insert oppty;


Registration__c Reg=new Registration__c ();
 Reg.Name='Test Registration1';
 Reg.Status__c='Open';
 Reg.Opportunity__c=oppty.Id;
 Reg.Customer_Account__c=acc.Id;
 Reg.Approver_1__c=u.Id;
insert Reg;
 

test.startTest();
              
        PageReference pageRef = Page.RegUpdate ;      
        Test.setCurrentPage(pageRef);      
        ApexPages.currentPage().getParameters().put('id', Reg.id); 
        ApexPages.StandardSetController newController = new ApexPages.StandardSetcontroller(Reg);       
        RegMassupdate massupdate1= new RegMassupdate(newController);
        massupdate1.getApproverList();
       
       
test.stopTest();
}

 

I am facing problem in getting RegId from VF page. Please can you help me in this.

 

Thank you in advance.

  • October 20, 2012
  • Like
  • 0

Hi ,

 

I am facing problem in wirtting test classes for the auto approval process and i have written the code for auto submit for approval in one class and i wrote one more trigger and the intenstion of writting ths trigger is after the approval process is auto initiated and after first level approver approves i want to auto approve the record. The code is working fine and the problem is covering the code for WorkitemId and i am getting below error along with this.

 

System.DmlException: Update failed. First exception on row 0 with id a0xZ0000000yiGkIAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error Occured in Trigger: []

 

The code is as follows

 

Trigger

 

trigger AutoApproval on Registration__c (after Update) {
    for (Integer i = 0; i < Trigger.new.size(); i++)
      {
       try
        {
          if(Trigger.new[i].Status__c == 'submitted' && Trigger.new[i].Check__c==True )
          {
          AutoApprove approve=new AutoApprove ();
          approve.approveRecord(Trigger.new[i]);
          }
          }catch(Exception e)
          {
           Trigger.new[i].addError(e.getMessage());
          }
 
     }
}

 

Class

 

Public class AutoApprove{

//Get ProcessInstanceWorkItemId using SOQL
      public Id getWorkItemId(Id targetObjectId)
      {
          Id retVal = null;
          for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
              where p.ProcessInstance.TargetObjectId =: targetObjectId])
          {
              retVal  =  workItem.Id;
          }
  
          return retVal;
      }
  
      // This method will Approve the Registration
      public void approveRecord(Registration__c r)
      {
          Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
          req.setComments('Auto Approved');
          req.setAction('Approve');
          Id workItemId = getWorkItemId(r.id);
  
          if(workItemId == null)
          {
              r.addError('Error Occured in Trigger');
          }
          else
          {
              req.setWorkitemId(workItemId);
              // Submit the request for approval
              Approval.ProcessResult result =  Approval.process(req);
          }
      }
   }

 

 

and the test class is as follows

 

@isTest
Private class TestAutoApprove {
 
static testMethod void testAutoApproval(){
       
Profile p = [select id from Profile limit 1];
       
User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test1@gmail.com, Alias='test', CommunityNickname='test Approve', LanguageLocaleKey='en_US', LocaleSidKey='en_US',IsActive=false,Email_Address__c='test@email.com',EmployeeNumber='6534866',Legacy_Employee_Ref__c='487547');
Insert u;
system.debug('user data'+u);

Account acc=new Account(Name='Test Account2',ShippingCountry='India',ShippingPostalCode='2345645',ShippingState='Karnataka',ShippingStreet='Ecity');
Insert acc;
    
Opportunity oppty=new Opportunity(Name='Test Opty',AccountId=acc.Id,StageName='01 - Understand Customer',CloseDate=Date.today());
Insert oppty;


 Registration__c Reg=new Registration__c ();
 Reg.Name='Test Registration1';
 Reg.Status__c='Open';
 Reg.Opportunity__c=oppty.Id;
 Reg.Account__c=acc.Id;
 Reg.Program_Comments__c='Testing Registration';
 Insert Reg;
  
 List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :DealReg.id];
 System.assertEquals(processInstances.size(),1);

Reg.Check__c=True;
Reg.Status__c='Submitted';
 Update Reg;
 }
}

 

Please can you help me in this.

  • October 09, 2012
  • Like
  • 0

Hi All,

 

I have a requirement where the validation rule should fire on Custom object before the first level approver click on Approve/Reject button after validating some criteria.

 

I have written the validation rule on custom object like when the Ispickvalue(Status__c,"submitted") fire this validation rule before first level approver approves and the problem i am facing is the first level approver can approve the record against the validation rule and because of approval process field updates it is updating Status field from submitted to Approved.

 

Please let me know is there any way so that i can create the validation rule for approval process instead of adding this criteria in entry level because i want the record to enter the approval process and in approval step i want to fire the validation rule.

 

Please let me know if any one has come with the same problem.

  • October 04, 2012
  • Like
  • 0

Hi ,

 

I have one custom object which has lookup to standard object Opportunity and i want to display the opportunity id and name in email template and my relatedToType is custom object . I gave {!relatedTo.opportunity__r.id} and {!relatedTo.opportunity__r.name} but i am not getting the id and name values of opportunity object.

 

Please let me know if any one worked on the same requirement .

 

Thank you in advance

  • September 23, 2012
  • Like
  • 0

Hi,

 

Please let me know is there any way so that i can give visibility to button in the list view based on the profile or some criteria. Please let me know your suggestion on this.

 

Thank you in advance.

  • September 12, 2012
  • Like
  • 0

Hi,

 

I have a requirement where i need to create the approval process for product object which is the child of opportunity object. In Salesforce we cannot create the approval process for product object related to opportunity.

 

Please let me know is there any way so that i can create the approval process for product object

  • September 10, 2012
  • Like
  • 0

Hi,

 

I wrote test class for controller called from visualforce page and i am facing problem in getting the id from the VF page. The below is the controller class wiritten

public class RegMassupdate {
  
    public RegMassupdate () {
    }
   
    public RegMassupdate (ApexPages.StandardSetController controller) {
    }

public List<Registration__c> queryList= new List<Deal_Registration__c>();
public String RegId{get;set;}
public Set <string> dealSet = new Set<string>();
public string approver{get;set;}

    public List<SelectOption> getApproverList(){
    List<SelectOption> optionsDealList = new List<SelectOption>();
    for(Registration__c dealList:[select Approver_1__c from Registration__c where Id=:RegId]){
       queryList.add(dealList);
      
    if(dealList.Level_1_Approver_1__c != null && dealList.Level_1_Approver_1__c != ''){
        dealSet.add(dealList.Level_1_Approver_1__c);
       }
        }

    for (String allDealList: dealSet){
            if (allDealList!= null){
                optionsDealList.add(new SelectOption(allDealList,allDealList));
              }   
       }
         return optionsDealList;
     }

 

The test class written is as follows

 

@isTest(seeAllData = true)
Private class RegMassupdateTest{


static testMethod void testdealMassupdateNew(){

Profile p = [select id, name from Profile limit 1];
system.debug('a1b'+p.Name);
       
User u = new User(ProfileId = p.id,LastName = 'Test', FirstName = 'Test', Username ='test@salesforce.com', Alias='test', CommunityNickname='test Approve', Email='test@email.com',TimeZoneSidKey='America/Chicago',EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US', LocaleSidKey='en_US',IsActive=false,Email_Address__c='test@email.com',EmployeeNumber='6534866',Legacy_Employee_Ref__c='487547');
Insert u;
   
Account acc=new Account(Name='Test Account3');
Insert acc;

Opportunity oppty=new Opportunity(Name='Test Oppty',AccountId=acc.Id,StageName='01 ',CloseDate=Date.today());
Insert oppty;


Registration__c Reg=new Registration__c ();
 Reg.Name='Test Registration1';
 Reg.Status__c='Open';
 Reg.Opportunity__c=oppty.Id;
 Reg.Customer_Account__c=acc.Id;
 Reg.Approver_1__c=u.Id;
insert Reg;
 

test.startTest();
              
        PageReference pageRef = Page.RegUpdate ;      
        Test.setCurrentPage(pageRef);      
        ApexPages.currentPage().getParameters().put('id', Reg.id); 
        ApexPages.StandardSetController newController = new ApexPages.StandardSetcontroller(Reg);       
        RegMassupdate massupdate1= new RegMassupdate(newController);
        massupdate1.getApproverList();
       
       
test.stopTest();
}

 

I am facing problem in getting RegId from VF page. Please can you help me in this.

 

Thank you in advance.

  • October 20, 2012
  • Like
  • 0

I wrote the trigger for dynamic approval process ,i.e sunmit,Approve and reject successfully.

But i faced problems while writing the test class for this trigger to cover the code.please give me solution for the following trigger

to cover the code.

trigger AutomateApprove on Patient__c(After insert, After update)
{
    for (Integer i = 0; i < Trigger.new.size(); i++){
        try{
            //Insure that previous value not equal to current, else if there is any field update on approval process action
            //there will be recurrence of the trigger.
            if(Trigger.new[i].Next_Step__c == 'Submit' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Submit'))){
                submitForApproval(Trigger.new[i]);
            }
            else if(Trigger.new[i].Next_Step__c == 'Approve' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Approve'))){
                approveRecord(Trigger.new[i]);
            }
            else if(Trigger.new[i].Next_Step__c == 'Reject' && ((Trigger.isInsert || Trigger.old[i].Next_Step__c != 'Reject'))){
                rejectRecord(Trigger.new[i]);
            }

        }catch(Exception e){
            Trigger.new[i].addError(e.getMessage());
        }
    }

    // This method will submit the opportunity automatically
    public void submitForApproval(Patient__c opp){
        // Create an approval request for the Opportunity
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(new Id[] {opp.Next_Approver1__c});

        // Submit the approval request for the Opportunity
        Approval.ProcessResult result = Approval.process(req1);
    }

    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId){
        Id retVal = null;
        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId =: targetObjectId]){
            retVal  =  workItem.Id;
        }

        return retVal;
    }

    // This method will Approve the opportunity
    public void approveRecord(Patient__c opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Approving request using Trigger');
        req.setAction('Approve');
        req.setNextApproverIds(new Id[] {opp.Next_Approver1__c});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);
        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

    // This method will Reject the opportunity
    public void rejectRecord(Patient__c opp){
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        req.setComments('Rejected request using Trigger');
        req.setAction('Reject');
        //req.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        Id workItemId = getWorkItemId(opp.id);
        //opp.addError(workItemId);

        if(workItemId == null){
            opp.addError('Error Occured in Trigger');
            //opp.addError(workItemId);
        }
        else{
            req.setWorkitemId(workItemId);
            // Submit the request for approval
            Approval.ProcessResult result =  Approval.process(req);
        }
    }

}