• Melissa Hall
  • NEWBIE
  • 45 Points
  • Member since 2014
  • Salesforce Developer
  • BerkleyMed

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hi, 

I have 4 record types ( Business1, Business2, Business3, Business4). I want to prevent users from changing record type to Business4 from any other (Busiess1,2,3)

Please advise. Thanks 

Banti 

 
  • August 13, 2015
  • Like
  • 0
I've been researching this for two weeks, and I can only come up with half the solution.  I need to have the same capability as the standard Clone, but be able to pull in pre-populated fields.  We currently use Super Clone (https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B4mjFEAR), but I can't use that to pull data from one custom object (Expiring Structure) to another (Pricing Option).  Plus, with over a 100 fields on each page, it's really hard to maintain the page layouts.  

I'm starting with a simple clone of Pricing Option before I move onto something more difficult, but I'm lost as to how to proceed.  I DO NOT want to use a URL hack because that will not function correctly when we move to Lightning. I use that solution on several other objects, but I want to code this using Apex and VF on this object.

I've studied Jeff Douglas' code (http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/) and can successfully clone the record with pre-populated data.  Unfortunately, you have to insert the record and then open in edit mode.  I do not want the record created before the user chooses to complete the clone process.
//Created from instructions by Jeff Douglas: http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/

public with sharing class PricingOptionCloneController {
    
	//add an instance variable for the standard controller
    private ApexPages.StandardController controller {get; set;}

    //add the instance for the variables being passed by id on the url
    private Pricing_Option__c clonePO {get; set;}
    
    //set the id of the record that is created -- only used by test class
    public ID newRecordId {get; set;}
    
    //initialize the controller
    public PricingOptionCloneController(ApexPages.StandardController controller){
    	
    	//initialize the standard controller
        this.controller = controller;
        
        //load the current record
        clonePO = (Pricing_Option__c)controller.getRecord();
    
    }
    
    //method called from the VF's action attribute to clone the case
    public PageReference cloneSelectFields(){
    	
    	//setup the save point for rollback
    	Savepoint sp = Database.setSavepoint();
    	Pricing_Option__c newPO;
    	
    	try{
    		//copy the pricing option -- only include the fields that should be cloned
    		clonePO = [SELECT id, 
    					Opportunity__c,    					
    					Underlying_Comments__c
    				 FROM Pricing_Option__c
    				 WHERE id = :clonePO.id];
    		newPO = clonePO.clone(false);
    		newPO.Name = 'Autopopulated';
    		newPO.Pricing_Option_Description__c = 'update field';
    		newPO.Managing_Director_Pricing_Comments__c = 'update field';
    		insert newPO;
    		    		
    		//set the id of the new po created for testing
    		newRecordId = newPO.id;    		
    		    		
    	} catch (Exception e){
    		//roll the database back to before record was saved
    		Database.rollback(sp);
    		//add a page error/send an email/debug etc.
    		ApexPages.addMessages(e);
    		return null;
    	}
    	return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);
    }    
}

<apex:page standardController="Pricing_Option__c" 
    extensions="PricingOptionCloneController"
    action="{!cloneSelectFields}">
    <apex:pageMessages />
</apex:page>
I've also studied Deepak Gulian's code (https://developer.salesforce.com/forums?id=906F0000000D8XwIAK) and can successfully clone a record into edit mode without first inserting, but I cannot pre-prepopulate any of the fields.
//Created from instructions by Deepak Gulian: https://developer.salesforce.com/forums?id=906F0000000D8XwIAK

public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
   
    public AccountController(ApexPages.StandardController controller){       
       accountid = controller.getId();    
    }
   
    public PageReference autoRun(){
        PageReference clonePage = new PageReference('/'+accountid+'/e?clone=1&retURL=%2F'+accountid);
        clonePage.setRedirect(true);
        return clonePage;
    }   

}

<apex:page standardController="Account" extensions="AccountController" action="{!autoRun}">

  <apex:sectionHeader title="Auto-Running Apex Code"/>
  <apex:outputPanel >
      You tried calling Apex Code from a button.  If you see this page, something went wrong.  You should have been redirected back to the record you clicked the button from.
  </apex:outputPanel>
</apex:page>
I really need a blend of both techniques, but all of my attempts to modify their code has failed.  Can someone assist me in where I need to go?
I'm very new to development, and this is the first Apex Class I've built not using the training courses as guidelines.  My Apex Class and Trigger seem to be working fine, but I'm only getting 50% coverage on my test code.  I'm studied dozens of test code over the last two weeks, and tried oh so many attempts to get this to work, but just don't have the knowledge yet to troubleshoot.

In a nutshell, when an Opportunity is 30 days prior to CloseDate, I need to check a box on the Opp that will trigger a Process Builder process that my admin can maintain.  So simple, yet for some reason so complicated.

