• dmani
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 7
    Replies
Hi everyone,

we have developed a VF page to add campaign members from one campaign to another campaign. we have designed two paegs for this. we are getting a View State error in second page since the query fetched more than 2500 records. We are facing view state error for Wrapper list namely  'camMemListFinal '

Class:

public with sharing class addCampaignmember {

    transient private List<CampaignMember> camMem = new List<CampaignMember>();
    transient private List<CampMembWrapper> camMemListFinal {get;set;}
    Public String Currentid {get;set;}
    Public String StatusId {get;set;}
    Public String RadioButId {get;set;}
    Public String cmdId {get;set;}
    Public Set<Id> campaignMemb = new Set<Id>(); 
    public List<Contact> conToAdd = new List<Contact>();
    public List<CampaignMember> camMemToAdd = new List<CampaignMember>();
    public List<CampaignMember> finalCMList = new List<CampaignMember>();
    public List<CampaignMember> preExistCMList = new List<CampaignMember>();
    public Set<Id> campSet = new Set<Id>();
    transient private List<CampMembWrapper> camMemList = new List<CampMembWrapper>();
    public Integer camsize;
    
    //pagination
    public List<CampMembWrapper> PageList = new List<CampMembWrapper>();
    transient public List<CampMembWrapper> ResultList  = new List<CampMembWrapper>();
    
    private Integer pageNumber;
    private Integer pageSize;
    private Integer totalPageNumber;
    
    public addCampaignmember (ApexPages.StandardController controller) 
    {
    
    
        Currentid = ApexPages.CurrentPage().getParameters().get('CId');
        preExistCMList = [Select Id, ContactId from CampaignMember where CampaignId =: Currentid];
       
       
       
        for(Integer i=0; i<preExistCMList.size(); i++)
        {
            campSet.add(preExistCMList[i].ContactId);
        }
        
        
        camMem = [SELECT ContactId, 
                         Contact.Name, 
                         Contact.AccountId,
                         Id,Contact.Email,Contact.Phone,
                         Status,
                         CampaignId 
                         From CampaignMember  
                         WHERE CampaignId =:ApexPages.CurrentPage().getParameters().get('CmId')
                         and ContactId Not In: campSet];
                         
         cmdId =  ApexPages.CurrentPage().getParameters().get('CmId');   
         StatusId = ApexPages.CurrentPage().getParameters().get('StId');
         RadioButId = ApexPages.CurrentPage().getParameters().get('RbId');
        
        //pagination 
        pageNumber = 0;
        totalPageNumber = 0;
        pageSize = 15;
        viewData(); 
         
    }
    
    public List<CampMembWrapper> getcamMemList()
    {
      //  List<CampMembWrapper> camMemList = new List<CampMembWrapper>();
        
        
         
                                         
        for(CampaignMember campMemb : camMem)
        {
            CampMembWrapper cm = new CampMembWrapper(campMemb);
           
            cm.Selected = true;
            camMemList.add(cm);
        }  
        
        camMemListFinal = camMemList;
        return camMemList;
    }
    
    public pageReference addCampMembers()
    {
        
        
        
        camsize = camMemListFinal.size();
         system.debug('xxxx'+camsize);*/
         
        for(integer i=0; i<camMemListFinal.size(); i++)
        {
            system.debug('ggdhghushj' + camMemListFinal[i].selected);
            if(camMemListFinal[i].selected == True)
            {
                //camMemListFinal[i].campMem.Status =  StatusId;
                //camMemListFinal[i].campMem.CampaignId = Currentid;      
                campaignMemb.add(camMemListFinal[i].campMem.ContactId);
            }
        }
        // This part of code fetches the contact records which are in Active state 

        conToAdd = [Select Id, Name from Contact where Id In: campaignMemb and Active__c = true];
        
        for(Integer i=0; i<conToAdd.size(); i++)
        {
            camMemToAdd.add(new CampaignMember(ContactId=conToAdd[i].Id,CampaignId=CurrentId,Status=StatusId));
        }
        
        
        if(CamMemToAdd != null)
        {
            Database.SaveResult[] SaveResult_campMember = Database.Insert(camMemToAdd);
        }
                
        if(RadioButId == 'Override the member status')
        {
            finalCMList = [Select Id, CampaignId, Status from CampaignMember where CampaignId =: Currentid];
            for(Integer i=0; i<finalCMList.size(); i++)
            {
                if(finalCMList[i].Status != StatusId)
                {
                    finalCMList[i].Status = StatusId;
                }
            }
            update finalCMList;
        }

        PageReference firstPage = new PageReference('/'+Currentid);
        firstPage.setRedirect(true);
        return firstPage;
    }
    
    public pageReference goBack()
    {
        PageReference selectCampPage = new PageReference('/apex/addCampaignMembers?Id=' + CurrentId);  
        selectCampPage.setRedirect(true);
        return selectCampPage;
    }  

    public class CampMembWrapper 
    {
        public CampaignMember campMem         { get; set; }
        public boolean Selected     { get; set; }
        CampMembWrapper(CampaignMember c)
        {
            this.campMem = c;
        }
    }
    
   //Pagination
   
   public List<CampMembWrapper> getResultList()
    {
        return PageList;
    }
   
   public Integer getPageNumber()
    {
        return pageNumber; 
    }

     public Integer getPageSize()
    {
        return pageSize;
    }
    
    public Boolean getPreviousButtonEnabled()
    {
        return !(pageNumber > 1);
    }
    
     public Boolean getNextButtonDisabled()
    {
        if (ResultList == null) return true;
        else
                return ((pageNumber * pageSize) >= ResultList.size());
    }
    
    public Integer getTotalPageNumber()
    {
         system.debug('xxxYYY'+camsize);
        if (totalPageNumber == 0 && ResultList !=null)
        {
            totalPageNumber = camMemListFinal.size() / pageSize;
            Integer mod = camMemListFinal.size() - (totalPageNumber * pageSize);
            if (mod > 0)
            totalPageNumber++;
        }


        return totalPageNumber;
    }
    
     
     
     public PageReference ViewData()
    { 
        ResultList = getcamMemList();
        totalPageNumber = 0;
        BindData(1);
        return null;
    }
    
     public void BindData(Integer newPageIndex)
    {
        try
        {
            PageList = new List<CampMembWrapper>();
            
            system.debug('*****************'+ ResultList);
            Transient Integer counter = 0;
            Transient Integer min = 0;
            Transient Integer max = 0;
    
            if (newPageIndex > pageNumber)
            {
                min = pageNumber * pageSize;
                max = newPageIndex * pageSize;
            }
            else
            {
                max = newPageIndex * pageSize;
                min = max - pageSize;
             
            }
    
            for(CampMembWrapper a : ResultList)
            {
                counter++;
                if (counter > min && counter <= max) 
                        PageList.add(a);
            }
    
            pageNumber = newPageIndex;
    
            if (PageList == null || PageList.size() <= 0)
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Data not available for this view.'));
    
        }
        catch(Exception ex)
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,ex.getMessage()));
        }
    
    }
    
    public PageReference nextBtnClick() 
    {
        BindData(pageNumber + 1);
        return null;
    }
    
    public PageReference previousBtnClick() 
    {
        BindData(pageNumber - 1);
        return null;
    }
     
   
}

