Monday, June 27, 2016

SUGAR CRM 7: Bulkified POST Service using a custom API

1. Please take a look at the previous Post: http://programmersrealm.blogspot.com/2016/06/sugar-crm-7-creating-custom-api.html

2. Change reqType to 'POST'

3. Change path to array("Accounts", "PostExample")

4. Change method to 'MyPost'

5. Create mehtod MyPost()
public function MyPost($api, $args) 
{
    if (empty($args)) 
    {        
      return false; 
    } 
    $results = array(); 
    foreach ($args as $module => $records) 
    {        
      if (is_array($records)) 
      { 
        foreach ($records as $fieldsArray) 
        {                
          $sugarBean = 
           $this->create($module, $fieldsArray);                
          $results[$module][] = $sugarBean->id; 
        } 
      } 
    } 
    return $results; 
}

private function create($module, $fieldsArray) 
{    
    $sugarBean = BeanFactory::newBean($module);
    if (is_null($sugarBean)) 
    {        
      return null; 
    }    
    foreach ($fieldsArray as $field => $data) 
    {        
      $sugarBean->$field = $data; 
    }    
    $sugarBean->save(); 
    return $sugarBean; 
}

6. Add your url to Postman with the new endpoint (i.e. http://<my path>/rest/v10/Accounts/PostExample) with POST as the service

7. Add Authorization Token to Postman (if noauthorizationrequired is set to true, there's no need to add a token)

8. Create JSON data and put it under Body in Postman
{
"Accounts":
    [
        {
            "id" : "edf5e66e-6a2a-ccb3-d0bc-577142ee65ca",
            "name":"Testing10",
            "billing_address_city" : "ELP"
        },
        {
            "id" : "682d8dce-d397-85ce-7cc3-57714287c485",
            "name":"Testing3",
            "billing_address_city" : "PHX"
            
        },
        {
            "id" : "7de85759-0203-7a1c-240e-57714241b289",
            "name":"Testing2",
            "billing_address_city" : "LA"
            
        }
    ]
}

No comments:

Post a Comment