HOWTO: Dealing with set_relationship failures

sugarcrmdevelopers —  December 8, 2011 — 19 Comments

I’ve seen time and time again issues with the set_relationship call in the SOAP and REST web services, when people would think they are doing all the right things in making this call to create a relationship between the two records, only to be given the sad feedback of:

Success: 0
Failures: 1

This can be one of the more confusing web services error messages, since often times it seems like you are doing the right thing and there’s not much in the way of feedback on what went wrong.

So looking at all of the issues I have seen people have using this call, I’ve found it’s usually something fairly simple that is causing the problems. Here’s the list of things to check for when you are trying to set a relationship between records and it’s not working.

  1. Make sure that the two modules have a relationship between them. While this seems like a no-brainer, you’d be surprised how often people forget about this.
  2. You need to specify the programmatic name of the module rather than the display name. Custom modules and a few out of the box modules like Targets and Cases are places people run into problems here and use the display name of the module as the second parameter to set_relationship instead of what the name of the module is in the code. So for the Cases module, you’d use ‘aCases‘, and Targets would be ‘Prospects‘.
  3. The fourth parameter must be the name of link field in the module’s vardefs, not the name of the target module or the relationship. This tends to be the most confusing part for people, as finding this out requires you to dig into the module’s vardefs.php file for more details, as you need to find a field of type ‘link’ that the ‘relationship’ field matching the name of the relationship between the modules. You can find the relationship name in Studio by looking at the ‘Relationships’ screen in Studio for the module.
  4. Try doing the relationship in reverse. Sometimes the module only defines the link field on one side of the relationship, so in these cases just reversing the order in which you are setting the relationship can make it work.

If you have anyone other tips for solving set_relationships issues, feel free to let us know in the comments.

19 responses to HOWTO: Dealing with set_relationship failures

  1. 

    Here’s an idea: put an example up. Would that be so hard?

    I cannot believe that’s it’s basically impossible to find a *working* example php code to set a relationship via SOAP in SugarCRM. I’ve been searching for days and all I can find are countless forums of people asking the exact same question with no (useful) responses.

  2. 

    I mean seriously. ”
    The fourth parameter must be the name of link field in the module’s vardefs, not the name of the target module or the relationship ” Wtf does that even mean? Are you deliberately trying to be obtuse??

    Which module? The module1 or module2? Does that matter? Honestly, I find with virtually everything to do with coding for Sugar it’s like a secret club where every person who answers a question online deliberately uses ridiculously short, unhelpful responses, in some bizarre attempt to sound superior. I understand general public doing that but the documentation on this site is just atrocious. 

  3. 

    Glad this was here, John. I kept referring to the second in the third param, instead of the link name.

  4. 

    this post isn’t very helpful.
    I’m trying to call set_entry via Webservices and can’t figure out what I’m doing wrong…
    SoapUI tells me my call is fine, but when I run the same code under Talend , job fails.
    Please be more specific with code examples on the page. I’m using SugarPro ondemand and cannot edit source php.

  5. 

    hi john,

    i have read in documentaion that soap and rest both support name_value_list param
    but i tried it in rest api v2 sugarcrm ce6.5.4 but never works it creates relationship but with default values

    i am using this code

    //createRelation(‘valid call id’, valid user id’ , ‘Calls’, ‘users’, null);
    function createRelation (id, related_id, module_name, link_field, callback) {
    var params = {
    session: session_id,
    module_name: module_name,
    module_id: id,
    link_field_name: link_field,
    related_ids: [related_id],
    name_value_list: [{
    name: ‘accept_status’,
    value: ‘accept’
    }],
    delete: 0
    };
    if (!callback) {
    var callback=function(response){
    if (response.created > 0) {
    console.log(“relationship created”);
    }
    if (response.deleted > 0) {
    console.log(“relatinoship deleted”);
    }
    if (response.failed > 0) {
    console.log(“Error creating the relationship”);
    }
    }
    }
    doRestCall(‘set_relationship’, params, callback);
    }

    am i doing wrong?? is there any other way to do this???
    any help will be appreciated.
    thanks in advance

  6. 

    I read a lot of these posts and was still stuck, delving into the code I see the new_handle_set_relationship function get called, and noticed that it checks ACLAccess for DetailView. For the purpose of the example I commented out the “return false” and everything worked perfectly.

    if(!$mod->ACLAccess(‘DetailView’)){
    $GLOBALS[‘log’]->info(‘End: SoapHelperWebServices->new_handle_set_relationship’);
    //return false;
    }

    So another tip and trick here is to make sure that the user you are using to connect to the SOAP API actually has DetailView role permissions over the relevant module.

    Also the SOAP code is old as the set_relationship function in the version 4 I am using still uses retrieve as opposed to GetBean,

Trackbacks and Pingbacks:

  1. SOAP set_relationship with custom modules - SugarCRM Forums - January 26, 2012

    […] […]

  2. SugarCRM Developer Blog » Blog Archive » HOWTO: Debug web services problems - April 9, 2012

    […] issue tends to revolve around the oh-so-tempermental set_relationships() call, which I wrote a whole blog post on a while back, so I’ll let you dig in there if you are seeing issues with that […]

  3. angels leads soap php - May 7, 2012

    […] […]

  4. Setting a relationship between an User and an Account using SOAP - August 30, 2012

    […] […]

  5. 500 Error relating Account to Lead through SOAP - August 30, 2012

    […] […]

  6. The after_relationship_add logic hook ok for Email but not for Calls on a Case - October 22, 2012

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s