function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KyoKyo 

I have a problem with the Delete Record and Standard Field.

Hey,

I created the AddRow () Function. but I can not delete a Record.

 

 

 

public class NewAddProducts{
    public List<Case> Cases {get; set;}
    
    public NewAddProducts(){
        Cases = new List<Case>();
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.Contacts__c = Apexpages.Currentpage().getParameters().get('id');
        }
      
        //accts.add(new Opportunities_Line_Item__c());
        Cases.add(CaseAdd);
        
    }
    public void addrow(){
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.Contacts__c = Apexpages.Currentpage().getParameters().get('id');
        }
         Cases.add(CaseAdd);
        //accts.add(new Opportunities_Line_Item__c());
    }
   
    public PageReference save(){
        insert Cases;
        PageReference home = new PageReference('/' +    ApexPages.currentPage().getParameters().get('id'));
        home.setRedirect(true);
        return home;
    }
    public PageReference Cancel() {
        PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        returnPage.setRedirect(true);
        return returnPage;
    }
    }

 

And I can not come available Standard Field.

Standard Object : Case

Standard Field Name : Contact

Type : Lookup

Error: Compile Error: Illegal assignment from String to SOBJECT:Contact at line 9 column 13

 

 

public class NewAddProducts{
    public List<Case> Cases {get; set;}
    
    public NewAddProducts(){
        Cases = new List<Case>();
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.Contact = Apexpages.Currentpage().getParameters().get('id');
        }
      
        //accts.add(new Opportunities_Line_Item__c());
        Cases.add(CaseAdd);
        
    }
    public void addrow(){
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.Contact = Apexpages.Currentpage().getParameters().get('id');
        }
         Cases.add(CaseAdd);
        //accts.add(new Opportunities_Line_Item__c());
    }
   
    public PageReference save(){
        insert Cases;
        PageReference home = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        home.setRedirect(true);
        return home;
    }
    public PageReference Cancel() {
        PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        returnPage.setRedirect(true);
        return returnPage;
    }
    }

 

 

I hope you will help me.

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If you look at your add row link, you are rerendering the table:

 

 

<apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>

 However, the deleteRow button only rerenders the error:

 

 

 

<apex:commandButton value="Del Row" action="{!deleteRow}" rerender="error"/>            

 So even though you have changed the cases list, you haven't redrawn it on the page.  Add the table into the rerender attribute and that should solve the problem.

 

All Answers

bob_buzzardbob_buzzard

Case.Contact is a reference to a contact, whereas you are assigning an id to it.

 

Try:

 

 

CaseAdd.ContactId = (Id) Apexpages.Currentpage().getParameters().get('id');

 

 

KyoKyo

Hey Bob,

Ok Standard Field Successful.

Thank you so much.

 

but Delete Record I have a problem with it so much.

bob_buzzardbob_buzzard

Are you looking to delete out an entry from your Cases list?

KyoKyo

 

<apex:page controller="NewAddProducts" >
    <apex:form >
        <apex:pageBlock >
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
                <apex:commandButton value="Cancel" action="{!Cancel}" rerender="error"/>
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!Cases}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                <apex:column headerValue="Contact Name">
                    <apex:inputField value="{!a.ContactID}"  required="false"/>
                </apex:column>            
                <apex:column headerValue="Case Category">
                    <apex:inputField value="{!a.Case_Category__c}"/>
                </apex:column>                       
                <apex:column headerValue="Case Reson">
                    <apex:inputField value="{!a.Reason}"/>
                </apex:column>   
                <apex:column headerValue="Description">
                    <apex:inputField value="{!a.Description__c}" required="false"/>
                </apex:column> 
                <apex:column headerValue="Po Number">
                    <apex:inputField value="{!a.Po_Number__c}" required="false"/>
                </apex:column> 
                <apex:column headerValue="Detail">
                    <apex:inputField value="{!a.Detail__c}"/>
                </apex:column>    
                <apex:column headerValue="Status">
                    <apex:inputField value="{!a.Status}"  required="false"/>
                </apex:column>                           
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Page 

I want to remove my Add New Record.

 

bob_buzzardbob_buzzard

Is it only the last record that you want to delete, or could it be any record in the list?

 

I'm assuming its the latter - you'll need to ensure that there is a field on the Case that uniquely identifies it.  Then you can pass back this field from the page when the user clicks the delete button.

 

If you don't have a unique field, you can look at changing your list of cases to be a list of wrapper classes that contains the case and a unique index.  Then the page can pass through the unique index when the delete button is clicked.

 

In either of the above, your controller code will then iterate the list, locate the element with the unique id, and remove it from the list.

KyoKyo

Yes, it only the last record.

I want to delete the last record, but the function must be done before the save.

Thank you so much.

bob_buzzardbob_buzzard

If its always the last record, just add a Delete button to the page that calls an action method to remove the final entry.  Something like.

 

 

public PageReference deleteRow()
{
   if (cases.size()>1)
   {
      cases.remove(cases.size()-1);
   }

   return null;
}

 

 

 

KyoKyo

Hey, I'm sorry to annoy you again.

I have done as you recommend, but did not missing Record.

 

 

public class NewAddProducts{
    public List<Case> Cases {get; set;}
    
    public NewAddProducts(){
        Cases = new List<Case>();
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.ContactID = (id)Apexpages.Currentpage().getParameters().get('id');
        }
      
        //accts.add(new Opportunities_Line_Item__c());
        Cases.add(CaseAdd);
        
    }
    public void addrow(){
        Case CaseAdd = new Case();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            CaseAdd.ContactID = (id)Apexpages.Currentpage().getParameters().get('id');
        }
         Cases.add(CaseAdd);
        //accts.add(new Opportunities_Line_Item__c());
    }
   public PageReference deleteRow() {
   if (cases.size()>1)
   {
      cases.remove(Cases.size()-1);
   }
 return null;
 }
    public PageReference save(){
        insert Cases;
        PageReference home = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        home.setRedirect(true);
        return home;
    }
    public PageReference Cancel() {
        PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        returnPage.setRedirect(true);
        return returnPage;
    }
    }

 

 

            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
                <apex:commandButton value="Cancel" action="{!Cancel}" rerender="error"/>
                <apex:commandButton value="Del Row" action="{!deleteRow}" rerender="error"/>            
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!Cases}" var="a" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>
                </apex:facet>  

 

 

 

 

 

Thank you so much.

bob_buzzardbob_buzzard

If you look at your add row link, you are rerendering the table:

 

 

<apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/>

 However, the deleteRow button only rerenders the error:

 

 

 

<apex:commandButton value="Del Row" action="{!deleteRow}" rerender="error"/>            

 So even though you have changed the cases list, you haven't redrawn it on the page.  Add the table into the rerender attribute and that should solve the problem.

 

This was selected as the best answer
KyoKyo

Perfect! Thank you Bob.