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
suji srinivasansuji srinivasan 

Hi, how to escape single quotes in soql.

I am getting error for my batch apex in soql as'illegal assignment of list to string'
how to resolve this?
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator  start(Database.BatchableContext bc) {
    string query=[select Id,Rating,(select Id,StageName from Opportunities where StageName='Inprogress,Delivered,closedwon') From account where Rating!='client'];
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<Account> Scope){
      
        for(Account a :Scope){
            a.Rating='client';        
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope; }
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
     
    }   
     }
    thanks in advance
Best Answer chosen by suji srinivasan
mukesh guptamukesh gupta
Hi Suji,

Please follow below code:-
 
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator  start(Database.BatchableContext bc) {
    string query='select Id,Rating,(select Id,StageName from Opportunities where StageName=\''+String.escapeSingleQuotes('Inprogress,Delivered,closedwon')+ '\') From account where Rating <> \''+String.escapeSingleQuotes('client') + '\'';
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<Account> Scope){
      
        for(Account a :Scope){
            a.Rating='client';        
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope; }
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
     
    }   
     }


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Srinivasan,

Can you try to replace the query as below.
 
string query='select Id,Rating,(select Id,StageName from Opportunities where StageName=\'Inprogress,Delivered,closedwon\') From account where Rating!=\'client\'';

If this solution helps, Please mark it as best answer.

Thanks,
 
mukesh guptamukesh gupta
Hi Suji,

Please follow below code:-
 
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator  start(Database.BatchableContext bc) {
    string query='select Id,Rating,(select Id,StageName from Opportunities where StageName=\''+String.escapeSingleQuotes('Inprogress,Delivered,closedwon')+ '\') From account where Rating <> \''+String.escapeSingleQuotes('client') + '\'';
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<Account> Scope){
      
        for(Account a :Scope){
            a.Rating='client';        
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope; }
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
     
    }   
     }


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
This was selected as the best answer
suji srinivasansuji srinivasan
how to achieve test coverage for this batch class. @isTest public class UpdateAccountRatingTest { @testSetup static void setup() { List accList = new List(); Account ac =new Account(); ac.Name = 'test'; ac.rating='prospect'; insert ac; ac.Name = 'test'; ac.rating='client'; update ac; } @isTest static void test() { Test.startTest(); UpdateAccountRating uar = new UpdateAccountRating(); Id batchId = Database.executeBatch(uar); Test.stopTest(); } } Thanks in advance
mukesh guptamukesh gupta
Hi Suji,

Kindly mark my solution as the best answer if it helps you.

And create a new question for your new requirment

Thanks
Mukesh