• Matias Ramirez 2
  • NEWBIE
  • 15 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello everyone.

I need to process a selected row in a table in a VFP, let's say I need to show what is selected.

I can do it with Checkboxes, but I need to do it with RadioButtons (only one row should be checked at time)
I wanted to know if it is possible to achive this with Apex.

This is what I've done:
VFP
<apex:page controller="showContactsCtrl">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockTable value="{!contactsWrapList}" var="contactVariable" >
                <apex:column onclick="{!contactVariable.selected}">
                    <apex:inputCheckbox value="{!contactVariable.selected}" id="CheckBox" onclick="SelectedOne(this)" > 
                    </apex:inputCheckbox>
                </apex:column>
                <apex:column value="{!contactVariable.c.name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!m1}" value="Select Contact" />
    </apex:form>
    
    <apex:outputPanel>
        <p>You have selected:</p>
        <apex:dataList value="{!selectedContact}" var="c">{!c}</apex:dataList>
    </apex:outputPanel>
</apex:page>

And this is the controller:
public class showContactsCtrl {
    public List<Contact> contacts;
    public List<contactsWrap> contactsWrapList {get; set;}
    public String selectedContact {get; set;}
    
    public showContactsCtrl(){
        contacts = [SELECT Id, Name FROM Contact WHERE Account.Id = someAccountId];
        contactsWrapList = new list<contactsWrap>();
        for(Contact c : contacts){
            contactsWrapList.add(new contactsWrap(false, c));
        }
    }
    
    public void m1(){
        for(contactsWrap contactsWrapObj : contactsWrapList) {
            if(contactsWrapObj.selected)
                selectedContact = contactsWrapObj.c.Name;
        }
    }
    
    
    public class contactsWrap{
        public Contact c {get; set;}
        public boolean selected{get; set;}
        
        public contactsWrap(Boolean s, Contact param){
            this.c = param;
            selected = s;
        }
    }
    
}

 
Hello everyone!
I have a problem.
I'd like to prevent removing data in the Account's phone field, by using Apex code. I know you can do this in the UI, but I need to do it this way.
I've already done this:

Trigger:
trigger AccountTrigger2 on Account (before update) {
    AccountTriggerHandler handler = new AccountTriggerHandler();
 
    if (Trigger.IsUpdate && Trigger.IsBefore) {
        handler.onBeforeUpdate(Trigger.New, Trigger.OldMap);
    }
}


Handler:
Public class AccountTriggerHandler {

    public void onBeforeUpdate(List<Account> accountList, Map<Id, Account> accountOldMap) {
        
        for(Account aItem : accountList) {
            Account oldAccountItem = accountOldMap.get(aItem.Id);
            if(aItem.phone == null && oldAccountItem.phone != null) {
                aItem.phone = oldAccountItem.phone;
            }
        }
    }

}
This works well when testing in the UI, but the problems come with the test:
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
        
        test.phone = NULL;
        update test;
        
        System.assertNotEquals(NULL, test.phone);
    }
    
}
I'd expect the phone to have 1234, but it has NULL, failing the test.
It says: System.AssertException: Assertion Failed: Same value: null
Any idea?

Best regards!
Hello everyone!
I have a problem.
I'd like to prevent removing data in the Account's phone field, by using Apex code. I know you can do this in the UI, but I need to do it this way.
I've already done this:

Trigger:
trigger AccountTrigger2 on Account (before update) {
    AccountTriggerHandler handler = new AccountTriggerHandler();
 
    if (Trigger.IsUpdate && Trigger.IsBefore) {
        handler.onBeforeUpdate(Trigger.New, Trigger.OldMap);
    }
}


Handler:
Public class AccountTriggerHandler {

    public void onBeforeUpdate(List<Account> accountList, Map<Id, Account> accountOldMap) {
        
        for(Account aItem : accountList) {
            Account oldAccountItem = accountOldMap.get(aItem.Id);
            if(aItem.phone == null && oldAccountItem.phone != null) {
                aItem.phone = oldAccountItem.phone;
            }
        }
    }

}
This works well when testing in the UI, but the problems come with the test:
@isTest
public class AccountTriggerHandlerTest {

    @isTest
    public static void onBeforeUpdateTest(){
        Account test = new Account(name='Test 1', phone='1234');
        insert test;
        
        test.phone = NULL;
        update test;
        
        System.assertNotEquals(NULL, test.phone);
    }
    
}
I'd expect the phone to have 1234, but it has NULL, failing the test.
It says: System.AssertException: Assertion Failed: Same value: null
Any idea?

Best regards!