How to Link / Get Config Item to a Ticket through Webservice (SOAP or REST) in OTRS

前端 未结 2 610
庸人自扰
庸人自扰 2021-01-01 07:19

I want to know how to get and link the ticket to Configuration item through SOAP or REST Webservice. I have imported this Restfull Web service in admin console and successfu

2条回答
  •  天涯浪人
    2021-01-01 07:57

    After lot of work, i've found the solution to made it by POST REST call. My main trouble was about creating LinkObject with right %Param. So, i've implement direct code to aquire right call LinkAdd. Mainly, Artjoman provide the way. But i catch some errors on it, so here is another perl module in OTRS_HOME/Custom/Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm:

    # --
    # Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm - GenericInterface LinkAdd operation backend
    # Copyright (C) 2016 ArtyCo (Artjoms Petrovs), http://artjoms.lv/
    # --
    # This software comes with ABSOLUTELY NO WARRANTY. For details, see
    # the enclosed file COPYING for license information (AGPL). If you
    # did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
    # --
    
    package Kernel::GenericInterface::Operation::LinkObject::LinkAdd;
    
    use strict;
    use warnings;
    
    use Kernel::System::ObjectManager;
    use Kernel::System::VariableCheck qw(IsStringWithData IsHashRefWithData);
    
    =head1 NAME
    
    Kernel::GenericInterface::Operation::LinkObject::LinkAdd - GenericInterface Link Create Operation backend
    
    =head1 SYNOPSIS
    
    =head1 PUBLIC INTERFACE
    
    =over 4
    
    =cut
    
    =item new()
    
    usually, you want to create an instance of this
    by using Kernel::GenericInterface::Operation->new();
    
    =cut
    
    sub new {
        my ( $Type, %Param ) = @_;
    
        my $Self = {};
        bless( $Self, $Type );
    
        # check needed objects
        for my $Needed (
            qw( DebuggerObject WebserviceID )
            )
        {
            if ( !$Param{$Needed} ) {
                return {
                    Success      => 0,
                    ErrorMessage => "Got no $Needed!"
                };
            }
    
            $Self->{$Needed} = $Param{$Needed};
        }
    
        # create additional objects
    
        local $Kernel::OM = Kernel::System::ObjectManager->new( %{$Self} );
        $Self->{LinkObject}
            = $Kernel::OM->Get('Kernel::System::LinkObject');
    
        return $Self;
    }
    
    =item Run()
    
    Create a new link.
    
        my $Result = $OperationObject->Run(
            Data => {
                SourceObject => 'Ticket',
                SourceKey    => '321',
                TargetObject => 'Ticket',
                TargetKey    => '12345',
                Type         => 'ParentChild',
                State        => 'Valid',
                UserID       => 1,
            },
        );
    
        $Result = {
            Success      => 1,                                # 0 or 1
            ErrorMessage => '',                               # In case of an error
            Data         => {
                Result => 1,                                  # 0 or 1 
            },
        };
    
    =cut
    
    sub Run {
        my ( $Self, %Param ) = @_;
    
        # check needed stuff
        if ( !IsHashRefWithData( $Param{Data} ) ) {
            return $Self->ReturnError(
                ErrorCode    => 'LinkAdd.MissingParameter',
                ErrorMessage => "LinkAdd: The request is empty!",
            );
        }
    
        my $LinkID = $Self->{LinkObject}->LinkAdd(
            'SourceKey' => $Param{Data}{SourceKey},
            'SourceObject' => $Param{Data}{SourceObject},
            'State' => $Param{Data}{State},
            'TargetKey' => $Param{Data}{TargetKey},
            'TargetObject' => $Param{Data}{TargetObject},
            'Type' => $Param{Data}{Type},
            'UserID' => $Param{Data}{UserID},
        );
    
        if ( !$LinkID ) {
            return $Self->ReturnError(
                ErrorCode    => 'LinkAdd.AuthFail',
                ErrorMessage => "LinkAdd: Authorization failing!",
            );
        }
    
        return {
            Success => 1,
            Data    => {
                Result => $LinkID,
            },
        };
    }
    
    sub ReturnError {
        my ( $Self, %Param ) = @_;
    
        $Self->{DebuggerObject}->Error(
            Summary => $Param{ErrorCode},
            Data    => $Param{ErrorMessage},
        );
    
        # return structure
        return {
            Success      => 1,
            ErrorMessage => "$Param{ErrorCode}: $Param{ErrorMessage}",
            Data         => {
                Error => {
                    ErrorCode    => $Param{ErrorCode},
                    ErrorMessage => $Param{ErrorMessage},
                },
            },
        };
    }
    
    1;
    
    =back
    
    =head1 TERMS AND CONDITIONS
    
    This software is part of the OTRS project (L).
    
    This software comes with ABSOLUTELY NO WARRANTY. For details, see
    the enclosed file COPYING for license information (AGPL). If you
    did not receive this file, see L.
    
    =cut
    

    So, making POST call to (http://otrs_host/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/LinkAdd?UserLogin=login&Password=password) with json

    {"SourceObject":"Ticket","SourceKey":"7","TargetObject":"ITSMConfigItem","TargetKey":"1","Type":"DependsOn","State":"Valid","UserID":"1"}

    will create a link between Ticket and ITSMConfigItem (not a computer, hardware and so on.) I think, this simple and quite rude solution will help to understand how to add a full-api operations to your REST otrs with a better(but working) way.

提交回复
热议问题