All Collections
Working with Cloud Code
How to transfer existing User Entities to Custom Entities?
How to transfer existing User Entities to Custom Entities?

user entities, custom entities, app data, import

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

There are no direct operations set on the Portal page that allow a user to import the exported User Entities JSON file to Custom Entities. But, we can achieve it by writing a Cloud Code script (setting to S2S callable for calling from custom server). Here is the code script that will do this trick for you.

var response = {};
var createdTypes = 0;
var createdEntities = 0;

var profileId = bridge.getProfileId();
var email = bridge.getEmail();
bridge.logInfo("running from ConvertUserEntitiesToCustomEntities script profileid: ", profileId);


var session = bridge.getSessionForProfile(profileId);
bridge.logInfo("running from ConvertUserEntitiesToCustomEntities script session: ", session);


//use pagination to determin if need to get next page
var maxperpage = 100;
var pageNumber = 1;

var context = {
    "pagination": {
        "rowsPerPage": maxperpage,
        "pageNumber": pageNumber
    },
    "searchCriteria": {
       
    },
    "sortCriteria": {
        "createdAt": 1,
        "updatedAt": -1
    }
};

var entityProxy = bridge.getEntityServiceProxy(session);

var firstPageResult = entityProxy.getPage(context);

if (firstPageResult.status == 200) {
   
    var count = firstPageResult.data.results.count;
   
    response.count = count;

    if(count>0){
       
        var customEntityProxy = bridge.getCustomEntityServiceProxy();
       
        var items = firstPageResult.data.results.items;
       
        //get exist custome entitties types
        var collectionsResult = customEntityProxy.sysListCollections();
       
        //create an array to hold current types/collections names (type name is unique required in custom entities)
        var arrTypes = [];
        var existCustomEntityTypes;
       
        if (collectionsResult.status == 200) {
           
            existCustomEntityTypes = collectionsResult.data.collections;
           
            for (var x in existCustomEntityTypes){
               
                //add collections name to this array
                arrTypes.push(Object.keys(existCustomEntityTypes[x])[0]);
            }
           
        } else{
            response.getCustomEntityTypesErro = "error call sysListCollections";
        }
       
       
        bridge.logInfo("collection arrTypes before : ", arrTypes);
       
        response.existedCustomTypes = arrTypes;
       
        //modify the option if want to change to global
        var collectionOptsJson = {
            "isOwned": true,
            "migrate": false,
            "identifier": email
        };
       
        for (var i in items) {
            var entityType = items[i].entityType;
           
            response.entityType =  entityType;
           
            bridge.logInfo("check entity type : ", entityType);
           
            //check custom entities' types (arrTypes) whether contain this type, create one if not
            response.checkTypeIncludes = arrTypes.includes(entityType);
           
            if (!arrTypes.includes(entityType)){
                var createCollectionResult = customEntityProxy.sysCreateCollection(entityType, collectionOptsJson);
                if (createCollectionResult.status == 200) {
                    //add to the type check array
                    arrTypes.push(entityType);
                    createdTypes ++;
                }else{
                    response.createCollectionError = "sysCreateCollection call errors from first page, check entityType you injected";
                }
            }
           
            //after create collection type, we can create custom entity
           
            var dataJson = items[i].data;
            var acl = items[i].acl;
            var timeToLive = null;
            var ownerId = profileId;
            var customEntityProxy = bridge.getCustomEntityServiceProxy();
           
            var createEntityResult = customEntityProxy.sysCreateEntity(entityType, dataJson, acl, timeToLive, ownerId);
            if (createEntityResult.status == 200) {
                bridge.logInfo("first page totally created "+ i +" custom entity(ies) with type : ", entityType);
                createdEntities ++;
            }
        }
       
        bridge.logInfo("collection arrTypes after firstpage looped throuhg and created custom entities: ", arrTypes);
       
       
        //if return items count great than maxperpage will get into belows for loop to call next page, untill scan all        
        var remainder = 0;
        var numberpage = 0;
        remainder = count % maxperpage;
        numberpage = (count-remainder)/maxperpage;
       
        response.remainder = remainder;
        response.numberpage = numberpage;
       
        bridge.logInfo("remainder: ", remainder);
        bridge.logInfo("numberpage: ", numberpage);
       
        for (var n = 0; n < numberpage; n++ ){
           
            pageNumber ++;
           
            context.pagination.pageNumber = pageNumber;
           
            response.nextpageContext = context;
            bridge.logInfo("numberpage: ", numberpage);
           
            var nextPageResult = entityProxy.getPage(context);
           
            if (nextPageResult.status == 200) {
           
                var nextItems = nextPageResult.data.results.items;
               
                for (var y in nextItems) {
                    var entityType = nextItems[y].entityType;
                   
                    //check custom entities' types (arrTypes) whether contain this type, create one if not
                    if (!arrTypes.includes(entityType)){
                        var createCollectionResult = customEntityProxy.sysCreateCollection(entityType, collectionOptsJson);
                        if (createCollectionResult.status == 200) {
                            //add to the type check array
                            arrTypes.push(entityType);
                            createdTypes ++;
                        }else{
                            response.createCollectionError = "sysCreateCollection call errors from next page, check entityType you injected";
                        }
                    }
                   
                    //after create collection type, we can create custom entity
                    var dataJson = nextItems[y].data;
                    var acl = nextItems[y].acl;
                   
                    var createEntityResult = customEntityProxy.sysCreateEntity(entityType, dataJson, acl, timeToLive, ownerId);
                    if (createEntityResult.status == 200) {
                        bridge.logInfo("page ("+ pageNumber + ") totally created "+ y +" custom entity(ies) with type : ", entityType);
                        createdEntities ++;
                    }
                }
            }else{
                response.getNextPageResultError = "get next page entities error";
            }
        }
       
        response.createdTypes = "totally created custom entity types : "+createdTypes;
        response.createdEntities = "totally created custom entities: "+createdEntities;
    }
}

response;

Note: to convert all users' User Entities to Custom Entities accordingly, go ahead to call RunBatchUserScript() from S2S Explore.

 

Did this answer your question?