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
umair ayazumair ayaz 

How to get id of a record when editing from table

Hi,
I have a table with 3 output columns and 1 colum with input field just as shown in the picture below.
User-added image
I am trying to fire a trigger after the order number is updated. The problem that i am facing is that i want the id of the updated record only where as all the trigger functions display the IDs of all the records in the account and trigger.size returns the total number of records which means that all the records are being updated so my question is how do i get the id of the changed record in the trigger.

Here is my Visualforce page :- 
<apex:page controller="OrderAccountController" >
    <apex:form >
    <apex:pageBlock title="List of accounts">
                <apex:pageBlockButtons location="top"> 
        <apex:commandButton value="Reorder" action="{!save}" />
            </apex:pageBlockButtons> 
        <apex:pageBlockTable value="{!account}" var="a">
            <apex:column value="{!a.Name}"/>
            <apex:column value="{!a.Industry}"/>
            <apex:column value="{!a.Type}"/>
            <apex:column headerValue="Order Number">
            <apex:inputField value="{!a.order__c}" />
              
            </apex:column>
        
        
        </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form> 
    
</apex:page>

 Here is my custom controller
public class OrderAccountController {
    public List <Account> account=new List <Account>();
    public Account act{get;set;}
    
     public List <Account> getAccount() {
        account = [SELECT Id, Name, Site, Type, accountNumber, CustomerPriority__c, order__c, industry FROM Account order by order__c ];
        return account;
    }
    public PageReference save() {
        try {
            update account;
        }  

        catch(Exception e){
            System.debug('Exception occurred '+String.valueOf(e));
        }
        return NULL;
    }       
    

}

Here is my trigger
 
trigger trigger1 on Account (after update) {
    public List <Account> account=new List <Account>();
    Map<Id,Account> mp = new Map<Id,Account>(); 
    public Integer currentRecordId {get;set;}
     account = [SELECT Id, Name  FROM Account WHERE Id IN :Trigger.New];
     for(Account con :trigger.new){  
        
  }     
     system.debug(mp);
    
    
    for (Account acc: Trigger.new) {
		
		currentRecordId= trigger.size;

		}
    System.debug(currentRecordId);
	}

Any help regarding this would be much appreciated. Please help a beginner. Thanks in advance
Amit Chaudhary 8Amit Chaudhary 8
Hi umair ayaz,

In your controller class you are updating all Account record.
public List <Account> getAccount() {
      // you are query all record from here
        account = [SELECT Id, Name, Site, Type, accountNumber, CustomerPriority__c, order__c, industry FROM Account order by order__c ];
        return account;
    }
    public PageReference save() {
        try {
            update account; // And updating all record here
        }  

        catch(Exception e){
            System.debug('Exception occurred '+String.valueOf(e));
        }
        return NULL;
    }

If you want to check which record order number is changes in trigger try to update code like below
trigger trigger1 on Account (after update) {
    public List <Account> account=new List <Account>();
    Map<Id,Account> mp = new Map<Id,Account>(); 
    
	public Integer currentRecordId {get;set;}

    for(Account acc :trigger.new)
	{  
		Account accOld = Trigger.oldMap.get(acc.id);
		if(acc.order__c != accOld.order__c)
		{
			System.debug('---------ACC id of updated record---------->'+acc.id);
		}
	}     
}