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 

I need to make batch rest apex callout through named credential approach

I got error as 'variable doesnot exist' for my fields .(Highlighted fields for which i am getting error)
This is my code:
global class RecruitmentCalloutBatch implements Database.Batchable<sObject>,Database.AllowsCallouts {
     
     global Database.QueryLocator start(Database.BatchableContext BC) {
         String query = 'Select Id,Name,OwnerId,ParentId,Zone__c,ExternalAccId__c,Phone,Fax,Website,Type,Industry,Description,NumberOfEmployees,AnnualRevenue from Account';
         return Database.getQueryLocator(query);
 }

     global void execute(Database.BatchableContext BC, List<Account> Accrecords) { 
           
         HttpRequest req = new HttpRequest();
        String accquery='select+Id,Name,OwnerId,ParentId,Zone__c,ExternalAccId__c,Phone,Fax,Website,Type,Industry,Description,NumberOfEmployees,AnnualRevenue+from+Account';
        req.setEndpoint('callout:Recruitmen/services/data/v49.0/query/?q='+accquery);
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse resp = http.send(req);
        //This response will have all the data records in json structure
        system.debug('Body:'+resp.getBody());
        AccountDataWrapper resWrapper = (AccountDataWrapper)JSON.deserialize(resp.getBody(),AccountDataWrapper.class);
        
        List<Account> newAccounts = new List<Account>();
        for(AccountDataWrapper.Records eachRec :resWrapper.records){
            Account acc = new Account();
            acc.ExternalAccId__c=eachRec.Id;
            acc.Name = eachRec.Name;
            acc.OwnerId=eachRec.OwnerId;
            acc.ParentId = eachRec.ParentId;
            acc.Zone__c =eachRec.Zone__c;
            acc.Phone =eachRec.Phone;
            acc.Fax =eachRec.Fax ;
            acc.Website =eachRec.Website;
            acc.Type =eachRec.Type;
            acc.Industry =eachRec.Industry;
            acc.Description =eachRec.Description;
            acc.NumberOfEmployees =eachRec.NumberOfEmployees;
            acc.AnnualRevenue =eachRec.AnnualRevenue;
           
//acc.BillingAddress =eachRec.BillingAddress;
            //acc.ShippingAddress =eachRec.ShippingAddress;
            //acc.CreatedById =eachRec.CreatedById;
            //acc.LastModifiedById  =eachRec.LastModifiedById;
            newAccounts.add(acc);
        }
        
        upsert  newAccounts;
      

}
     global void finish(Database.BatchableContext BC){    
    }
}
Thanks in advance
Best Answer chosen by suji srinivasan
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Suji,

Do we have the same Parentids and Ownerids across two orgs. 


I hope mapping of these two fields is causing the issue. Can you try mapping to the existing owner in new org.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Suji,

Can you share the apex class AccountDataWrapper . Because your Batch is using this class. Can you also let me know what functionallity are you trying to achive by using this.

Thanks
suji srinivasansuji srinivasan
public class AccountDataWrapper {

    public Integer totalSize;
    public Boolean done;
    public List<Records> records;

    public class Attributes {
        public String type;
        public String url;
    }

    public class Records {
        public Attributes attributes;
        public String Id;
        public String Name;
        public String OwnerId;
        public String ParentId;
        public String Phone;
        public String Fax ;
        public String Website;
        public String Type;
        public String Industry;
        public String Description;     
        public Integer NumberOfEmployees;
        public Integer AnnualRevenue;
       public String Zone__c;
    }

    
    public static AccountDataWrapper parse(String json) {
        return (AccountDataWrapper) System.JSON.deserialize(json, AccountDataWrapper.class);
    }
}

I am trying  to fetch Account fields data from one org to another org . I am unable to fetch Custom field zone__c 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

Did you check if zone__c field is available in your org or in other org. When I tried commenting that field my apex classes both batch and wrapper got saved succesfully.

Let me know if you face any issues.

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

Thanks,
mukesh guptamukesh gupta
Hi Suji,

Just want to make sure what result you are getting in your code, please share debug log of below
 
system.debug('Body:'+resp.getBody());

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

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

Thanks
Mukesh
suji srinivasansuji srinivasan
Hi  after commenting custom field .i got error as System.JSONException: For input string: "400000.0" at [line:1, column:338]
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I hope AnnualRevenue is currency field which should be Decimal in Apex. Can you change   the below line to Decimal     in wrappper class.
 
from

public Integer AnnualRevenue;

to 

public Decimal AnnualRevenue;




Let me know if you face any issues.

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

Thanks,
 
suji srinivasansuji srinivasan
Hi, Mukesh
i got response  the following response as DEBUG|Body:{"totalSize":20,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v49.0/sobjects/Account/0010w00000eJqaSAAS"},"Id":"0010w00000eJqaSAAS","Name":"xyz","OwnerId":"0056F00000ACLGdQAP","ParentId":null,"Phone":null,"Fax":null,"Website":null,"Type":null,"Industry":null,"Description":null,"NumberOfEmployees":null,"AnnualRevenue":400000.0},{"attributes":{"type":"Account","url":"/services/data/v49.0/sobjects/Account/0010w00000slSt5AAE"},"Id":"0010w00000slSt5AAE",...................................

I got this error aswell
System.JSONException: For input string: "400000.0" at [line:1, column:338]
 
suji srinivasansuji srinivasan
Hi sai,
As you suggested  i changed annual revenue to decimal in wrapper class.
i got the following error
DUPLICATE_DETECTION_MATCH_INVOCATION_SUMMARY|EntityType:Account|NumRecordsToBeSaved:18|NumRecordsToBeSavedWithDuplicates:0|NumDuplicateRecordsFound:0
23:55:15.0 (987135568)|DUPLICATE_DETECTION_END
23:55:15.0 (987147259)|CODE_UNIT_FINISHED|DuplicateDetector
23:55:15.0 (986882281)|DUPLICATE_DETECTION_MATCH_INVOCATION_SUMMARY|EntityType:Account|NumRecordsToBeSaved:18|NumRecordsToBeSavedWithDuplicates:0|NumDuplicateRecordsFound:0
FATAL_ERROR|System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Thanks in advance
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Suji,

Do we have the same Parentids and Ownerids across two orgs. 


I hope mapping of these two fields is causing the issue. Can you try mapping to the existing owner in new org.

Thanks,
 
This was selected as the best answer
suji srinivasansuji srinivasan
Thank you sai , It was helpful.