Search code examples
ibm-cloud-infrastructure

Error deleting an user to notify in Softlayer


When I delete an user to notify, it returns Error: com.softlayer.api.ApiException$Internal: All objects being deleted must have an ID set.(code: SoftLayer_Exception, status: 500) I've set a proper UserId and DeviceId, but it still returns the same error. This is my sample code.

private void deleteUserTonotify(){

    // Define the hardware and user identifiers
    Long hardwareId = new Long(164420);
    Long userId = new Long(538047);

    // Define SoftLayer_User_Customer_Notification_Hardware service
    com.softlayer.api.service.user.customer.notification.Hardware.Service hardwareService = com.softlayer.api.service.user.customer.notification.Hardware
            .service(client);

    // Declare SoftLayer_User_Customer_Notification_Hardware List
    List<com.softlayer.api.service.user.customer.notification.Hardware> templateObjects = new ArrayList<com.softlayer.api.service.user.customer.notification.Hardware>();


    com.softlayer.api.service.user.customer.notification.Hardware templateObject = new com.softlayer.api.service.user.customer.notification.Hardware();
    templateObject.setUserId(userId);
    templateObject.setHardwareId(hardwareId);

    // Add templateObject to templateObjects
    templateObjects.add(templateObject);

    try {
        boolean result = hardwareService.deleteObjects(templateObjects);
        System.out.println(" delete result : " + result);

    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
}

Solution

  • Each object in the array must have at least their id properties defined. You can find further information in the next link: http://sldn.softlayer.com/reference/services/SoftLayer_User_Customer_Notification_Hardware http://sldn.softlayer.com/reference/services/SoftLayer_User_Customer_Notification_Hardware/deleteObjects

    I’ve changed this in your script and it works:

    package SoftLayer_Java_Scripts.Examples;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.softlayer.api.*;
    import com.softlayer.api.service.user.customer.notification.Hardware;
    
    public class EG
    {
      private static String user = "set me";
      private static String apiKey = "set me";
    
      private static ApiClient client = new RestApiClient().withCredentials(user, apiKey);
    
      public static void main( String[] args )
      {
        deleteUserTonotify();
      }
    
      private static void deleteUserTonotify(){
    
        // Define the SoftLayer_User_Customer_Notification_Hardware id here
        Long userNotificationfHardwareId = new Long(280346);
    
        // Define SoftLayer_User_Customer_Notification_Hardware service
        Hardware.Service hardwareService = Hardware.service(client);
    
        // Declare SoftLayer_User_Customer_Notification_Hardware List
        List<Hardware> templateObjects = new ArrayList<Hardware>();
    
        // Creating a template object
        Hardware templateObject = new Hardware();
    
        templateObject.setId(userNotificationfHardwareId);
    
        // Add templateObject to templateObjects
        templateObjects.add(templateObject);
    
        try {
          boolean result = hardwareService.deleteObjects(templateObjects);
          System.out.println(" delete result : " + result);
    
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
      }
    
      // This method can be used to retrieve and print 
      // the SoftLayer_User_Customer_Notification_Hardware ids by hardware id. 
        private static void printUserNotificationListByHardwareId(Long hardwareId, Long userId){
        Hardware.Service hardwareService = Hardware.service(client);
    
        try {
          List<Hardware> userNotificationByHardwareList = hardwareService.findByHardwareId(hardwareId);
          if(userNotificationByHardwareList.isEmpty()){
            System.out.println("There's no User Customer Notification for hardware id: " + hardwareId);
          }
          else{
            for(Hardware h : userNotificationByHardwareList){
              if((long)h.getUserId() == (long)userId){
                System.out.println(h.getId());
                break;
              }
            }
          }
        } catch (Exception e) {
          System.out.println("Error: " + e);
        }
      }
    }