VF page : 

<apex:page standardController="Campaign" id="addcampaign" extensions="addCampaignmember" readonly="true">
    <script>
        function CheckUncheckFacet(obj)
        {      
            document.getElementById('addContact:form1:block1:listUsers:datatableId:selectall').checked = false;               
            if(obj.checked)
                intSelectedItemsCount = intSelectedItemsCount + 1;
            else
                intSelectedItemsCount = intSelectedItemsCount - 1;
        }
        function selectChecked(form,value)
        {   
            var i = 0;
            for (i = 0; i < form.elements.length; i++) {
               
                if (form.elements[i].name.search('selected') > 0)
                    (document.getElementById(form.elements[i].name)).checked = value;
                /*if (form.elements[i].name.search('selected') > 0 && form.elements[i].disabled == false) {
                    if (form.elements[i].checked != value){
                        if (value)
                            intSelectedItemsCount = intSelectedItemsCount + 1;
                        else
                            intSelectedItemsCount = intSelectedItemsCount - 1;
                    }                                               
                    form.elements[i].checked = value;
                }*/
            }
        }
        function selectAllOrNoneByCheckbox(form,control)
        {   
           selectChecked(form, control.checked); 
        }
        function closeWindow()
        {
            top.window.close();
            parent.window.opener.location.reload();
        }
        
        function baktoCamp(campId) {
    
            top.window.opener.location.href="/"+campId;
        }    
    </script>
    <apex:sectionHeader title="Campaign" subtitle="Add Members" />
    
    <apex:form id="form1">
        <apex:pageBlock id="pageBlock">
            <apex:pageBlockSection Title="Step 2. Select Campaign Members to be Added" id="listUsers" columns="1">
                <apex:PageBlockTable value="{!ResultList}" var="c" border="1" id="datatableId">
                    <apex:column >
                        <apex:facet name="header" >
                            <apex:inputCheckbox id="selectall" onclick="javascript:selectAllOrNoneByCheckbox(document.forms['addcampaign:form1'], this);" selected="true"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!c.selected}" onclick="javascript:CheckUncheckFacet(this)"  id="selected" />   
                    </apex:column>
                    <apex:column headervalue="Name" value="{!c.campMem.Contact.Name}" />
                    <apex:column headervalue="Account Location      " value="{!c.campMem.Contact.AccountId}" /> 
                    <apex:column headervalue="Status" value="{!c.campMem.status}" />                    
                    <!--  <apex:column headervalue="Campaign" value="{!c.campMem.Campaign.Name}" />-->
                    <apex:column headervalue="Phone Number" value="{!c.campMem.Contact.Phone}" />
                    <apex:column headervalue="E-mail" value="{!c.campMem.Contact.Email}" />
                </apex:PageBlockTable>
            </apex:pageBlockSection>  
            <br/>
            <div align="right">
                <br/>
                <font size="1pt">Page #:&nbsp;<apex:outputLabel value="{!PageNumber}"/>&nbsp;out of&nbsp;<apex:outputLabel value="{!totalPageNumber}"/>&nbsp;&nbsp;&nbsp;&nbsp;</font>
                <apex:commandButton value="Previous" action="{!previousBtnClick}" disabled="{!previousButtonEnabled}" reRender="pageBlock"></apex:commandButton>
                <apex:commandButton value="Next" action="{!nextBtnClick}" reRender="pageBlock" disabled="{!nextButtonDisabled}"></apex:commandButton>
            </div>
            <br/>
            <br/>
        
            <table width="500px">
                <tr>
                    <td align="right">
                    <apex:commandButton value="Add Members" action="{!addCampMembers}" 
                            onComplete="baktoCamp('{!Currentid}'); closeWindow();" />
                    <apex:commandButton value="Back" action="{!goBack}"/>
                    </td>
                </tr>
            </table>
        </apex:pageBlock>  
    </apex:form>
    <script>
        function backToCampaign(campId) 
        {
            top.window.opener.location.href="/"+campId;
        }    
    </script>
