+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Visualforce alter message, if condition met submit for approval

Hi, 

  I have written a controller which is on a custom object is related to opportunity. My requirement is if there is a any product with name 'COTERM' is added and attachment is missing an alter message should be populated on visual force page saying approval cannot be submit since there is no attached. 

 If product is COTERM and attachment is added it must submit for approval please sugget me how to add this functionality 

Below is the controller checking the product and attached 
public class  check_spr_coterm
{
 public check_spr_coterm()
 {
   SPR__c SP;
   //OpportunityLineItem OLI;
   
   SP = [select Opportunity__c from SPR__c 
          where Id = : ApexPages.currentPage().getParameters().get('id') limit 1];
   
   List<AggregateResult> OLI = [select count(id) from OpportunityLineItem 
                                where opportunityid = :SP.Opportunity__c and name like '%COTERM%' ];
    
   List<AggregateResult>  ATH = [select count(id) from  Attachment 
                                 where parentid = :ApexPages.currentPage().getParameters().get('id')];  
   
   
 }
}


How to add the approval process and call this controll in visualforce page please suggest me.
 

<apex:page controller="check_spr_coterm">
<script>
window.alert('{!$CurrentPage.Parameters.Id}');
window.parent.opener.location.reload(true);
window.parent.close();
</script>
</apex:page>
 



Thanks

Sudhir

AnuragGautamAnuragGautam
Hi Sudhir,

Please find the below solution. You have to invoke page action then only you would be able to count atachment on page load.
Please mark as a best answer if its resolve your problem.

Class:

public class  check_spr_coterm
{
Integer count = 0;
 public check_spr_coterm()
 {
   SPR__c SP;
   //OpportunityLineItem OLI;
   
   SP = [select Opportunity__c from SPR__c 
          where Id = : ApexPages.currentPage().getParameters().get('id') limit 1];
   
   List<AggregateResult> OLI = [select count(id) from OpportunityLineItem 
                                where opportunityid = :SP.Opportunity__c and name like '%COTERM%' ];
    
   List<AggregateResult>  ATH = [select count(id) from  Attachment 
                                 where parentid = :ApexPages.currentPage().getParameters().get('id')];  
   
   count = ATH[0].get('id');
 }
 public void pageAction(){
     if(count ==0){
         ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'You cannot submit as this record dont have attachment'));
     }
 }
}


visualForce Page: 

<apex:page controller="check_spr_coterm" action="{!pageAction}">
<apex:pageMessages/>
</apex:page>
Thanks,
Anurag
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Aunragh for your reply sorry for the deal in my responce 

I am using the modified controller you made I get below error message. can you suggest me to fix this issue. 

Error: Compile Error: Illegal assignment from Object to Integer at line 18 column 4


Thanks
Sudhir

 
sudhirn@merunetworks.comsudhirn@merunetworks.com
I made the fix   from count = ATH[0].get('id'); to  count = (Integer)ATH[0].get('id');  
sudhirn@merunetworks.comsudhirn@merunetworks.com
I am calling this visualforce page inside a custom object app there is a approval set on this object how to call the approval process when there is a attachment is added.  
 
  Can we call the approval process inside the apex class.

Thanks
Sudhir
AnuragGautamAnuragGautam
ok great.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Anugrag,

   Any suggestion how to call the approval process when attachement is added. 

Thanks
Sudhir
AnuragGautamAnuragGautam
Hi Sudhir,

You can use below link for your problem.

And you have to write apex:action function on page load. Show alert message and on the confirm of that message call your apex code using same action function.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

Thanks,
Anurag