Sunday, September 28, 2014

How to get inserted record id with oncomplete javascript event.

How to get inserted record id with oncomplete javascript event.

Hi,

In some cases you need to read the record id that is recently inserted and perform some operations in javascript, to achieve this you need to set oncomplete  to call java script function in command button tag. The below is the example.

Visualforce Page:

<apex:page controller="OncompleteCLS" >
 <apex:includeScript value="/soap/ajax/26.0/connection.js"/>
<apex:includeScript value="/support/console/26.0/integration.js"/>  
<script>
    function  fun(value){  
    alert('value-->'+value);
   if (typeof(srcUp) == 'function') {
   srcUp('/'+value);
  } else {
   window.open('/'+value,'_parent');
  }
     
    }
   </script>
   

 <apex:form >
 <apex:pageBlock id="pb">
  <apex:pageBlockButtons >
   <apex:commandButton action="{!save}" value=" Save  " oncomplete="fun('{!accountID}')"/>
    <apex:commandButton action="{!cancel}" value="Cancel"/>  
  </apex:pageBlockButtons>
  
 <apex:pageBlockSection id="pbs" collapsible="true" columns="2"> 
   <apex:inputField value="{!acc.Name}"/>
   <apex:inputField value="{!acc.AccountNumber}"/>
   <apex:inputField value="{!acc.phone}"/>
   <apex:inputField value="{!acc.Fax}"/>     
 </apex:pageBlockSection>

 </apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class OncompleteCLS {

public Account acc{get;set;}
public String accountID{get;set;}

public OncompleteCLS(){
acc = new Account();
}


public PageReference cancel() {
    return null;
}



public PageReference save(){
insert acc;
if(acc.id !=null){
accountID = acc.id;
}


 return null;
}
}

Output:

save image



that's it.. 

0 comments:

Post a Comment

 
| ,