You need to sign in to do that
Don't have an account?
Natraj
How to compare two lists on save button click and avoid inserting duplicates....
Step 1. I have queried opportunity line items and displayed in the VF page
step 2. On save button click this will be saved in assets
step 3. if values are already in asset databse and if i click save, its again duplicating in assets
So i have to avoid this by confirm msg using javascript. If "Ok" it should be saved and if "Cancel" it sholud not be save in assets while on clicking save button....How to do this????
You can compare on the basis of one Unique field in both objects like email, if it exists then you can show the javascript alert message. Use set and Map in apex to avoid duplication.
Thnks a lot!!!!!!!!!!!!!!!!!!!!
In Order to avoid duplication of the assets ,you can check with database by SOQL
and then insert necessary records only.
For confirm javascript we need to do some more exercise.
you need to use ActionFunction to call controller from javascript
Please check with this code,
let me know if you need any more help on it
Thanks ,
Bala
Here is my code.....
<apex:page controller="AssetController" showheader="false" sidebar="false">
</style>
<apex:form >
<apex:actionFunction name="Save" action="{!save}" rerender="jspanel"/>
<apex:outputPanel id="jspanel">
<script>
function onControllerReturn() {
if('{!message}'=='i'){
alert('The values are succesfully inserted into the assets');
}
else if('{!message}'=='j'){
var c= confirm('It appears that one or more of these line items have already been converted into assets. Do you want to duplicate now?');
if (c)return true;
else
return false;
}
else if('{!message}'=='k'){
alert('Cannot process conversion. Make sure the opportunity is closed and/or the opportunity has an account associated with it');
return false;}
}
</script>
</apex:outputPanel>
<apex:pageBlock Title="Creating Assets from Product Line Items" tabstyle="Account" mode="InlineEdit">
<apex:pageBlockSection Title="List of Available Line Items">
<apex:pageBlockTable value="{!MyProd}" var="a" columnsWidth="50px,50px" cellpadding="4" border="1">
<apex:column >
<apex:facet name="header"> <apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="false"/>
</apex:inputCheckbox></apex:facet>
<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="false"/>
</apex:inputCheckbox></apex:column>
<apex:column >
<apex:facet name="header"><b>Product Name</b></apex:facet>
<apex:inputField value="{!a.opp.PricebookEntry.Name}">
</apex:inputField>
</apex:column>
<apex:column headervalue="Quantity Number" value="{!a.opp.Quantity}" />
<apex:column >
<apex:facet name="header"><b>Asset Name</b></apex:facet>
<apex:inputField value="{!a.opp.PricebookEntry.Product2.Name}" style="color:green" required="false">
</apex:inputField>
</apex:column>
Some other columns to be included
</apex:pageBlockTable>
</apex:pageBlockSection>
<center><apex:commandButton action="{!Save}" onClick="return onControllerReturn()" value="Save"/></center>
</apex:pageBlock>
</apex:form>
<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>
</apex:page>
Controller code for the above page....
public with sharing class AssetController{
public String message {get; set;}
List<Asset> assets = new List<Asset>();
List<Asset> assetvalue= new List<Asset>();
List<OpportunityLineItemwrapper> OppList = new List<OpportunityLineItemwrapper>();
List<OpportunityLineItem> selectedLineItems = new List<OpportunityLineItem>();
Opportunity oppu=[select name,ID,AccountId,StageName from Opportunity where id=:ApexPages.currentPage().getParameters().get('id')];
public List<OpportunityLineItemwrapper> getMyProd()
{
for(OpportunityLineItem o:[Select Id,OpportunityId,PricebookEntry.Name,PricebookEntry.Product2.Name,Quantity, UnitPrice, TotalPrice, ServiceDate, PricebookEntryId,Status__c,Serial_Number__c,Usage_Date__c,Purchase_Date__c From OpportunityLineItem where OpportunityId=:ApexPages.currentPage().getParameters().get('id')])
OppList.add(new OpportunityLineItemwrapper(o));
return OppList;
}
public PageReference getSelected()
{
selectedLineItems.clear();
for(OpportunityLineItemwrapper accwrapper : OppList)
if(accwrapper.selected == true)
selectedLineItems.add(accwrapper.opp);
return null;
}
public class OpportunityLineItemwrapper
{
public OpportunityLineItem opp{get; set;}
public Boolean selected {get; set;}
public OpportunityLineItemwrapper(OpportunityLineItem a)
{
opp = a;
selected = false;
}
}
public PageReference save() {
if((Oppu.StageName== 'Closed Lost' || Oppu.StageName== 'Closed Won')&& Oppu.AccountId != null){
assetvalue=[Select name from Asset where AccountId =: Oppu.AccountId];
Integer i=assetvalue.size();
Integer j;
Integer m=selectedLineItems.size();
Integer n;
if(i==0)
{
for(OpportunityLineItem a: selectedLineItems){
Asset ast=new Asset(AccountId=Oppu.AccountId,Name=a.PricebookEntry.Name,Quantity=a.Quantity,InstallDate=a.ServiceDate,Status=a.Status__c,SerialNumber=a.Serial_Number__c,PurchaseDate=a.Purchase_Date__c,UsageEndDate=a.Usage_Date__c,Price=a.TotalPrice);
Assets.add(ast);
}
insert Assets;
//message = 'The values are succesfully inserted into the assets';
message='i';
System.debug('**********************************************************************i****'+message);
return null;
}
else if(i>0){
for(OpportunityLineItem a: selectedLineItems){
for(n=0;n<m;n++){
for(j=0;j<i;j++){
if(selectedLineItems[n].PricebookEntry.Name==assetvalue[j].name)
{
// message = 'It appears that one or more of these line items have already been converted into assets. The values are duplicated now';
message='j';
System.debug('**********************************************************************j****'+message);
}
}
}
Asset ast=new Asset(AccountId=Oppu.AccountId,Name=a.PricebookEntry.Name,Product2Id = a.PricebookEntry.Product2Id,Quantity=a.Quantity,InstallDate=a.ServiceDate,Status=a.Status__c,SerialNumber=a.Serial_Number__c,PurchaseDate=a.Purchase_Date__c,UsageEndDate=a.Usage_Date__c,Price=a.TotalPrice);
Assets.add(ast);
}
insert Assets;
return null;
}
}
else{
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Cannot process conversion. Make sure the opportunity is closed '));
// message='Cannot process conversion. Make sure the opportunity is closed and/or the opportunity has an account associated with it';
message='k';
System.debug('**********************************************************************k****'+message);
}
return null;
}
}
On save i have to compare the asset list which i have queried with the selected oppline items from VF Page. If there is any match found it should prompt me for confirm VALUES ALREADY IN ASSETS. DO U WANT TO DUPLICATE?. Based on this i have to do save.... If no duplicates found then it should be saved and alert me your values saved.