CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Civicrm api error
Pages: [1] 2

Author Topic: Civicrm api error  (Read 3048 times)

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Civicrm api error
March 08, 2013, 12:41:11 am
At the start when i tried calling civicrm api on a js of a modulle it gave me the following errors

1.Uncaught ReferenceError: cj is not defined==>
CODE: cj().crmAPI('Contact','get',{}, {'callBack': function(data){ alert(data.count);}  });)
2.Uncaught TypeError: Object #<an Object> has no method 'crm API'==>
CODE  :$().crmAPI('Contact','get',{}, {'callBack': function(data){ alert(data.count);}  });

To rectify this error i included following civicrm js files (path :sites/all/modules/civicrm/js/)
Common.js ,noconflict.js,rest.js,Contact.js
And
in my MY_MODULE.module i did the following:
  $form['#attached']['js'] = array(drupal_get_path('module','MY_MODULE') . '/Contact.js');
  $form['#attached']['js'] = array(drupal_get_path('module','MY_MODULE') . '/rest.js');
  $form['#attached']['js'] = array(drupal_get_path('module','MY_MODULE') . '/noconflict.js');
   $form['#attached']['js'] = array(drupal_get_path('module','MY_MODULE') . '/Common.js');
Finally by this the above error disappeared but
could did not get a alert message.
Next what i tried was
in the console at testsns/civicrm/dashboard i got the alert message
then I created a URL path alias : testsns.com/civicrm/user-profile for  testsns.com/node/123

Then with the new URL  testsns.com/civicrm/user-profile i tried the code in console(command-line).
At this point of time  i again started getting the error shown above:
1.Uncaught ReferenceError: cj is not defined
2.Uncaught TypeError: Object #<an Object> has no method 'crm API'
 
So this what i comclude from above :

civicrm api  works when you call the api with command-line on /civicrm/dashboard, but it doesn't work on /node/123 or its url alias

When i try calling on dashboard 
cj().crmAPI('Contact','get',{}, {'callBack': function(data){ alert(data.count);}  });
I get a alert message
But when i try calling on /node/123
I dont get alert message

« Last Edit: March 08, 2013, 12:47:16 am by harshal »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 08, 2013, 06:31:01 am
Hi,

a "non civi" page is indeed not including the jquery files that are by default when it's a civicrm page (eg the dashboard).

The file you need to add in the header of your page is js/rest.js

With that, you should have the crmAPI() method available. it should be on the drupal jquery, so $().crmAPI, not cj().crmAPI.

By default, crmAPI is only accessible if you do have "access civicrm" permission, so that might be an issue.

Check in firebug what's happening (eg if the ajax backend is properly called but on error or if it isn't even trying):
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 11, 2013, 02:30:39 am
Sorry,for late reply.
As per your comment,I have checked and observed the following
1.I have the 'access  civicrm' permissions
2.i have added rest.js.
3.i have tried using $().crmAPI but it still gives error :
$().crmAPI('Contact','get',{}, {'callBack': function(data){ alert(data.count);}  });
-->TypeError: undefined is not a function
What should I fill inside the quotes $('')    --->(May be this is what is creating error).

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Civicrm api error
March 11, 2013, 04:51:41 am
Try a more simple example to find where the error is. Your syntax doesn't look correct to me. See http://wiki.civicrm.org/confluence/display/CRMDOC42/AJAX+Interface where it says:
Code: [Select]
the syntax is:
cj().crmAPI ('entity','action',params);

So crmAPI has 3 parameters but your code is calling it with 4. Try just calling the API to get simple result and only once that is working, add your callback.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 12, 2013, 12:10:51 am
Even after trying the code given below,
$().crmAPI('Contact','get', {'callBack': function(data){ alert(data.count);}  });
I am getting the error.
Also when I add rest.js file i.e.civicrm js files, the other js functionality does not work.
The above code on the console gives error :- TypeError: undefined is not a function .
When written on js part it does not work.

Basically I just wanted  a crmAPI() function to  work on javascript in drupal 7.
Please help

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 12, 2013, 03:54:56 am
Are you sure rest.js is loaded? can you debug step by step to see if it is adding crmAPI into the available fn on jquery?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 12, 2013, 04:51:30 am
To load rest.js,
I have added the rest.js with the following steps:
1. Copied the rest.js file from civicrm module to my module.
2.  $form['#attached']['js'] = array(drupal_get_path('module','MY_MODULE') . '/js/rest.js');
3.When I do 'inspect element' on respective page and check the resources I can seee the rest.js file.
4.Due to adding of rest.js other js functionality is not working.
It would be kind enough from you if you could just give brief idea on how to debug.(Should I use  Logger into rest.js file for debugging? )

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 12, 2013, 05:41:22 am
Open firebug and check the console, see if you have error messages (sounds like you have)

I would suggest you NOT to copy but to use it directly from civicrm
array(drupal_get_path('module','civicrm') . '/js/rest.js')
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 13, 2013, 01:44:01 am
Even after using rest.js directly from the civicrm module it is giving error which is as follows :-
CODE :     $().crmAPI('Contact','get', {'callBack': function(data){ alert(data.count);}  });
ERROR :Uncaught TypeError: Object #<an Object> has no method 'crmAPI'
It is giving me the same error when i try on console.
What should i try,
To use civicrm api in js??
« Last Edit: March 13, 2013, 02:53:38 am by harshal »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 13, 2013, 06:54:22 am
Hi,

And you don't have any other error message and rest.js is properly included? weird.

Can you publish your module on github or drupal sandbox?

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 14, 2013, 02:48:02 am
Please check my module on the following Link : https://github.com/harshal6/civicrm-api
and let me know my errors.
« Last Edit: March 14, 2013, 03:11:04 am by harshal »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 14, 2013, 04:58:02 am
Hi,

Is this supposed to work stand alone? Will check later today, quick feed back:

instead of
 
Code: [Select]
$sql = array();
   $query = "SELECT city  FROM testsns_civicrm.civicrm_contact,testsns_civicrm.civicrm_address";
   $query .= " WHERE contact_type = 'Organization' AND civicrm_contact.id = civicrm_address.contact_id ";
   $sql = db_query($query,array('fetch' => PDO::FETCH_ASSOC));
   $records = array();
   $records = $sql->fetchAll();

Why don't you use the api
Code: [Select]
$r = civicrm_api("contact","get", array("contact_type=>"organization", return=>array ("city"), count=1000);

$records=$r.values
Not tested, but shouldn't be too far away.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 14, 2013, 06:13:50 am
ok . I will just check that and let you know about how that works.
 
Could you pls check why code on line 9 of branch.js is giving error.(or may be it is not giving error on your side).
Code: [Select]
     $().crmAPI('Contact','get', {'callBack': function(data){ alert(data.count);}  });


« Last Edit: March 14, 2013, 06:25:20 am by harshal »

harshal

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7.24
  • MySQL version: Mysql 5.1.32
  • PHP version: 5.2
Re: Civicrm api error
March 21, 2013, 06:43:11 am
when i tried the code given below

Code: [Select]
$records=$r.values

values gives  me errors .Pls help in this topic .As it will be useful for me to have civicrm api work on js
« Last Edit: March 21, 2013, 06:44:59 am by harshal »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Civicrm api error
March 21, 2013, 06:59:43 am
Sorry, it's an array, not an object, so $r["values"]

print_r($r) to see what's the result.

As for you extension, could you tell me how to test it? should I go to a specific url?
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Civicrm api error

This forum was archived on 2017-11-26.