• Pramod Sharma
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi,

I am getting following error when I run my test class:

System.QueryException: Insufficient access rights: you cannot access draft articles.

The line for which it gives error is:

String q = 'select ' +pkb_Controller.join(pkb_Controller.kavFields, ',')+ ' from KnowledgeArticleVersion where Id = \'' +kavObj.get('Id')+  '\' and PublishStatus = :publishStatus';
kavObj = (KnowledgeArticleVersion)Database.query(q);

here I am trying to query the 'KnowledgeArticleVersion' object and I am getting the error.
How to give access for this object ? I am system administrator so should have the access by default.
How do I make my code be Schedulable with the informations that the User inform into Visualforce PAGE?

THE CODE and THE PAGE:

global class CSVTest implements System.Schedulable {
    public String mailSouhaite {get; set;}
    public string inputText {get; set;}
   
    global void execute(SchedulableContext sc) {
        planifier();
    }
 
public void planifier(){
    String query=inputText;
    String premier=query.substringAfter('select ');
    premier=  premier.substringBefore('from');
   
    string titre= premier+'\n';
    string contenuCSV = titre;

    string queryResultatString = '';
    list<sObject> queryResultat = (List<sObject>)database.query(inputText);
    for(sObject a: queryResultat)
    {

        queryResultatString = queryResultatString + string.valueof(a);
    }

    list<string> queryLignes = queryResultatString.split('}');

    for(string s:queryLignes){
        list<string> queryColonnes = s.split(',');
        for(string st:queryColonnes){
            contenuCSV = contenuCSV + st.substringAfter('=') + ',';
        }
        contenuCSV = contenuCSV.substringBeforeLast(',').substringBeforeLast(',') + '\n';
    }
    String lignes = contenuCSV;
    List<String> parts = lignes.split('\n');
    integer lineNumber = queryResultat.size();
    integer nbLignesPJ = 50;
    integer compterParties=0;

    for(integer i=0;i<lineNumber;i++){

      string fichierParties = parts[0] + '\n';

      if(math.mod(i,nbLignesPJ)<>0) continue;
      if((lineNumber-i)<nbLignesPJ){
     
        for(integer j=1;j<=(lineNumber-i);j++){
            fichierParties = fichierParties + parts[i+j] + '\n';
        }
      }
      if((lineNumber-i)>=nbLignesPJ){
         for(integer j=1;j<=nbLignesPJ;j++){
            fichierParties = fichierParties + parts[i+j] + '\n';
        }
      }
      //Send Mail
      Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
      blob csvBlob = Blob.valueOf(fichierParties);
      string csvNom = 'cases_fermes_'+Date.today().format()+'.csv';
      csvPJ.setFileName(csvNom);
      csvPJ.setBody(csvBlob);
      Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
      String[] adressMail = new list<string> {mailSouhaite};
      compterParties++;
      String subject;
      if (compterParties >=2)
          subject = 'CSV - '+Date.today().format()+' - Partie '+compterParties;
      else
          subject = 'CSV - '+Date.today().format();
      email.setSubject(subject);
      email.setToAddresses(adressMail);
      email.setPlainTextBody('Mail....');
      email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
      Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}
}

-------------------------------------------------------------------------------------------------------------------------

PAGE

<apex:page controller="CSVTest">
  <apex:form >
  <apex:PageBlock >
    :::::::::: TEST TEST TEST :::::::::: <br /><br />
    Mail..........:&nbsp;<apex:inputText styleClass="classeMail" value="{!mailSouhaite}"/><br /><br />
    Request  :&nbsp;&nbsp;<apex:inputText styleClass="classeRequete" value="{!inputText}"/><br /><br />
    <apex:inputCheckbox id="Monday "/>      
    <apex:outputLabel value="Monday " for="Monday "/><br/>
    <apex:inputCheckbox id="Tuesday "/>      
    <apex:outputLabel value="Tuesday " for="Tuesday "/><br/>
    <apex:inputCheckbox id="Wednesday "/>      
    <apex:outputLabel value="Wednesday " for="Wednesday "/><br/>
    <apex:inputCheckbox id="Thursday "/>      
    <apex:outputLabel value="Thursday " for="Thursday "/><br/>
    <apex:inputCheckbox id="Friday "/>      
    <apex:outputLabel value="Friday " for="Friday "/><br/>
    <apex:inputCheckbox id="Saturday "/>      
    <apex:outputLabel value="Saturday " for="Saturday "/><br/>
    <apex:inputCheckbox id="Sunday"/>      
    <apex:outputLabel value="Sunday" for="Sunday"/><br/><br/>
	
    When:
    <apex:selectList size="1">
         <apex:selectOption itemValue="00:00" itemLabel="00:00"/>
         <apex:selectOption itemValue="01:00" itemLabel="01:00"/>
         <apex:selectOption itemValue="02:00" itemLabel="02:00"/>
         <apex:selectOption itemValue="03:00" itemLabel="03:00"/>
         <apex:selectOption itemValue="04:00" itemLabel="04:00"/>
         <apex:selectOption itemValue="05:00" itemLabel="05:00"/>
         <apex:selectOption itemValue="06:00" itemLabel="06:00"/>
         <apex:selectOption itemValue="07:00" itemLabel="07:00"/>
         <apex:selectOption itemValue="08:00" itemLabel="08:00"/>
         <apex:selectOption itemValue="09:00" itemLabel="09:00"/>
         <apex:selectOption itemValue="10:00" itemLabel="10:00"/>
         <apex:selectOption itemValue="11:00" itemLabel="11:00"/>
         <apex:selectOption itemValue="12:00" itemLabel="12:00"/>
      </apex:selectList>
      <br/><br/>
    
    
    <apex:commandButton value="SEND" action="{!planifier}"/> <br/><br/>
   
  </apex:PageBlock>
  </apex:form>
</apex:page>


Help me please, THANK YOU!