Tuesday, September 25, 2018

Web Services .NET sample code for attach/detach sample operation

The attach/detach operations can be used to attach or remove a file record from your file cabinet to an entity record. Here's a sample code in C# to attach a file to an employee record.

Here is how you can locate the files attached to an employee record:
List > Employees > Employees > Select an employee, in the 'General' tab there is a 'File Cabinet' subtab.
 
To locate the files in your Netsuite account:
Documents > Files > File Cabinet
 
The code: 

//file record

RecordRef recFile = new RecordRef();

recFile.internalId = "373";

recFile.type = RecordType.file;

recFile.typeSpecified = true;

 

//employee record

RecordRef recEmployee = new RecordRef();

recEmployee.internalId = "470";

recEmployee.type = RecordType.employee;

recEmployee.typeSpecified = true;

 

//attach class

AttachBasicReference attachBasicRef = new AttachBasicReference();

attachBasicRef.attachedRecord = recFile;

attachBasicRef.attachTo = recEmployee;

 

WriteResponse response = _service.attach(attachBasicRef);

if (response.status.isSuccess) {

Response.Write("Successfully Attach a file record");

}

else {

Response.Write("Attaching file record, fail");

}

Detaching a file record to an employee record: 

//file record

RecordRef recFile = new RecordRef();

recFile.internalId = "373";

recFile.type = RecordType.file;

recFile.typeSpecified = true;

 

//employee record

RecordRef recEmployee = new RecordRef();

recEmployee.internalId = "470";

recEmployee.type = RecordType.employee;

recEmployee.typeSpecified = true;

 

//detach class

DetachBasicReference detachBasicRef = new DetachBasicReference();

detachBasicRef.detachedRecord = recFile;

detachBasicRef.detachFrom = recEmployee;

 

WriteResponse response = _service.detach(detachBasicRef);

 

if (response.status.isSuccess) {

Response.Write("Successfully Attach a file record");

}

else {

Response.Write("Attaching file record, fail");

}

 

NOTE: The _service is a NetsuiteService class object. The login process is omitted in this sample code.

Here is attach/detach SOAP request:
Attach:
<Envelope>
    <Header />
    <Body>
        <attach>
            <attachReference type="AttachBasicReference">
                <attachTo internalId="470" type="employee" type="RecordRef"/>
                <attachedRecord internalId="219" type="file" type="RecordRef"/>
            </attachReference>
        </attach>
    </Body>
</Envelope>

Detach:
<Envelope>
    <Header />
    </Header>
    <Body>
        <detach>
            <detachReference type="DetachBasicReference">
                <detachFrom type="employee" internalId="470" type="RecordRef"/>
                <detachedRecord type="file" internalId="373" type="RecordRef"/>
            </detachReference>
        </detach>
    </Body>
</Envelope>

No comments:

Post a Comment