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
cl0s3rcl0s3r 

How to handle an On Click

I have a custom VF Button on my Opportunties. How can I validate if the custom button has been clicked within a Trigger? If the button is clicked I would like to verify if a custom field is null or not.

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Change your constructor to

 

 

public ProdCase(ApexPages.StandardController stdController) {
		this.o = (Opportunity)stdController.getRecord();
                this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id];
	}
	//For testability
	public ProdCase(Opportunity o) {
		this.o = o;
                this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id];
	}

 This will work for you.

 

All Answers

Shashikant SharmaShashikant Sharma

Are you using native page layout or custom vfp where this button is?

cl0s3rcl0s3r

The button is just referencing the custom vf page so I can gather records via the custom controller, I think I can just validate if the field is null via the controller but I am not sure how to post a custom msg to the screen of the Opportunity.

 

Shashikant SharmaShashikant Sharma

Ia ma not getting your issue, Do you want to show error detected in trigger to be shown at vf page? Could you  elaborate your issue a bit more?

cl0s3rcl0s3r

It isnt an error, I want to validate if a field is null and post to the screen a msg requesting additional info.

 

Shashikant SharmaShashikant Sharma

You can add error using many ways

1)Simplest is :

 

 

new Account().addError('Your Error Message');
   return Apexpages.currentpage();

 

 

 

2)

 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'my error msg');
ApexPages.addMessages(myMsg);
return ApexPages.currentPage();

 

 

But these message will only be visible on the page if you have page message in your page

 

 

<apex:pageMessages></apex:pageMessages>

 

Add this just after form tag if you don't have it.

 

cl0s3rcl0s3r

I am definately getting closer, I have added the Apex.AddMessage() function and the msg is still not displaying on the page.  I was thought theissue was a little more strait forward so I didnt include code, but I have inclded the code to assist with my explanation.

Custom Controller:

public class ProdCase {
	private final Opportunity o;
	
	
	public ProdCase(ApexPages.StandardController stdController) {
		this.o = (Opportunity)stdController.getRecord();
	}
	//For testability
	public ProdCase(Opportunity o) {
		this.o = o;
	}
	
//capture the opportunity id from the button
		public PageReference prodDetails(){
			String theID = ApexPages.currentPage().getParameters().get('id');
			if (theID == null){
				return null;
			}
			return prodDetails(theID);
		}
		
