How to copy an image from lookup object record to current object record

Introduction

This article  provides information about how to copy the image field of a lookup record to the current record image field.

Steps to follow

For example, Consider we have two objects Source Image and Copy Image. Copy Image object has lookup to the Source Image. I want to copy the image field of the Source Image object record to the Copy Image object record.


1) Create a class by navigating to GearIcon.png> Customization > Developer Resources > Classes > New class
2) Invoke this from the Business rule of Copy Image object create / update event.

3) Create/update a record of Copy Image. Now the user can see the same image on Copy Image record.

Code:



package com.platform.mounika.test;

import com.platform.api.*;
import java.io.File;
import com.platform.beans.*;
import com.platform.api.utility.*;
import java.util.*;

public class copyImage

{
  public void addimg(Parameters p) throws Exception{

    String id=p.get("id");
    Base64Codec base64 = new Base64Codec();
    String precordID = p.get("source_lookup");

    // retrieve the lookup record data

    Result result = Functions.getRecord("source_image", "image_field", precordID);

    if (result != null && result.getCode() == 1)

    {

      Parameters params = result.getParameters();
      if (params != null)     

      {

        String title = params.get("subject");
        String attachment = params.get("image_field");
        PlatformFileBean file = params.getPlatformFileBean("image_field");
        String encodedContent = file.getEncodedFileContent();
        byte[] bytes = base64.decode(encodedContent);

        String text = new String(bytes);
        String encodedString = base64.encodeToString(bytes);
        PlatformFileBean file1 = new PlatformFileBean(attachment.split(":")[0], encodedString);

        // adding image field to current record.

        p.add("copy_image", file1);
        Functions.updateRecord("copy_image", id, p);
      }
    }
  }
}