0

i am making a content release trigger with a method to update the mobile business app for my work. The objective of all this is to send the data stored by the SNOTES and store it in the backend of the mobile app mentioned above.

ContentVersion Trigger:

im launchig the trigger when is after and is insert. I call the class whit is the "UpdateAppFileds" methods. Next, make 2 querys, a one where call contentDocumentId and another to extrac LinkedEntityId to ContentDocumentLink. I use a for and inside, check the sobjecttype and i see the sobject to check on which object it is created, when the object is checked, this is assing a var. Finally call the UpdateAppField method.

trigger GO_ContentVersionTrigger on ContentVersion (before delete, after delete, before insert,after insert,after update,before update) {

    System.debug('trigger de content version --->');

    if(Trigger.isAfter){

        if(Trigger.isInsert){
            
          for(ContentVersion itemContentVersion: Trigger.new){
            
            GO_ContentDocumentTriggerHandler docHandler = new GO_ContentDocumentTriggerHandler();

            System.debug('trigger de content version, la id ---> '+ itemContentVersion.Id);

            String contentDocumentId = [SELECT ContentDocumentId FROM contentVersion WHERE id =: itemContentVersion.Id].contentDocumentId;
            List<ContentDocumentLink> linkedEntityId = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId =: contentDocumentId];
            String objectId; //Objetos relacionados (Case o Work Order) ¡Cuidado! User también se guarda relacionado, por eso se realiza dicha validaciób

            for(ContentDocumentLink itemContentDocumentLink : linkedEntityId){
              System.debug('Objeto --> ' + itemContentDocumentLink.linkedEntityId.getsobjecttype().getDescribe().getName());

              if(itemContentDocumentLink.linkedEntityId.getsobjecttype().getDescribe().getName() == 'Case') {
                objectId = itemContentDocumentLink.linkedEntityId;
                System.debug('CASE --> ' + objectId);
              }else if(itemContentDocumentLink.linkedEntityId.getsobjecttype().getDescribe().getName() == 'Work Order'){
                objectId = itemContentDocumentLink.linkedEntityId;
                System.debug('WORK ORDER --> ' + objectId);
              }else{
                System.debug('NADA');
              }
            }

            docHandler.updateAppFields(contentDocumentId, objectId);
          }
        }
    }
}

The triggerHandler

In this class, create a method to launch in the trigger. The first method is the method UpdateAppFields. Have a 2 params, contentDocumentId and LinkedEntityId, used to filter in a contentDocument query. Next, instanciate a class to conect the php backend to de salesforce org and check the object type SNOTE, send the object linked and the http method and finally execute the batch.

    public void updateAppFields(Id recordId, Id objectId){

        //Registro Case o Work Order
        System.debug('-------------------------->>>>>>>>>> ' + recordId);


        try{
            
            //Se hace una query al ContentDocumentLink pasandole la Id del contentVersion de la nota
            List<ContentDocumentLink> listCDlink = [SELECT Id, ContentDocument.FileType, ContentDocumentId, LinkedEntityId 
                                                    FROM ContentDocumentLink
                                                    WHERE ContentDocumentId =: recordId
                                                    AND LinkedEntityId =: objectId];

            System.debug('ContentDocumentLink ------------------>>>>>>>>>> ' + listCDlink[0].Id);

            //Se instancia la clase de php, con el objetivo de usas sus metodos para enviar los datos a la app movil
            GO_PhpBatch_App phpRequest = new GO_PhpBatch_App();
            
            if(listCDlink.size() > 0){

                System.debug('Dentro del if que comprueba el nº de registros');
                //Se hace un for usando el numero de resultados de la query
                for(Integer i = 0; i < listCDlink.size(); i++){

                    //Se hace un if comparando el tipo de documento que es la nota y en los objteos que puede estar anclada, que son Case y Workorder
                    if(listCDlink[i].ContentDocument.FileType == 'SNOTE' && 
                    (listCDlink[i].LinkedEntityId.getsobjecttype().getDescribe().getName() == 'Case' ||
                    listCDlink[i].LinkedEntityId.getsobjecttype().getDescribe().getName() == 'WorkOrder')) {

                        System.debug('php request setup batch');
                        //Se pasa el objeto entero por el constructor, no ninguno de sus paramentros, y el httpmethod
                        phpRequest.SetUpBatch(listCDlink[i], GO_PhpBatch_App.httpMethods.POST);

                        System.debug('php request setup batch');
                        //Se ejecuta el batch para hacer la conexion y enviar los datos
                        GO_PhpBatch_App.execute(phpRequest);

                        system.debug(listCDlink[i]);
                    }
                }
            }

        }catch(Exception e){
            system.debug('----------> La actualizacion no se ha efectuado, consultar el Log para mas detalles');
            system.debug(e.getMessage());
        }
    }
}

The problem

The conection to database is good, but the insert failed and return this error: List index out of bounds: 0 Im think the problem is the query, but worked in the inspector, now, i have no idea how to proceed. why this error occurs?

0

Your Answer

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

Browse other questions tagged or ask your own question.