0

I am trying to insert queues using metadata api but getting error while inserting sobject in the queue.

MetadataService.QueueSobject obj =new MetadataService.QueueSobject();
obj.sobjectType='Account';      
MetadataService.Queue queue = new MetadataService.Queue();
queue.fullName = 'Account_Queue';
queue.name = 'Account_Queue'; 
System.debug('*****Selected Object*****'+obj.sobjectType); // getting value here.
queue.queueSobject[0].sobjectType = obj.sobjectType;  // Getting attempt to de-refrence null object

MetadataService.MetadataPort service = createService(); 
List<MetadataService.SaveResult> results = service.createMetadata(new 
MetadataService.Metadata[] { queue });
handleSaveResults(results[0]);

queue.queueSobject[0].sobjectType = obj.sobjectType; -- This line throwing error.

obj.sobjectType is not getting stored in queue.queueSobject[0].sobjectType. How can I fix it ?

  • According to the Salesforce guid, the supported object for queue.queueSobject.sObjectType are Case, Lead, serviceContract and custom objects. Please check Queue Metadata Guide – apple123 Apr 5 at 15:10
  • Are you using the apex-mdapi library? – Jayant Das Apr 5 at 15:30
  • Yes, I am using metadata api library. – Ashutosh Apr 6 at 9:23
2

NPE in your code is most likely related to the fact that you are trying to reference queue.queueSobject collection which you have not initialised yet.

Try changing line

queue.queueSobject[0].sobjectType = obj.sobjectType; 

to something like this:

queue.queueSobject = new List<QueueSobject>();
final QueueSobject qsobj = new QueueSobject();
qsobj.sobjectType = 'Account'
queue.queueSobject.add(qsobj);
  • I tried this one too but throwing error "Illegal assignment from List<QueueSobject> to List<MetadataService.QueueSobject>" at first line and when I am trying to add it into list getting "Method does not exist or incorrect signature: void add(QueueSobject) from the type List<MetadataService.QueueSobject>". – Ashutosh Apr 6 at 9:22
  • Hello @Ashutosh , I did not expect this code to compile. What I tried to do is to answer your original question and point out where your NPE is coming from and how (in general) the code may look like. Not being an apex-mdapi library user myself I can not provide a fully working example. You may want to ask another question if my answer resolved your NPE confusion. – gaiser Apr 6 at 12:25
  • 1
    The point is you need to initialize the list. You can still one-line it. queue.queueSObject = new List<MetadataService.QueueSObject>{ obj } – Adrian Larson Apr 8 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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