</apex:page>


Could anyone suggest a solution to solve this view state issue. I have tried with Transient Keyword. But the wrapper class list gives null point exception since i am using transient .

Tried with StandardsetController to fetch the records , but Standard set controller is not supported for Campaign Members Object.
 Please suggest a Solution ASAP. Any help is much appreciated. 
  • November 03, 2014
  • Like
  • 0

Hi,

 

I have a requirement to disable few lead records checkbox using wrapper class.

 

I have used disabled attribute to check some conditions and if the conditions returns true, it should disable the checkbox of tat particular lead records and remaining lead records should not be disabled.

 

Still i am not able to achieve it. Could some one help me.

 

class

public String UserRegion{get;set;}

currentUser = [select id,email,name,GCAM_Region__c from User where id =: userInfo.getUserId()];
         if(currentUser.GCAM_Region__c != null)
         {
         UserRegion = currentUser.GCAM_Region__c.substring(0,3);
         system.debug('XXX'+UserRegion );
         }

 

 

class LeadWrapper
    {
         public Boolean isSelected{get; set;}
         public Lead ld{get; set;}
         
         public LeadWrapper(Lead l,Boolean flag)
         {
              ld = l;
              isSelected = flag;
         }
    }
   

 

VF

<apex:pageBlockTable value="{!allLeads}" var="leadVar">
                                     <apex:column width="20px">
                                       <apex:facet name="header">
                                          <input type="checkbox" name="chk" id="chk" onclick='masterCheck(this.checked);'/>
                                       </apex:facet>
                                       <apex:inputCheckbox value="{!leadVar.isSelected}" onclick="childCheck(this.checked)" disabled="{IF(UserRegion!=leadVar.ld.LTC_Leads_Region_Name__r.Name,True,False)}" title="Please contact Retail Leads Team for Transfer of this Lead"/>
                                     </apex:column>

 

if the current user Region code is not matching with the leads Region Code, then the checkbox for that lead should be disabled.

 

Please help me to find out how to handle it if disabled attribute doesnt support.

 

  • September 03, 2013
  • Like
  • 0

Hi,

 

I am getting below error when i run the test class.

 

System.QueryException: List has no rows for assignment to SObject

 

Error is in below line in controller:

 

User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];

 

 

From Visualforce Page i am passing the UserId to controller. IN test method iam not able to pass the UserId in the Above Query , hence Query fetches 0 Row and error occurs. How to Pass the Userid in test method. I tried using

 

apexpages.currentpage().getparameters().put('lecord.Id',Userid.id);

 

but still it fails. Guide me how to pass Pls.

  • August 13, 2013
  • Like
  • 0

Hi,

 

I am facing error while running the test class in a function saveTask(). 

 

the query fails and returns no value. please suggest ur opinions. the code is below

 

 

test class:

 

@isTest(seeAllData = false)
public class TransferLeadsTests {

public static testMethod void testTransferLeads() {
Profile p = [SELECT Id,Name FROM Profile Limit 1];
User u = new User(Alias = 'test', Email='test@gmail.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='test@testorg.com');
insert u;

User Userid = [Select id,Name,Email from User where Alias = 'test' and Email = 'test@gmail.com'];

User u1 = [SELECT Id,Name,Email FROM User Where Id = :UserInfo.getUserId()];
RecordType RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];

TransferLeads controller = new TransferLeads() ;

controller.getUser1();

List<Lead> ldlist = new List<Lead>();
Lead ld = new Lead ();
ld.OwnerId = u1.Id;
ld.FirstName= 'testing';
ld.Lastname='test';
ld.Company = 'test';
ld.LTC_Returned_Date__c = system.Today();
ld.RTL_Lead_Assigned__c =system.Now();
ldlist.add(ld);
insert ldlist;
system.debug('AAA'+ldlist.size());
System.AssertEquals(ldlist.size()==1, true);

apexpages.currentpage().getparameters().put('id', ld.Id);
controller.getLead();

apexpages.currentpage().getparameters().put('lecord.OwnerId',Userid.id);
controller.getLeadId();

User unamename = [Select id,Name,Email from User where id=:Userid.id Limit 1];

//User u3 = [Select id, Name from User where id = :Userid.Id Limit 1];


Task t = new Task();
t.Subject ='test';
t.RecordTypeId = RecordtypeName.Id;
t.WhoId =ld.Id;
t.ActivityDate= system.Today();
t.Transfer_agent_s_name__c=ld.Owner.Name;
t.RI_Date_of_Creation__c=system.now();
t.Status='Completed';
ld.OwnerId=unamename.id;


datetime dT =ld.RTL_Lead_Assigned__c.addDays(179);
Date returnDate = date.newinstance(dT.year(), dT.month(), dT.day());
ld.LTC_Returned_Date__c = returnDate ;
//Lead leads = [Select id,Name,LTC_Returned_Date__c,RTL_Lead_Assigned__c from Lead where id =:ld.Id Limit 1];
ld.RTL_Lead_Assigned__c =system.Now();
//ld.OwnerId=Userid.Id;

try{
insert t;
update ld;
system.debug('CCC'+ld.LTC_Returned_Date__c);
System.AssertEquals(ld.LTC_Returned_Date__c!=Null,true);

}
catch(exception e){
system.debug('ZZZ'+ld.LTC_Returned_Date__c);
}

List<task> insertedtask = new List<task> ();
insertedtask = [Select id, Subject,WhoId from Task where WhoId = :ld.id and RecordTypeId =:RecordtypeName.Id Limit 1] ;
system.debug ('GGG' +insertedtask[0].id);

controller.saveTask();

controller.cancel();
}
}

 

 

Class:

 

public with sharing class TransferLeads{

public Lead leadRecord{get;set;}
public User usertransfering{get;set;}
public RecordType RecordtypeName{get;set;}
public String Taskt{get;set;}
public String UId{get;set;}
public string Uname {get;set;}
public String Usertype{get;set;}
public String searchText{get;set;}
public Lead lecord {get;set;}
public User userRecord{get;set;}
public string NewUserID{get;set;}
//public string searchText{get;set;}
public List<User> results{get;set;}
public String NewOwner{get;set;}

 

 

public TransferLeads()
{
usertransfering = [Select Name, Id from User Where Id = :UserInfo.getUserId()];
RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];
lecord = new Lead();
}

public Lead getLead()
{
leadRecord =[SELECT Id,Name,OwnerId,FirstName,Lastname,Owner.Name,Owner.Email,LTC_Returned_Date__c,RTL_Lead_Assigned__c FROM Lead WHERE Id =:ApexPages.currentPage().getParameters().get('Id')];
return leadRecord ;
}

public User getUser1(){
return usertransfering;
}

public PageReference cancel()
{
PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;
}


public PageReference saveTask()
{
system.debug('XXXXX'+Uname);


User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];
system.debug('RRRRR'+unamename.id);


Task Transfertask = new Task(Subject = Taskt,RecordTypeId = RecordtypeName.Id,WhoId =leadRecord.Id,Transfer_agent_s_name__c=leadRecord.Owner.Name,RI_Date_of_Creation__c=system.now(),ActivityDate= system.Today(),Status='Completed',Receiving_Agent_s_Name__c =unamename.Name);


leadRecord.LTC_Returned_Date__c = Date.today() + 179;
leadRecord.OwnerId=unamename.id;
system.debug('Transfertask.Subject-----------------------------------:' + Transfertask.Subject);
if(Taskt==null || Taskt.length() == 0 )
{
system.debug('Error validation');
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Subject is required'));
return null;

}

try{
insert Transfertask;
update leadRecord;
}

catch (DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}

 

PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;


}

 

}

 

VF Page:

 

<apex:page Controller="TransferLeads" sidebar="false" id="page">

<apex:form id="formid">

<apex:pageBlock title="New Task" id="thePageBlock" mode="edit">
<apex:pageMessages ></apex:pageMessages>

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveTask}" />
<apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:pageBlockSection id="pageBlockSection" title="Task Information" columns="1">

<apex:pageBlockSectionItem >
<apex:outputLabel value="Lead Name"/>
<apex:OutputField value="{!Lead.Name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Current owner"/>
<apex:OutputField value="{!Lead.Owner.Name}"/>
</apex:pageBlockSectionItem>

<apex:inputField value="{!lecord.ownerid}" required="true" / >



<apex:pageblockSectionItem id="taskid" >
<apex:outputLabel value="Subject" />
<apex:outputPanel id="panel" >
<div class="requiredInput">
<div class="requiredBlock"></div>
<apex:inputText label="Subject" value="{!Taskt}" />
</div>

</apex:outputPanel>
</apex:pageblockSectionItem>





<apex:pageblockSectionItem >
<apex:outputLabel value=" Who is Transfering" />
<apex:inputField value="{!User1.Name}"/>
</apex:pageblockSectionItem>




</apex:pageBlockSection>
</apex:actionRegion>
</apex:pageBlock>
</apex:form>

</apex:page>

 

I am getting below error when i run the test class.

 

System.QueryException: List has no rows for assignment to SObject

 

the below line is the cause of error

 

User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];

 

and the  Savetask() call in test class causes the error.

 

Help me to solve this and achieve 75 %. As of now without SaveTask() function i am bale to achieve 65%.

 

 

 

 

 

  • August 13, 2013
  • Like
  • 0

Hi

 

i need to display custom error message for a required inputtext. How is it achievable.

 

I have tried

 

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
return null;

 

in controller method

 

and

 

<apex:pageBlock>

<apex:pageMessages ></apex:pageMessages>

<apex:pageblockSectionItem id="taskid" >
                            <apex:outputLabel value="Subject" />
                            <apex:outputPanel id="panel"  >
                                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:inputText label="Subject" value="{!Taskt}" required="true"/>
                </div>
                
                </apex:outputPanel>
                    </apex:pageblockSectionItem>

</apex:pageBlock>

 

  in page.

 

But its not helpful . kindly provide your ideas.

 

 

  • August 12, 2013
  • Like
  • 0

Hi,

 

I need the code similar to Change Owner Link  next to Lead Owner.

 

My requirement is tat in a Lead detail page,  i do have a Custom Button called "Transfer" . On click of the button i need to redirect to a VF page where i need to change the lead Owner field. When i click on the lookup of Owner field which is a inputfield i can search the Users. But i need portal users and users with someother roles too to be included in search . How to achieve it . Kindly help me. this is a ASAP requirement. Thanks in advance.

  • August 06, 2013
  • Like
  • 0

Hi,

 

I am getting below error when i run the test class.

 

System.QueryException: List has no rows for assignment to SObject

 

Error is in below line in controller:

 

User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];

 

 

From Visualforce Page i am passing the UserId to controller. IN test method iam not able to pass the UserId in the Above Query , hence Query fetches 0 Row and error occurs. How to Pass the Userid in test method. I tried using

 

apexpages.currentpage().getparameters().put('lecord.Id',Userid.id);

 

but still it fails. Guide me how to pass Pls.

  • August 13, 2013
  • Like
  • 0

Hi,

 

I am facing error while running the test class in a function saveTask(). 

 

the query fails and returns no value. please suggest ur opinions. the code is below

 

 

test class:

 