Can someone point me in the right direction?  Thanks!

Apex Class
public class OFACMCOQuote {
	
	//create list of Opps
	public static void OFACTask(List<Opportunity> oppList){
		System.Debug('OFACTask: Entering');
			
		oppList = [SELECT Id, Name, isMCOOFACQuoteTriggered__c
	  			   FROM Opportunity 
	  			   WHERE (Type = 'Renewal' OR Type = 'Guaranteed Renewal') 
	  			     AND isClosed = False
	  			     AND Market_Segment__c = 'Managed Care Organization'
	  			     AND CloseDateTrigger_30__c <= TODAY
	  			     AND isMCOOFACQuoteTriggered__c = False]; {
	  				  	  
	 		//field update to trigger process
	 		for(Opportunity opp : oppList) {
	  			System.debug('Name: ' + opp.Name);
	  			if (!oppList.isEmpty()){
					opp.isMCOOFACQuoteTriggered__c = True;	  				
	  			}
	  		}
	 		update oppList;
	  	}	
	}  					 
}
Apex Trigger
trigger OFACMCOTriggers on Opportunity (after insert, after update) {
	OFACMCOQuote.OFACTask(Trigger.new);
	OFACMCOBinder.OFACTask(Trigger.new);
}
Test Class:
@isTest
private class OFACMCOQuote_Test {

	//Verify that OFAC task and notification are triggered
    static testMethod void OFACTask_Test() {
        
		//Load test Profile
    	Map<String, Profile> profiles = new Map<String, Profile>();
        for(Profile p : [select id, name from profile where name IN ('System Administrator')]){
            profiles.put(p.Name, p);
        }
        
        //Load test User
        User u1 = new User(alias = 'test01', email='test01@testorg.com',
                        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                        localesidkey='en_US', profileid = profiles.get('System Administrator').Id,
                        timezonesidkey='America/Los_Angeles', username='test01@testorg.com', CommunityNickname = 'test01');
    	insert new List<User>{u1};
        
        //Load test Account 
      	Account acc1 = new Account(Name='Test01', Managing_Director_lu__c = u1.Id);
        insert new List<Account>{acc1};
    
        //Load test Opportunity 
      	Opportunity opp1 = new Opportunity(AccountId = acc1.Id, Name = 'Test Opp 1', StageName = 'Qualified', Submission_Entry__c = FALSE, 
      										Managing_Director_lu__c = u1.Id, Market_Segment__c = 'Managed Care Organization', 
      										Type = 'Renewal', CloseDate = date.Today()/*, isMCOOFACQuoteTriggered__c = TRUE*/);
      	//Opportunity opp2 = new Opportunity(AccountId = acc1.Id, Name = 'Test Opp 2', StageName = 'Qualified', Submission_Entry__c = FALSE, 
      		//								Managing_Director_lu__c = u1.Id, CloseDate = Date.today(), isMCOOFACQuoteTriggered__c = FALSE);
      	insert new List<Opportunity>{opp1};
      	
      	opp1.CloseDate = opp1.MCOOFACDateHelper__c;
      	update new List<Opportunity>{opp1};
      	        
        Map<Id, Opportunity> opps2Check = new Map<Id, Opportunity>([SELECT Id, CloseDate, isMCOOFACQuoteTriggered__c
        															FROM Opportunity WHERE Id IN :new List<Opportunity>{opp1}]);
        System.assert(opps2Check.get(opp1.Id).isMCOOFACQuoteTriggered__c);
        System.assertEquals(opp1.isMCOOFACQuoteTriggered__c,opps2Check.get(opp1.Id).isMCOOFACQuoteTriggered__c);
        //System.assertNotEquals(opp2.isMCOOFACQuoteTriggered__c,opps2Check.get(opp2.Id).isMCOOFACQuoteTriggered__c);       
    }
}
Our Standard Opportunity has a Master-Detail relationship our Custom Object, Hi-Level.  The Record Type on Hi-Level is used to change page layouts.  We can successfully get the Record Type to update via workflow when the field changes take place on Hi-Level, but are stumped when our criteria lies on the Opportunity.  When the Standard Field, ‘Stage’, on the Opportunity is changed from ‘Stage A’ to ‘Stage B’, we need the Record Type on Hi-Level to change from 'Private' to ‘Released’. 

Things I've attempted:

1) Created a formula field on Hi-Level that references the Stage field on the Opportunity, and created a workflow on Hi-Level to update Record Type  to 'Released' when the formula field equals 'Stage B'.  The formula field works perfectly, but it appears that workflows cannot update from formula fields.  Is that correct, or I'm a missing a key component?

2) Created a workflow on Hi-Level with a field update to mark a checkbox as True when Stage moves to 'Stage B'.  Cannot get the checkbox marked as True because its appears that Cross-Object workflows will not perform field updates from parent to child.  Am I enterpreting this correctly, or could I be missing something simple?

