M.App Enterprise Discussions

Discuss topics with other M.App Enterprise Product pioneers and experts to get the most out of it.
Showing results for 
Search instead for 
Do you mean 
Reply
Highlighted
Staff
Posts: 103
Registered: ‎01-13-2017

Javascript API error

Hi Team,

 

I am using M.App Enterprise v2018 EP03.

 

In my desktop app, I have written a Javascript to count the selected feature in the map - SC.Map.getSelectedElements()

 

I tried to put alert - 

alert(SC.Map.getSelectedElements());

It shows a message saying - "[object Object]"

SelectedFeatureCountError.png

 

When I access the Workflow in the browser - https://in-g-smathur1/Workflows/Form?workflow=SOI_DATA_CAPTURE&lang=en-IN&sessionid=5e703396-64b4-43...

 

It shows following error in the console:

Uncaught TypeError: Cannot read property 'getSelectedElements' of undefined
    at Object.<anonymous> (CustomScript?workflow=SOI_DATA_CAPTURE&lang=en-IN&sessionid=5e703396-64b4-43d6-a617-2bf0bcf082ea&tenant=ShashankTenant&nodeid=DeletePolygonFeatureWF&id=2977&scriptFile=GetMapInfo.js:5)
    at Object.trigger (IGForm?v=OMgWO-AyZ6TLiFXlLe3ns0UAv9EZJav1tFtjj0Y8vFc1:1)
    at Array.<anonymous> (?v=hltF3jPHcTdJixEkwpj0A2wOb1flsPqzcp9bODWMWwY1:1)
    at l (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at Object.fireWith [as resolveWith] (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at Object.<anonymous> (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at l (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at Object.fireWith [as resolveWith] (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at Object.t.<computed> [as resolve] (jquery?v=KVlI-fyBB-Z8AxpOE7nGfeB4Lms7WAtGmiTFfQ0bG8s1:1)
    at ?v=hltF3jPHcTdJixEkwpj0A2wOb1flsPqzcp9bODWMWwY1:1

Regards,

Shashank Mathur

 

 

Highlighted
Staff
Posts: 1,068
Registered: ‎10-18-2015

Re: Javascript API error

Hi,

 

getSelectedElements returns an array of ids, please refer to this doc:

https://community.hexagongeospatial.com/t5/M-App-Enterprise-Tutorials/Javascript-API-in-Desktop-M-Ap...

 

Outside of the Java conext the SC.Map object is not defined, so you cannot refer to any of its methods.

 

Stefano

Stefano Turcato
Presale Engineer
Hexagon Geospatial
Highlighted
Staff
Posts: 103
Registered: ‎01-13-2017

Re: Javascript API error

Hi Stefano,

 

I have refered the same link  and created this javascript&colon;

IG.vent.on('form:ready', function(form){
	
	
	
	var selFeaturesID = SC.Map.getSelectedElements();
	
	alert(SC.Map.getSelectedElements());
	
	form.getItemById('SelectedFeatures').setValue(selFeaturesID);
	form.getItemById('FeaturesCount').setValue(selFeaturesID.length);
	
});

Same javascript is called in the Workflow to fill the form fields.

 

But still I am getting this error.

 

Regards,

Shashank Mathur

Highlighted
Staff
Posts: 1,068
Registered: ‎10-18-2015

Re: Javascript API error

please do not use alert, since it stops JS execution, better to use IG.notify. You can also use console.log and inspect JS console (firebug lite is added now by default to the integrated java browser). 

 

 

Stefano Turcato
Presale Engineer
Hexagon Geospatial
Highlighted
Staff
Posts: 103
Registered: ‎01-13-2017

Re: Javascript API error

Following functions are working fine:

SC.Map.setActiveLayer('polygon_features')
SC.Map.clearSelectedElements()
SC.Map.setMapScale(50000)

But when I use it to get the values, it doesn't work like:

SC.Map.getSelectedElements();
SC.Map.getMapScale()
SC.Map.getActiveLayer()

Regards,

Shashank Mathur

Highlighted
Staff
Posts: 1,068
Registered: ‎10-18-2015

Re: Javascript API error

All of the mentioned methods return a promise, so you have to wait for their execution. For instance in this form I'm showing the current map scale:

 

Annotation 2019-06-19 191020.png

 

and this is my JS definition for the aciton:

 

SC.Map.getMapScale().then(
	function(results) {
			IG.notify('Current scale: ' + results).show()
		})

I will update the tutorial to underline all of the methods provided by the API are asynchronous.

 

Thanks,

Stefano

Stefano Turcato
Presale Engineer
Hexagon Geospatial
Highlighted
Staff
Posts: 103
Registered: ‎01-13-2017

Re: Javascript API error

Thanks Stefano,

 

Its working fine. But when I use the same syntax for getSelectedElements(), it gives following message instead of returning ID values:

"Feature IDs: AttributeImpl [type=class java.lang.Integer, value=2977]".

 

Here is my code:

SC.Map.getSelectedElements().then(
	function(selFeaturesID) {		
			IG.notify('Feature IDs: ' + selFeaturesID).show();
			IG.getItemById('SelectedFeatures').setValue(selFeaturesID)
			IG.getItemById('FeaturesCount').setValue(selFeaturesID.length)
		});

 

SelectedFeatureIDError.png

 

Regards,

Shashank Mathur

Highlighted
Regular Contributor
Posts: 186
Registered: ‎11-17-2015

Re: Javascript API error

Hello,

I have the same issue. Any update on this subject?

Thank you.

Highlighted
Staff
Posts: 1,068
Registered: ‎10-18-2015

Re: Javascript API error

apparently there is an issue in the Java method that returns a Java array that cannot be resolved in JS. I would suggest to log a support ticket.

Stefano Turcato
Presale Engineer
Hexagon Geospatial
Highlighted
Regular Contributor
Posts: 186
Registered: ‎11-17-2015

Re: Javascript API error

I have logged a ticket.

Thank you.