@isTest(seeAllData = false)
public class TransferLeadsTests {

public static testMethod void testTransferLeads() {
Profile p = [SELECT Id,Name FROM Profile Limit 1];
User u = new User(Alias = 'test', Email='test@gmail.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='test@testorg.com');
insert u;

User Userid = [Select id,Name,Email from User where Alias = 'test' and Email = 'test@gmail.com'];

User u1 = [SELECT Id,Name,Email FROM User Where Id = :UserInfo.getUserId()];
RecordType RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];

TransferLeads controller = new TransferLeads() ;

controller.getUser1();

List<Lead> ldlist = new List<Lead>();
Lead ld = new Lead ();
ld.OwnerId = u1.Id;
ld.FirstName= 'testing';
ld.Lastname='test';
ld.Company = 'test';
ld.LTC_Returned_Date__c = system.Today();
ld.RTL_Lead_Assigned__c =system.Now();
ldlist.add(ld);
insert ldlist;
system.debug('AAA'+ldlist.size());
System.AssertEquals(ldlist.size()==1, true);

apexpages.currentpage().getparameters().put('id', ld.Id);
controller.getLead();

apexpages.currentpage().getparameters().put('lecord.OwnerId',Userid.id);
controller.getLeadId();

User unamename = [Select id,Name,Email from User where id=:Userid.id Limit 1];

//User u3 = [Select id, Name from User where id = :Userid.Id Limit 1];


Task t = new Task();
t.Subject ='test';
t.RecordTypeId = RecordtypeName.Id;
t.WhoId =ld.Id;
t.ActivityDate= system.Today();
t.Transfer_agent_s_name__c=ld.Owner.Name;
t.RI_Date_of_Creation__c=system.now();
t.Status='Completed';
ld.OwnerId=unamename.id;


datetime dT =ld.RTL_Lead_Assigned__c.addDays(179);
Date returnDate = date.newinstance(dT.year(), dT.month(), dT.day());
ld.LTC_Returned_Date__c = returnDate ;
//Lead leads = [Select id,Name,LTC_Returned_Date__c,RTL_Lead_Assigned__c from Lead where id =:ld.Id Limit 1];
ld.RTL_Lead_Assigned__c =system.Now();
//ld.OwnerId=Userid.Id;

try{
insert t;
update ld;
system.debug('CCC'+ld.LTC_Returned_Date__c);
System.AssertEquals(ld.LTC_Returned_Date__c!=Null,true);

}
catch(exception e){
system.debug('ZZZ'+ld.LTC_Returned_Date__c);
}

List<task> insertedtask = new List<task> ();
insertedtask = [Select id, Subject,WhoId from Task where WhoId = :ld.id and RecordTypeId =:RecordtypeName.Id Limit 1] ;
system.debug ('GGG' +insertedtask[0].id);

controller.saveTask();

controller.cancel();
}
}

 

 

Class:

 

public with sharing class TransferLeads{

public Lead leadRecord{get;set;}
public User usertransfering{get;set;}
public RecordType RecordtypeName{get;set;}
public String Taskt{get;set;}
public String UId{get;set;}
public string Uname {get;set;}
public String Usertype{get;set;}
public String searchText{get;set;}
public Lead lecord {get;set;}
public User userRecord{get;set;}
public string NewUserID{get;set;}
//public string searchText{get;set;}
public List<User> results{get;set;}
public String NewOwner{get;set;}

 

 

public TransferLeads()
{
usertransfering = [Select Name, Id from User Where Id = :UserInfo.getUserId()];
RecordtypeName = [Select Id ,RecordType.Name from RecordType where RecordType.Name =:'Transfer Lead Task' and SObjectType=:'Task' Limit 1];
lecord = new Lead();
}

public Lead getLead()
{
leadRecord =[SELECT Id,Name,OwnerId,FirstName,Lastname,Owner.Name,Owner.Email,LTC_Returned_Date__c,RTL_Lead_Assigned__c FROM Lead WHERE Id =:ApexPages.currentPage().getParameters().get('Id')];
return leadRecord ;
}

public User getUser1(){
return usertransfering;
}

public PageReference cancel()
{
PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;
}


public PageReference saveTask()
{
system.debug('XXXXX'+Uname);


User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];
system.debug('RRRRR'+unamename.id);


Task Transfertask = new Task(Subject = Taskt,RecordTypeId = RecordtypeName.Id,WhoId =leadRecord.Id,Transfer_agent_s_name__c=leadRecord.Owner.Name,RI_Date_of_Creation__c=system.now(),ActivityDate= system.Today(),Status='Completed',Receiving_Agent_s_Name__c =unamename.Name);


leadRecord.LTC_Returned_Date__c = Date.today() + 179;
leadRecord.OwnerId=unamename.id;
system.debug('Transfertask.Subject-----------------------------------:' + Transfertask.Subject);
if(Taskt==null || Taskt.length() == 0 )
{
system.debug('Error validation');
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Subject is required'));
return null;

}

try{
insert Transfertask;
update leadRecord;
}

catch (DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}

 

PageReference pageRef = new PageReference('/'+leadRecord.Id);
pageRef.setRedirect(true);
return pageRef;


}

 

}

 

