1

I get Item's list from 1 organisation and want to insert them like Another Items in 2 organisation. But it try to insert it with Id. How can I fix that? Error: System.DmlException: Insert failed. First exception on row 0 with id a01f400000NiRsVAAV; first error: INVALID_ID_FIELD, 'a01f400000NiRsV' is not a valid Salesforce ID for the type Another Item: []

HttpGet:

@RestResource(urlMapping='/endpoint/*')
global with sharing class Endpoint {
@HttpGet
global static List<Item__c> getItem(){
    RestRequest req = RestContext.request;

    List<Item__c> itemList;
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        itemList = [SELECT Firsname__c, Lastname__c, Phone__c, Price__c FROM Item__c LIMIT 100];
        res.responseBody = Blob.valueOf(JSON.serialize(itemList));
    } catch(Exception e){
        System.debug('Error:  ' + e.getMessage());
    }
    return itemList;
}
}

Callout:

public class Callout {    
public static String webTokenBody(){
    String requestBody = 'my_web_token_string';
    return request;
}

public class webTok{
    public String webToken{get;set;}    
}

public static HttpRequest createRequestForToken(String service){
    String requestBody = webTokenBody();
    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setBody(requestBody);
    ourRequest.setMethod(service);
    ourRequest.setEndpoint('https://my.salesforce.com/services/oauth2/token');
    return ourRequest;
}

public static HttpRequest createRequestForService(String token, String method){
    HttpRequest finalRequest = new HttpRequest();
    finalRequest.setHeader('Authorization','Bearer ' + token);
    finalRequest.setHeader('Content-Type','application/json');
    finalRequest.setHeader('accept','application/json');
    finalRequest.setMethod(method);
    finalRequest.setEndpoint('https://my.salesforce.com/services/apexrest/endpoint/');
    return finalRequest;
}

public static HttpResponse getCalloutResponseContents() {   
    Http ourHttp = new Http();
    HttpRequest requestForToken = createRequestForToken('GET');
    HttpResponse responseToken = ourHttp.send(requestForToken);
    webTok objAuthenticationInfo = (webTok)JSON.deserialize(responseToken.getbody(), webTok.class);

    List<Another_Item__c> itemList = new List<Another_Item__c>();

    if(objAuthenticationInfo.webToken != null){
        HttpRequest requestForService = createRequestForService(objAuthenticationInfo.webToken, 'GET');
        HttpResponse responseService = ourHttp.send(requestForService);
        itemList = (List<Another_Item__c>)JSON.deserialize(responseService.getBody(), List<Another_Item__c>.class);
        //System.DmlException: Insert failed. First exception on row 0 with id a01f400000NiRsVAAV; first error: INVALID_ID_FIELD, 'a01f400000NiRsV' is not a valid Salesforce ID for the type Another Item: []
        insert itemList;
        return responseService;
    }
}
}
  • 2
    To confirm - are you trying to insert a record with its Id field populated? – Jayant Das Nov 20 '18 at 14:08
2

You cannot do DML on ID which does not exists in your organization. You have to do clean the id as null.

 public static HttpResponse getCalloutResponseContents() {   
        Http ourHttp = new Http();
        HttpRequest requestForToken = createRequestForToken('GET');
        HttpResponse responseToken = ourHttp.send(requestForToken);
        webTok objAuthenticationInfo = (webTok)JSON.deserialize(responseToken.getbody(), webTok.class);

        List<Another_Item__c> itemList = new List<Another_Item__c>();

        if(objAuthenticationInfo.webToken != null){
            HttpRequest requestForService = createRequestForService(objAuthenticationInfo.webToken, 'GET');
            HttpResponse responseService = ourHttp.send(requestForService);

            itemList = (List<Another_Item__c>)JSON.deserialize(responseService.getBody(), List<Another_Item__c>.class);
            for(Another_Item__c al: itemList){
                al.Id =null;
            }
            insert itemList;
            return responseService;
        }
    }
    }

On other note, Cleaning out the ID before insert will solve the problem for now. But then if same rest endpoint is called again and again you would end up inserting same records again and again.

You have to use External ID upserts and alter your datamodel a bit for that

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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