• cloudForceDev
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

I have too many incoming Client_Chat__c records created from Lead, which is causing troubles with my scheduled Apex. I need to rewrite this, but I don't see how I can avoid doing a query for each Client_Chat__c. Leads come in, a new uniqu chat record is created and the lead is marked as "TRASH"

If I get duplicate records, I want to mark them as "TRASH" before deletion.

 

global class ScheduledDelete implements Schedulable {
        
    global void execute (Schedulablecontext SC) {
        
        //Begin daily lead deletion.      
                
        date myDate = system.Today();
        
        Client_Chat__c[] savedChats =[Select ID, ChatID__c FROM Client_Chat__c WHERE CreatedDate = LAST_N_DAYS:1];
        system.debug('savedChats&&&: ' + savedChats);
        
        // Check for duplicate leads
            for(Client_Chat__c savec : savedChats){
                
                Lead[] copiedLeads = [SELECT Id, TempCode__c From Lead WHERE ApexChatID__c =:savec.ChatId__c];
                system.debug('copiedLeads***: ' + copiedLeads);
                
                for (Lead cl : copiedleads){
                    cl.TempCode__c = 'TRASH';
                    update cl;
                }                
            }
            
        
        Lead[] deleteLeads = [SELECT Id FROM Lead WHERE TempCode__c = 'TRASH'];
        
        delete deleteLeads;
    }        
}

 

  • August 30, 2012
  • Like
  • 0

hi guys ,

 

i am using a page block section for creating new and edit when creating new it works fine.

when editing the record its creating another record .Iam using upsert on the save button .

-------------Controller-------------------------

Public Class lComments{
public lead l {get;set;}
public list<Comments__c> Com{get; set;}
public Comments__c c{get;set;}
public Boolean property {get; set;}
public Boolean showblock{get;set;}
public Boolean hidebutton{get;set;}
public String SelId { get; set; }
public String delid{get;set;}
id str{get;set;}
public map<id,comments__c> mpid{get;set;}

public LComments(ApexPages.StandardController controller) {
l=(Lead)controller.getRecord();
str=Apexpages.currentpage().getparameters().get('id');
property=false;
hidebutton=true;
c=new Comments__c();
loaddata();
}
public void loaddata(){
com=[select id,name,lead__c,Contact__c,Opportunity__c,Finance__c,Comments_text__c,lead__r.Name ,Role__c ,CreatedDate from comments__c  where lead__c=:str order by CreatedDate desc limit 1000];
mpid=new map<id,Comments__c>([Select id, name from comments__c where id =:selid ]);
for(comments__c d:com){
mpid.put(d.id,d);

}
}

public PageReference save1(){
Comments__c c=new Comments__c(Name=c.Name,Comments_text__c=c.Comments_text__c,Lead__c=str);
upsert c;
PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
pageRef.setRedirect(true);
return pageRef;
}

public Comments__c getc(){
property=true;
hidebutton=false;
c=new comments__c();
return null;
}

Public void edit1(){
Property=true;
c=New Comments__c();
if(mpid.containsKey(SelId)){
c.Name=mpid.get(SelId).Name;
c.Comments_text__c=mpid.get(SelId ).Comments_text__c;
}

}

public Comments__c del1(){
Com=[select id from comments__c where id=:delid];
if(com.size()>0){
delete com;
}
loaddata();
return null;

}
}

----------------------Vf page----------------------------------------

--------
<apex:page standardController="Lead" extensions="lComments" >
<apex:form id="main" >
<table border="5">
<tr><td>
<div style="overflow: auto; height: 250px; width:500px;">
<apex:outputText value="{!Lead.Name}" style="font-size:12px;"/>
</div>
</td><td>
<div style="overflow: auto; height: 250px; width:500px;" >

<div id="parentdiv" style="text-align:center;width:30px;margin:auto;">
<apex:commandButton value="New" action="{!getc}" reRender="main,ref" rendered="{!hidebutton}" />
</div>

<apex:pageBlock rendered="{!Property}" id="ref" >
<apex:pageBlockSection columns="1" >
<apex:inputField value="{!c.Name}" required="true" />
<apex:inputField value="{!c.Comments_text__c}" style="width:380px;height:100px" />
</apex:pageBlockSection>
<apex:pageBlockButtons location="Bottom">
<apex:commandbutton action="{!Save1}" title="Save" value="Save" />
<apex:commandbutton action="{!cancel}" title="cancel" value="Cancel" onclick="window.history.previous()" />
</apex:pageBlockButtons>
</apex:pageBlock>

<apex:pageBlock >
<apex:pageBlockTable value="{!Com}" var="p" style="table-layout:fixed;" >

<apex:column width="100px" >
<apex:commandLink value="Edit" action="{!edit1}" reRender="main,ref" >
<apex:param name="cid" value="{!p.id}" assignTo="{!selid}"/>
</apex:commandlink>
&nbsp;&nbsp;&nbsp;&nbsp;
<apex:commandLink value="Del" action="{!del1}" reRender="main">
<apex:param name="cid" value="{!p.id}" assignTo="{!delid}"/>
</apex:commandLink>
</apex:column>

<apex:column style="overflow: hidden;">
<apex:facet name="header">Name</apex:facet>
{!If(LEN(p.Name)<25,p.Name,LEFT(p.Name,25))}
</apex:column>

<apex:column style="overflow: hidden;height:20px; ">
<apex:facet name="header">Comments</apex:facet>
{!If(LEN(p.Comments_text__c)<100,p.Comments_text__c,LEFT(p.Comments_text__c,100))}
</apex:column>
<apex:column value="{!p.Role__c}" />
<apex:column value="{!p.Lead__c}" />
<apex:column value="{!p.CreatedDate}" />
</apex:pageBlockTable>
</apex:pageBlock>
</div>
</td></tr>
</table>
</apex:form>
</apex:page>

------------------

Thanks

Anil.B