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
azu.shaik1.3965080575576013E12azu.shaik1.3965080575576013E12 

not able to upsert using extenal id able to insert in apex rest api

Iam able to insert the data into object. But iam unable to update the same record using the external id . when i use upsert dml without external id (phpId) i can able to insert the data into custom object . when i use upsert with external id i am not able to insert the data into object . please help where i did worng

Product__c product= createProduct(params);
              
                if(product!=null){
                 body = upsertSObject(product);
                              
                if (body.id != null) {  
                   String name1 =null;
                   String value1 =null;  
                   String phpId=null;               
                   JSONParser parser = JSON.createParser(params.get('attributeValues',false));       
                  
                    while (parser.nextToken() != null) {
                          if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'name1')) {
                                 parser.nextToken();
                                 name1= parser.getText();
                                }
                          if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'value1')) {
                                parser.nextToken();
                                value1 = parser.getText();
                              }
                              if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'phpId')) {
                                parser.nextToken();
                                phpId= parser.getText();
                              }
                          if(name1 !=null && value1 !=null&&phpId !=null) {
                                Attribute__c atrb= createAttributeItem(name1,value1,phpId, body.id);
                                upsertSObject (atrb) ;
                                value1 = null;
                                phpId=null;
                               }
................


public static Attribute__c createAttributeItem(String name1,String value1,string phpId, Id id) {
   
        Attribute__c atb= new Attribute__c ();
        atb.Product_Name__c= id ;
        atb.Attribute_Value__c= value1;
        atb.Php_Id__c = phpId;
        atb.name = name1;
            
        return atb;
    }

..............

private static ResponseBody upsertSObject(SObject sObj) {
        System.debug(JSON.serializePretty(sObj));
        ResponseBody body = new ResponseBody();
        try {
            if (sObj instanceof Product__c)
          
              upsert (Product__c) sObj Php_External_Id__c ;
             else if(sObj instanceof Attribute__c )
           upsert (Attribute__c) sObj ;//here iam inserting attributes but if upsert (Attribute__c) sObj Php_Id__c iam not able to insert the data into object please       help    me  how to insert and update using external id here.
            body.id = sObj.id;
          }
        catch (Exception e) {
            body.addError(e);
          }
        return body;
    }


logontokartiklogontokartik
Can you try using Database.upsert() instead of upsert statement? Also before doing an upsert, please assign to the concrete SObject. Something like

f (sObj instanceof Product__c){
            Product__c uProduct = (Product__c)sObj;
            Database.upsert(uProduct,'Php_External_Id__c',true);
  // you can also capture upsertResult if you want to.
}


azu.shaik1.3965080575576013E12azu.shaik1.3965080575576013E12
Hi karthick ,

Thanks for the response . when try with above code iam getting Database.upsert. i am getting the incorrect method signatures .

Thanks,
Hareesh
logontokartiklogontokartik
Yeah, sorry for the 2nd argument, it must of schema.sobjectfield type. 

Database.upsert(uProduct,Product__c.fields.Php_External_Id__c,true);

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm#apex_System_Database_upsert