0

Image of my object in salesforce enter image description here

I want to update the information contained in my "var" variable in my "Principale__c" object, using this function

public void DMLOperation () {

List <Principale__c> letter = getValo ();
List <Principale__c> updateList = new List <Principale__c> ();

for (Principale__c opps: letter) {
    updateList.add (opps);
    system.debug (opps);
}
 update (sObject []) updateList;
}

But when I do a debug of my valriable "letter" I do not see any value. What I would like to do is to be able to update the data that is in my variable

("(Principale__c: {Id = a0A1i000001VoH8EAK, CodeAlliance__c = wzgr6301}, Principle__c: {Id = a0A1i000001VoHIEA0, CodeAlliance__c = hqdl8921}, Principle__c: {Id = a0A1i000001VoHSEA0, CodeAlliance__c = lqlp8695} Principale__c: {Id = a0A1i000001VoHXEA0, CodeAlliance__c = cfjk9062} Principale__c: {Id = a0A1i000001VoHhEAK, CodeAlliance__c = bjqb3665} Principale__c: {Id = a0A1i000001VoHmEAK, CodeAlliance__c = mtds9152} Principale__c: {Id = a0A1i000001VoI1EAK , CodeAlliance__c = proy0003} ")"

from my "Principale__c" object because the "CodeAlliance" field, the data is incomplete. Here is the code that returns the data I want to update in my "Principale__c" object

public List<Principale__c> getValo(){

List<Principale__c> searchList = new   List<Principale__c>();
List<Principale__c> var = new List<Principale__c>();
for (integer l = 0; l < data.size(); l++) {
    searchList=[SELECT id From Principale__c where Email__c=:data[l][0]];
    system.debug(searchList);
    for (Principale__c principalProd :searchList) {
        principalProd.CodeAlliance__c = data[l][1];
        var.add(principalProd);
    }               
 }
    system.debug(var);
    return var;
}

I show you all my code

public class ImportExcel {  
public String fileName{get; set;}
public Blob contentFile{get;set;}
public String[] csvFileLines{get;set;}
public List<List<String>> data; 
public List<String> mails;
List<Principale__c> searchList;
List<Principale__c> var;
public ImportExcel(){
    csvFileLines = new String[]{};
    data= new List<List<String>>();
    searchList= new List<Principale__c>();
    var=new List<Principale__c>();
} 
public void readFile(){
    try
    {
        fileName=contentFile.toString();       
        csvFileLines = fileName.split('\n'); 
    }catch(Exception e){
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
        ApexPages.addMessage(myMsg); 
        return ; 
    }      
} 
//retourne la liste des données parcourues
public List<List<String>> getVal(){
    String[] data2; 
    for(integer i=0;i<csvFileLines.size();i++){
        data2=csvFileLines[i].split(',');
        //instancie la liste
        List<String> tampon = new List<String>();
        for(integer j=0; j<data2.size();j++){
          tampon.add(data2[j]);
        }
        data.add(tampon); 
    }
    return data;  
}   
//recupère le tableau des E-mails
public List<String> getMail(){
    List<List<String>> var = data;
    List<String> tampon = new List<String>();
    for(integer k=0;k<var.size();k++){
        tampon.add(var[k][0]);
    }
    //system.debug(tampon);
    mails = tampon;
    return tampon;
}


public  List<Principale__c> getValo(){
    system.debug(data.size());
for(integer l=0;l<data.size();l++){
        searchList=[SELECT id From Principale__c where Email__c=:data[l][0]];
        for(Principale__c principalProd :searchList){
            principalProd.CodeAlliance__c=data[l][1];
            var.add(principalProd);
        }   
}
    system.debug(var);
return var; 
}

public void DMLOperation() {  
List<Principale__c> lettre= getValo();
List<Principale__c> updateList = new List<Principale__c>();
system.debug(lettre);
    for(Principale__c opps: lettre){
        updateList.add(opps);
    }
update (sObject[]) updateList;
}
}

Visualforce page

<apex:page controller="ImportExcel">
    <apex:pageBlock >
        <apex:form >
            <apex:pageBlockSection columns="4">
               <apex:inputFile value="{!contentFile}"  filename="{!fileName}"/>
               <apex:commandButton value="Import Excel" action="{!readFile}"/>
            </apex:pageBlockSection>    
            <apex:pageBlock >
               <apex:pageBlockTable value="{!val}" var="acct">
                   <apex:column value="{!acct}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock>   
            <apex:pageBlock >
               <apex:pageBlockTable value="{!mail}" var="liste">
                   <apex:column value="{!liste}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock> 
               <apex:pageBlock >
               <apex:pageBlockTable value="{!valo}" var="li">
                   <apex:column value="{!li}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock> 
        </apex:form>
    </apex:pageBlock>
</apex:page>
0

As per this documentation Considerations for Creating Custom Controllers and Controller Extensions

You can't use data manipulation language (DML) operations in a “getxxx” method in a controller. For example, if your controller had a getName method, you could not use insert or update in the method to create an object

As you are trying to update the records in the getxxx method in a controller, that records won't be updated.

You will have to perform the update operation on click of Import Excel button. So, this is how your controller and visualforce page should look like to make this to work.

Updated controller

public class ImportExcel {  
    public String fileName{get; set;}
    public Blob contentFile{get;set;}
    public String[] csvFileLines{get;set;}
    public List<List<String>> data; 
    public List<String> mails;
    List<Principale__c> searchList;
    List<Principale__c> var;
    public List<List<String>> Val {get;set;}
    public List<Contact> Valo {get;set;}
    public List<List<String>> Val1 {get;set;}
    public List<Contact> Valo1 {get;set;}

    public ImportExcel(){
        csvFileLines = new String[]{};
        data= new List<List<String>>();
        searchList= new List<Principale__c>();
        var=new List<Principale__c>();
    } 

    public void readFile(){
        try
        {
            fileName=contentFile.toString();       
            csvFileLines = fileName.split('\n'); 
            Val1 = getValMethod();
            Valo1 = getValoMethod();
            List<Principale__c>  principaleToUpdate = Valo1;
            update principaleToUpdate;
        }catch(Exception e){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
            ApexPages.addMessage(myMsg); 
            return ; 
        }      
    } 

    //retourne la liste des données parcourues
    public List<List<String>> getValMethod(){
        String[] data2; 
        for(integer i=0;i<csvFileLines.size();i++){
            data2=csvFileLines[i].split(',');
            //instancie la liste
            List<String> tampon = new List<String>();
            for(integer j=0; j<data2.size();j++){
              tampon.add(data2[j]);
            }
            data.add(tampon); 
        }
        return data;  
    }   

    //recupère le tableau des E-mails
    public List<String> getMail(){
        List<List<String>> var = data;
        List<String> tampon = new List<String>();
        for(integer k=0;k<var.size();k++){
            tampon.add(var[k][0]);
        }
        //system.debug(tampon);
        mails = tampon;
        return tampon;
    }

    public  List<Principale__c> getValoMethod(){
        system.debug(data.size());
        for(integer l=0;l<data.size();l++){
            searchList=[SELECT id From Principale__c where Email__c=:data[l][0]];
            for(Principale__c principalProd :searchList){
                principalProd.CodeAlliance__c=data[l][1];
                var.add(principalProd);
            }   
        }
        system.debug(var);
        return var; 
    }

    /*public void DMLOperation() {  
        List<Principale__c> lettre= getValo();
        List<Principale__c> updateList = new List<Principale__c>();
        system.debug(lettre);
            for(Principale__c opps: lettre){
                updateList.add(opps);
            }
        update (sObject[]) updateList;
    }*/
}

Updated visualforce page

<apex:page controller="ImportExcel">
    <apex:pageBlock >
        <apex:form >
            <apex:pageBlockSection columns="4">
               <apex:inputFile value="{!contentFile}"  filename="{!fileName}"/>
               <apex:commandButton value="Import Excel" action="{!readFile}"/>
            </apex:pageBlockSection>    
            <apex:pageBlock >
               <apex:pageBlockTable value="{!val1}" var="acct">
                   <apex:column value="{!acct}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock>   
            <apex:pageBlock >
               <apex:pageBlockTable value="{!mail}" var="liste">
                   <apex:column value="{!liste}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock> 
               <apex:pageBlock >
               <apex:pageBlockTable value="{!valo1}" var="li">
                   <apex:column value="{!li}"/>
               </apex:pageBlockTable>  
            </apex:pageBlock> 
        </apex:form>
    </apex:pageBlock>
</apex:page>

Note : Save the controller first and then save the visualforce page. I updated few variables names, i.e. val and valo to val1 and valo1 respectively. Post saving the visualforce page, you can revert the variables names if needed from visualforce page and controller.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.