• Pooja Garg
  • NEWBIE
  • 5 Points
  • Member since 2013

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

Hi 

 

   I have implemented Inserting data to an SObject dynamically..... now I need to make it UPSERT functionality with given External ID field but it is throwing error as Upsert with a field specification requires a concrete SObject

 


Here is my Code : 

@RestResource(urlMapping='/JSONTest/*')
global with sharing class JSONtest {
@HttpPost
	global static String upsertRowDATA() {
		RestRequest req = RestContext.request;
		String datasetId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
		String JSONString = req.requestBody.toString();
		//Schema.SObjectType custObject = Schema.getGlobalDescribe().get(datasetId+'__c');
		
		List<Object> listJsonObjects = (List<Object>)JSON.deserializeUntyped(JSONString);
		
		Schema.SObjectType soTypeObject = Schema.getGlobalDescribe().get(datasetId + '__c');
		Schema.DescribeSObjectResult objDescription = soTypeObject.getDescribe();
  		Map<String, schema.Sobjectfield> fieldsMap = objDescription.Fields.getMap();
  		system.debug(fieldsMap);
  		for(Object jsonObj : listJsonObjects) {
  			Map<String, Object> mapObj = (Map<String, Object>)jsonObj;
  			
  			system.debug(mapObj);
  			SObject sObj = Schema.getGlobalDescribe().get(datasetId + '__c').newSObject();
			
			for(String fieldName : fieldsMap.keySet()) {
			
				if(fieldName.indexOf('__c') != -1) {
					Object fieldData = mapObj.get(fieldsMap.get(fieldName).getDescribe().getName());
		   			sObj.put(fieldName, fieldData);
				}
	    	}
	    	upsert sObj ;
  		}
		 
		return JSON.serialize('{success : true}');
    }
}

 here "datasetId + '__c' " is my string type to get SObject reference.....!!!

 

now here in code if I write upsert sObj externalId__c;  then it gives me error that field is not a concreate SObject type....!!!

 

 

Could anyone suggest me the solution as per my requirement INSERT is working perfectly but getting problem for Updating...................... !!!

 

 

Thanks in Advance...!!

Pooja :)

 

How to create a new Custom Object using REST API services????

 

 

I am using this url : https://ap1.salesforce.com/services/data/v28.0/sobjects/CustomName

 

n data input as  

 

{

    "Name" : " CustomName"

}

 

then it gives me error as : 

     [
       {
                "message": "The requested resource does not exist",
                "errorCode": "NOT_FOUND"
       }
    ]

 

I am able to do all kind of stuffs like creating new record, updating deleting records in an existing CUSTOM OBJECT..........creating Triggers, Classes for it but not able to create a NEW Custom Object............!!!!

 

 

Please Help....!!

 

Thanks in advance....!!!

Pooja 

Hi, 

   I have a requirement of creating dynamic triggers for Custom Objects which are also being created dynamicaly through MetaData API . 

 

I search and came to know that Tooling API can help me creating dynamic triggers for REST API but  I am not getting how to configure this to my project and use it.....................

 

 

Can I get some example of creating dynamic Triggers through Tooling API..........................

 

Well I tried 

 

String json = '{"Name" : "COTrigger", \'"TableEnumOrId" : "Inventory__c"\',\'"Body" : "trigger COTrigger on Inventory__c (after insert) { }"\'}'; 

LoginResult loginResult = binding.login('pooja.avantsoft59@gmail.com', 'Test1234@');

        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://ap1.salesforce.com/services/data/v28.0/tooling/sobjects/ApexTrigger');
req.setMethod('POST');        
req.setHeader('Content-Type','application/json');
       
        req.setHeader('Authorization','OAuth 00D90000000nlxu!AQIAQCuO8z08yJ1hMR_4.OEL_RYoBwKaWMK4ttOpLevsJBRqFIptV2lkenMZIXObFS3n3EtJmNBjjyWGF8ZM7xp_DX9IEHNw');
         
req.setBody(json);

        Http httpReq = new Http();
        HttpResponse res = httpReq.send(req); 
        System.debug('hii'+res.getBody());

 which always give me error as "16:24:52:435 USER_DEBUG [16]|DEBUG|hii[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]"

 

Although I checked and copy pasted Session ID correctly still it gives me this error again n again.... 

 

Any kind of help would be appreciated.

 

Thanks in advance...!!!

Pooja

How to create a new Custom Object using REST API services????

 

 

I am using this url : https://ap1.salesforce.com/services/data/v28.0/sobjects/CustomName

 

n data input as  

 

{

    "Name" : " CustomName"

}

 

then it gives me error as : 

     [
       {
                "message": "The requested resource does not exist",
                "errorCode": "NOT_FOUND"
       }
    ]

 

I am able to do all kind of stuffs like creating new record, updating deleting records in an existing CUSTOM OBJECT..........creating Triggers, Classes for it but not able to create a NEW Custom Object............!!!!

 

 

Please Help....!!

 

Thanks in advance....!!!

Pooja