VF Page:

 

<apex:page Controller="TransferLeads" sidebar="false" id="page">

<apex:form id="formid">

<apex:pageBlock title="New Task" id="thePageBlock" mode="edit">
<apex:pageMessages ></apex:pageMessages>

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!saveTask}" />
<apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:pageBlockSection id="pageBlockSection" title="Task Information" columns="1">

<apex:pageBlockSectionItem >
<apex:outputLabel value="Lead Name"/>
<apex:OutputField value="{!Lead.Name}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Current owner"/>
<apex:OutputField value="{!Lead.Owner.Name}"/>
</apex:pageBlockSectionItem>

<apex:inputField value="{!lecord.ownerid}" required="true" / >



<apex:pageblockSectionItem id="taskid" >
<apex:outputLabel value="Subject" />
<apex:outputPanel id="panel" >
<div class="requiredInput">
<div class="requiredBlock"></div>
<apex:inputText label="Subject" value="{!Taskt}" />
</div>

</apex:outputPanel>
</apex:pageblockSectionItem>





<apex:pageblockSectionItem >
<apex:outputLabel value=" Who is Transfering" />
<apex:inputField value="{!User1.Name}"/>
</apex:pageblockSectionItem>




</apex:pageBlockSection>
</apex:actionRegion>
</apex:pageBlock>
</apex:form>

</apex:page>

 

I am getting below error when i run the test class.

 

System.QueryException: List has no rows for assignment to SObject

 

the below line is the cause of error

 

User unamename = [Select id,Name,Email from User where id=:lecord.ownerid Limit 1];

 

and the  Savetask() call in test class causes the error.

 

Help me to solve this and achieve 75 %. As of now without SaveTask() function i am bale to achieve 65%.

 

 

 

 

 

  • August 13, 2013
  • Like
  • 0

Hi

 

i need to display custom error message for a required inputtext. How is it achievable.

 

I have tried

 

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Subject is required'));
return null;

 

in controller method

 

and

 

<apex:pageBlock>

<apex:pageMessages ></apex:pageMessages>

<apex:pageblockSectionItem id="taskid" >
                            <apex:outputLabel value="Subject" />
                            <apex:outputPanel id="panel"  >
                                <div class="requiredInput">
                    <div class="requiredBlock"></div>
                    <apex:inputText label="Subject" value="{!Taskt}" required="true"/>
                </div>
                
                </apex:outputPanel>
                    </apex:pageblockSectionItem>

</apex:pageBlock>

 

  in page.

 

But its not helpful . kindly provide your ideas.

 

 

  • August 12, 2013
  • Like
  • 0

I have a Vf page where I have pageblocktable of Account with checkbox.

In account I have  'doctype' field whose values could be 'hyp' or 'wel' or 'hyp and wel'

 

I have a 'reptype' field in User object suppose 'hyp'

 

In pageblocktable I need checkbox to be disabled according to rep type.

 

For ex,

 

1----If user reptype is 'hyp only', then disable accounts with value 'hype + wel' and 'hyp heavy'

2----If user reptype is 'wel', then disable accounts with value 'wel heavy' and 'well + hype'