• Mohammadasif Siddiqui
  • NEWBIE
  • 30 Points
  • Member since 2020

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

I have below Task to complete, can anyone provide me its code.

Create a trigger to check if the account name exists while creating a lead if the record exists then create a new case with the subject account name already exists. 

Please Help me.

Thanks in Advance

<apex:page controller="Task2Apex">
    <apex:form >
        <apex:pageBlock title="Account Name" >
            <apex:selectList value="{!selectedAccId}" size="1">
                <apex:selectOptions value="{!AccountNames}" />
                <apex:actionSupport event="onchange" action="{!showContacts}" reRender="op" />
            </apex:selectList><br/><br/>
            
         <apex:outputPanel id="op">
            <b><apex:outputText value="Related Contacts" rendered="{!renderPBtable}"/></b>
            <b><apex:outputText value="No Contacts on this Account" rendered="{!noContact}" style="color: red;"/></b>
            <apex:pageblockTable title="Contacts" value="{!conlist}" var="sc" id="relatedContactsBlock" rendered="{!renderPBtable}">
                <apex:column value="{!sc.name}"/>
                <apex:column value="{!sc.phone}"/>
            </apex:pageblockTable>
        </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Task2Apex {

    public Id selectedAccId{get;set;} 

    public List<Contact> conlist {get;set;}   
    
    public Boolean renderPBtable {get;set;}
    
    public Boolean noContact {get;set;}

    public List<SelectOption> getAccountNames() {
        List<SelectOption> accOptions= new List<SelectOption>();
        system.debug(selectedAccId);
        accOptions.add( new SelectOption('','--Select--'));
        for(Account acc : [select Id,name from Account ] ) {
            accOptions.add(new SelectOption(acc.Id,acc.name));
        }
        return accOptions;
    }

    public PageReference showContacts(){
        conlist=[select name,phone from Contact where Accountid=: selectedAccId];
        if(conlist.size() > 0)
        {
            renderPBtable = true; 
            noContact = false;
        }
        else
        {
            renderPBtable = false;
            noContact = true;
        }
        return null;   
    }
  
}

I want to Include edit link on each child record.
Thanks in Advance.

I am getting 2 error in below code.

Variable does not exist: conlist

Unknown property 'AccountController1_picklistrendering.conlist'

 

<apex:page controller="AccountController1_picklistrendering">
   <apex:form >
      <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

             <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" reRender="a"/>
             </apex:selectList>

             <br/><br/>

           Related Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <apex:pageblock >
   <apex:pageblockTable value="{!conlist}" var="c" id="a">
       <apex:column value="{!c.firstname}"/>
       <apex:column value="{!c.lastname}"/>
       <apex:column value="{!c.email}"/>
       <apex:column value="{!c.accountid}"/>
   </apex:pageblockTable>
</apex:pageblock>

       </apex:pageBlock>               
    </apex:form>
</apex:page>
 
public with sharing class AccountController1_picklistrendering {
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
      
          public List<SelectOption> getAccountNames() {
                  List<SelectOption> accOptions= new List<SelectOption>();
                  accOptions.add( new SelectOption('','--Select--'));
                  for( Account acc : [select Id,name from Account ] ) {
                          accOptions.add( new SelectOption(acc.Id,acc.name));
                  }
                 return accOptions;
           }
         
      public pagereference getContactNames()
{
    conlist = [select id,firstname,lastname,email,accountid from contact where accountid=:selectedAccId];
    return null;
}
    }

Please Help me.
Thanks in Advance​​​​​​​
<apex:page controller="sample1">
    <apex:pageBlock >
         <apex:pageBlockSection columns="1" >
        <apex:pageBlockTable value="{!acct}" var="a"  >
            
                <apex:column headerValue="Action">
                <apex:outputLink value="{!URLFOR($Action.Hospital__c.Edit, a.Id)}">Edit</apex:outputLink>
            </apex:column>           
            <apex:column value="{!a.Name}" />
          
            <apex:repeat value="{!a.Account__r}" var="c" >                
                <apex:column headerValue="Account Name" value="{!c.Name}"></apex:column>
            </apex:repeat>
        </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>
 
public class sample1
{    
    public List<Hospital__c> acct {get;set;}
    public sample1()
    {
        acct = [SELECT Name, Account__r.Name FROM Hospital__c];
    }    
}


User-added image

I am not getting the header name "Account Name" like Hospital Name 

I have declared headerValue also in Line 12 also

public class HospitalApex{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}

public HospitalApex( ApexPages.StandardController sc ) {
        this.hosp= (Hospital__c)sc.getRecord();
}
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Something'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid  ];
      hosp = new Hospital__c();     
 }   
  }
}
 
<apex:page standardController="Hospital__c" extensions="HospitalApex">
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>
I want to save record entered in text field in table but when i click on Save button, nothing happens
 
<apex:page controller="HospitalController" >
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>
 
public class HospitalController{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}

public HospitalController( ApexPages.StandardController sc ) {
        this.hosp= (Hospital__c)sc.getRecord(); }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Something'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid  ];
        hosp = new Hospital__c();     
 }   
  }
}

I am getting error 
Unknown constructor 'HospitalController.HospitalController()'

Please help me
<apex:page controller="Hospital" >
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>
 
public class Hospital{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}
 public Hospital()
    {
        hosp = new Hospital__c();
    }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
     
 }   
  }
}



Insert failed. First exception on row 0 with id a0B2w000000hYugEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!insertNewItem}' in component <apex:commandButton> in page task1hospital: Class.Hospital.insertNewItem: line 17, column 1
 

I am getting above error, Please help me

Thanks in Advance

I am getting 2 error in below code.

Variable does not exist: conlist

Unknown property 'AccountController1_picklistrendering.conlist'

 

<apex:page controller="AccountController1_picklistrendering">
   <apex:form >
      <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

             <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" reRender="a"/>
             </apex:selectList>

             <br/><br/>

           Related Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <apex:pageblock >
   <apex:pageblockTable value="{!conlist}" var="c" id="a">
       <apex:column value="{!c.firstname}"/>
       <apex:column value="{!c.lastname}"/>
       <apex:column value="{!c.email}"/>
       <apex:column value="{!c.accountid}"/>
   </apex:pageblockTable>
</apex:pageblock>

       </apex:pageBlock>               
    </apex:form>
</apex:page>
 
public with sharing class AccountController1_picklistrendering {
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
      
          public List<SelectOption> getAccountNames() {
                  List<SelectOption> accOptions= new List<SelectOption>();
                  accOptions.add( new SelectOption('','--Select--'));
                  for( Account acc : [select Id,name from Account ] ) {
                          accOptions.add( new SelectOption(acc.Id,acc.name));
                  }
                 return accOptions;
           }
         
      public pagereference getContactNames()
{
    conlist = [select id,firstname,lastname,email,accountid from contact where accountid=:selectedAccId];
    return null;
}
    }

Please Help me.
Thanks in Advance​​​​​​​
<apex:page controller="Hospital" >
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>
 
public class Hospital{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}
 public Hospital()
    {
        hosp = new Hospital__c();
    }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
     
 }   
  }
}



Insert failed. First exception on row 0 with id a0B2w000000hYugEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!insertNewItem}' in component <apex:commandButton> in page task1hospital: Class.Hospital.insertNewItem: line 17, column 1
 

I am getting above error, Please help me

Thanks in Advance