		 public PageReference prodDetails(String theID){
		 
			

//Build Oportunity Line Item List object with results from the matching records of the query
 			List<OpportunityLineItem> lin = [Select 
			o.UnitPrice,
			o.TotalPrice,
			o.ListPrice,
			o.Id,
			o.Opportunity.Pricebook2Id,
			o.PricebookEntryId,
			o.ServiceDate,
			o.Quantity,
			o.Description,
		    o.Transmission_Mode__c,
			o.Opportunity.Name,
			o.Opportunity.Type,
			o.Opportunity.Estimated_Implementation_Date__c,
			o.Opportunity.Opportunity_States__c,
			o.Opportunity.Implementation_Contac__c, 
			o.Opportunity.Implementation_Program__c
			From OpportunityLineItem o where o.OpportunityId =:theID and o.Implementation_Validation__c != true];	
			  //List<OpportunityLineItem> lin = [Select(Select UnitPrice, TotalPrice, 
			  //Quantity, ListPrice, Id From OpportunityLineItem WHERE Implementation_Validation__c != true)o.Name from Opportunity o where o.id =:theID] ;
			  
			  if (o.Implementation_Contac__c == null){
			 	ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The Implementation Contact has not been assigned, please correct and resubmit!');
			 	ApexPages.addMessage(myMsg);
			  	return ApexPages.currentPage(); 
			 }

//Build 
			list<Case> ToInsert = new list<Case>();
 				for(Integer I = 0; I < lin.size(); I++){
 	
 					//create the case object 
 					Case cas = new Case();

/*
Determine if the Opportuntiy is a Submitter, Vendor, or Receiver Opportuntiy and create the
correct case record type.
*/ 
	String oliID = lin[I].PricebookEntryId;
	List<PriceBookEntry> prd = [Select p.Product2Id from PricebookEntry p where Id =:oliID ];
	
	for(Integer ic = 0; ic < prd.size(); ic++){
		cas.Related_Products__c=prd[ic].Product2Id;
	
    }
	if(lin[I].Opportunity.Type == ('Submitter')){		
 		 cas.RecordTypeId='012600000004vNEAAY';
 		//Owner equals Ann Shelton
 		cas.OwnerId='00560000000lh2zAAA';	
 	}else if(lin[I].Opportunity.Type ==('Vendor')){	
 		cas.RecordTypeId='012600000004vacAAA';
 		//Owner equals Ann Shelton
 		cas.OwnerId='00560000000lh2zAAA';	
 	}else {	
 		cas.RecordTypeId='012600000004vN9AAI';
 		//Owner equals Cyn Criss
 		cas.OwnerId='00560000000lfXlAAI';		
 	}
 
    cas.Implementation_Name__c=lin[I].Opportunity.Name;
    cas.Status='Business Development';
    cas.Projected_Transactions__c=lin[I].Quantity;
    cas.States__c=lin[I].Opportunity.Opportunity_States__c;
    cas.ContactId=lin[I].Opportunity.Implementation_Contac__c;
	cas.Description=lin[I].Description;
	cas.Subject=lin[I].Opportunity.Name;
	cas.Implementation_Program__c=lin[I].Opportunity.Implementation_Program__c;
	
	//Sandbox
	//cas.Opportunity_Link__c='tapp0.salesforce.com/'+lin[I].OpportunityId;
	
	//Prd
	cas.Opportunity_Link__c='na4.salesforce.com/'+lin[I].OpportunityId;
	cas.Estimated_Implementation_Date__c=lin[I].Opportunity.Estimated_Implementation_Date__c;
	cas.Estimated_Live_Date__c=lin[I].ServiceDate;
//	cas.Related_Products__c=prd[ic].Product2Id;
	

//Sets the validation field true so that you can not submit a line item case more than once.					
    lin[I].Implementation_Validation__c = true;
    
//Insert the cas object into the ToInsert list.    
    ToInsert.add(cas);
    
  }
// Inserts all the objects within the ToInsert list 
  insert ToInsert;
  update lin;
  
  	PageReference pageRef = new PageReference('/' + theId);
	pageRef.setRedirect(true);
	return pageRef;
	}
}

 

VF Page:

<apex:page standardcontroller="Opportunity" extensions="ProdCase" action="{!prodDetails}">
    <apex:sectionHeader title="Oportunity Case Mesages"/>
  <apex:pageBlock >
  <apex:pageBlockSection >
  <apex:Form >
      <apex:pageMessages ></apex:pageMessages>
  </apex:Form>
  </apex:pageblockSection>
  </apex:pageBlock>
</apex:page>

 

 

When I run the code I get the following,

"

Visualforce Error


System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Implementation_Contac__c
Class.ProdCase.prodDetails: line 48, column 31 Class.ProdCase.prodDetails: line 19, column 32 External entry point

"

Shashikant SharmaShashikant Sharma

Change your constructor to

 

 

public ProdCase(ApexPages.StandardController stdController) {
		this.o = (Opportunity)stdController.getRecord();
                this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id];
	}
	//For testability
	public ProdCase(Opportunity o) {
		this.o = o;
                this.o = [Select Implementation_Contac__c from Opportunity where id =: this.o.id];
	}

 This will work for you.

 

This was selected as the best answer
cl0s3rcl0s3r

That worked like a charm!

Shashikant SharmaShashikant Sharma

Good to know it worked for you  :)

cl0s3rcl0s3r

How do you suggest to debug using Eclipse since the force.com plug-in doesnt support break points? I am now sufferning from code coverage issues!

 

Shashikant SharmaShashikant Sharma

I did not get you, If you are facing test coverage issue , I would suggest you should write a test class. And for debuging you have to use system.debug() statements and you can check debug logs to verify results.

 

LIke 

 

Integer i = 100/2;

system.debug('****** value of i : + i);