3) Attempted to created my second option using a workflow on the Opportunity instead of Hi-Level, but fields from Hi-Level are not available when I try to create a field update.

Is there any other way I can accomplish this without an Apex trigger?
I'm very new to development, and this is the first Apex Class I've built not using the training courses as guidelines.  My Apex Class and Trigger seem to be working fine, but I'm only getting 50% coverage on my test code.  I'm studied dozens of test code over the last two weeks, and tried oh so many attempts to get this to work, but just don't have the knowledge yet to troubleshoot.

In a nutshell, when an Opportunity is 30 days prior to CloseDate, I need to check a box on the Opp that will trigger a Process Builder process that my admin can maintain.  So simple, yet for some reason so complicated.

Can someone point me in the right direction?  Thanks!

Apex Class
public class OFACMCOQuote {
	
	//create list of Opps
	public static void OFACTask(List<Opportunity> oppList){
		System.Debug('OFACTask: Entering');
			
		oppList = [SELECT Id, Name, isMCOOFACQuoteTriggered__c
	  			   FROM Opportunity 
	  			   WHERE (Type = 'Renewal' OR Type = 'Guaranteed Renewal') 
	  			     AND isClosed = False
	  			     AND Market_Segment__c = 'Managed Care Organization'
	  			     AND CloseDateTrigger_30__c <= TODAY
	  			     AND isMCOOFACQuoteTriggered__c = False]; {
	  				  	  
	 		//field update to trigger process
	 		for(Opportunity opp : oppList) {
	  			System.debug('Name: ' + opp.Name);
	  			if (!oppList.isEmpty()){
					opp.isMCOOFACQuoteTriggered__c = True;	  				
	  			}
	  		}
	 		update oppList;
	  	}	
	}  					 
}
Apex Trigger
trigger OFACMCOTriggers on Opportunity (after insert, after update) {
	OFACMCOQuote.OFACTask(Trigger.new);
	OFACMCOBinder.OFACTask(Trigger.new);
}
Test Class:
@isTest
private class OFACMCOQuote_Test {

	//Verify that OFAC task and notification are triggered
    static testMethod void OFACTask_Test() {
        
		//Load test Profile
    	Map<String, Profile> profiles = new Map<String, Profile>();
        for(Profile p : [select id, name from profile where name IN ('System Administrator')]){
            profiles.put(p.Name, p);
        }
        
        //Load test User
        User u1 = new User(alias = 'test01', email='test01@testorg.com',
                        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                        localesidkey='en_US', profileid = profiles.get('System Administrator').Id,
                        timezonesidkey='America/Los_Angeles', username='test01@testorg.com', CommunityNickname = 'test01');
    	insert new List<User>{u1};
        
        //Load test Account 
      	Account acc1 = new Account(Name='Test01', Managing_Director_lu__c = u1.Id);
        insert new List<Account>{acc1};
    
        //Load test Opportunity 
      	Opportunity opp1 = new Opportunity(AccountId = acc1.Id, Name = 'Test Opp 1', StageName = 'Qualified', Submission_Entry__c = FALSE, 
      										Managing_Director_lu__c = u1.Id, Market_Segment__c = 'Managed Care Organization', 
      										Type = 'Renewal', CloseDate = date.Today()/*, isMCOOFACQuoteTriggered__c = TRUE*/);
      	//Opportunity opp2 = new Opportunity(AccountId = acc1.Id, Name = 'Test Opp 2', StageName = 'Qualified', Submission_Entry__c = FALSE, 
      		//								Managing_Director_lu__c = u1.Id, CloseDate = Date.today(), isMCOOFACQuoteTriggered__c = FALSE);
      	insert new List<Opportunity>{opp1};
      	
      	opp1.CloseDate = opp1.MCOOFACDateHelper__c;
      	update new List<Opportunity>{opp1};
      	        
        Map<Id, Opportunity> opps2Check = new Map<Id, Opportunity>([SELECT Id, CloseDate, isMCOOFACQuoteTriggered__c
        															FROM Opportunity WHERE Id IN :new List<Opportunity>{opp1}]);
        System.assert(opps2Check.get(opp1.Id).isMCOOFACQuoteTriggered__c);
        System.assertEquals(opp1.isMCOOFACQuoteTriggered__c,opps2Check.get(opp1.Id).isMCOOFACQuoteTriggered__c);
        //System.assertNotEquals(opp2.isMCOOFACQuoteTriggered__c,opps2Check.get(opp2.Id).isMCOOFACQuoteTriggered__c);       
    }
}
Hi, 

I have 4 record types ( Business1, Business2, Business3, Business4). I want to prevent users from changing record type to Business4 from any other (Busiess1,2,3)

Please advise. Thanks 

Banti 

 
  • August 13, 2015
  • Like
  • 0