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 ?

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

1 Answer 1

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);
3
  • 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
    Commented Apr 6, 2019 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
    Commented Apr 6, 2019 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
    Commented Apr 8, 2019 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .